History log of /freebsd-9.3-release/sbin/ipfw/ipfw2.c
Revision Date Author Comments
(<<< Hide modified files)
(Show modified files >>>)
# 267654 19-Jun-2014 gjb

Copy stable/9 to releng/9.3 as part of the 9.3-RELEASE cycle.

Approved by: re (implicit)
Sponsored by: The FreeBSD Foundation

# 265699 08-May-2014 melifaro

Merge r258708, r258711, r260247, r261117.

r258708:
Check ipfw table numbers in both user and kernel space before rule addition.
Found by: Saychik Pavel <umka@localka.net>

r258711:
Simplify O_NAT opcode handling.

r260247:
Use rnh_matchaddr instead of rnh_lookup for longest-prefix match.
rnh_lookup is effectively the same as rnh_matchaddr if called with
empy network mask.

r261117:
Reorder struct ip_fw_chain:
* move rarely-used fields down
* move uh_lock to different cacheline
* remove some usused fields


# 265692 08-May-2014 melifaro

Merge r258677.

Fix key lookup in ipfw(8) broken since r232865.
Print warning for IPv4 address strings which are valid in
inet_aton() but not valid in inet_pton(). (1)

Found by: Özkan KIRIK <ozkan.kirik@gmail.com>
Submitted by: Ian Smith <smithi@nimnet.asn.au> (1)


# 250762 18-May-2013 melifaro

MFC r248552, r248971

Add ipfw support for setting/matching DiffServ codepoints (DSCP).

Setting DSCP support is done via O_SETDSCP which works for both
IPv4 and IPv6 packets. Fast checksum recalculation (RFC 1624) is done for IPv4.
Dscp can be specified by name (AFXY, CSX, BE, EF), by value
(0..63) or via tablearg.

Matching DSCP is done via another opcode (O_DSCP) which accepts several
classes at once (af11,af22,be). Classes are stored in bitmask (2 u32 words).

Many people made their variants of this patch, the ones I'm aware of are
(in alphabetic order):

Dmitrii Tejblum
Marcelo Araujo
Roman Bogorodskiy (novel)
Sergey Matveichuk (sem)
Sergey Ryabin

PR: kern/102471, kern/121122

Fix ipfw rule validation partially broken by r248552.


# 248505 19-Mar-2013 melifaro

Merge r247666, r247712, r247811.

Fix ipfw table argument parsing/printing.
Fix style.

PR: kern/175909
Submitted by: Daniel Hagerty <hag@linnaean.org>

Implement buffer size checking in ipfw(8) add cmd.

PR: bin/65961
Submitted by: Eugene Grosbein <eugen@grosbein.pp.ru>

Do not suddenly fail on some rulesets if -n (syntax check only) is specified
and ipfw(4) module is not loaded.


# 234597 23-Apr-2012 melifaro

MFC r232865, r232868, r233478

- Add ipfw eXtended tables permitting radix to be used for any kind of keys.
- Add support for IPv6 and interface extended tables
- Make number of tables to be changed in runtime in range 0..65534.
- Use IP_FW3 opcode for all new extended table cmds

No ABI changes are introduced. Old userland will see valid tables for
IPv4 tables and no entries otherwise. Flush works for any table.

IP_FW3 socket option is used to encapsulate all new opcodes:
/* IP_FW3 header/opcodes */
typedef struct _ip_fw3_opheader {
uint16_t opcode; /* Operation opcode */
uint16_t reserved[3]; /* Align to 64-bit boundary */
} ip_fw3_opheader;

New opcodes added:
IP_FW_TABLE_XADD, IP_FW_TABLE_XDEL, IP_FW_TABLE_XGETSIZE, IP_FW_TABLE_XLIST

ipfw(8) table argument parsing behavior is changed:
'ipfw table 999 add host' now assumes 'host' to be interface name instead of
hostname.

New tunable:
net.inet.ip.fw.tables_max controls number of table supported by ipfw in given
VNET instance. 128 is still the default value.

Sysctl change:
net.inet.ip.fw.tables_max is now read-write.

New syntax:
ipfw add skipto tablearg ip from any to any via table(42) in
ipfw add skipto tablearg ip from any to any via table(4242) out

This is a bit hackish, special interface name '\1' is used to signal interface
table number is passed in p.glob field.

Sponsored by Yandex LLC

Approved by: kib(mentor)


# 234278 14-Apr-2012 glebius

Merge 231076,231078:
Make the 'tcpwin' option of ipfw(8) accept ranges and lists.

Submitted by: sem


# 225736 22-Sep-2011 kensmith

Copy head to stable/9 as part of 9.0-RELEASE release cycle.

Approved by: re (implicit)


# 225044 20-Aug-2011 bz

Add support for IPv6 to ipfw fwd:
Distinguish IPv4 and IPv6 addresses and optional port numbers in
user space to set the option for the correct protocol family.
Add support in the kernel for carrying the new IPv6 destination
address and port.
Add support to TCP and UDP for IPv6 and fix UDP IPv4 to not change
the address in the IP header.
Add support for IPv6 forwarding to a non-local destination.
Add a regession test uitilizing VIMAGE to check all 20 possible
combinations I could think of.

Obtained from: David Dolson at Sandvine Incorporated
(original version for ipfw fwd IPv6 support)
Sponsored by: Sandvine Incorporated
PR: bin/117214
MFC after: 4 weeks
Approved by: re (kib)


# 224942 17-Aug-2011 jhb

Fix a regression where a rule containing a source port option after a
destination IP would incorrectly display the source port as a destination
port.

Reviewed by: luigi
Approved by: re (kib)
MFC after: 1 week


# 223666 29-Jun-2011 ae

Add new rule actions "call" and "return" to ipfw. They make
possible to organize subroutines with rules.

The "call" action saves the current rule number in the internal
stack and rules processing continues from the first rule with
specified number (similar to skipto action). If later a rule with
"return" action is encountered, the processing returns to the first
rule with number of "call" rule saved in the stack plus one or higher.

Submitted by: Vadim Goncharov
Discussed by: ipfw@, luigi@


# 223262 18-Jun-2011 benl

Fix clang warnings.

Approved by: philip (mentor)


# 223080 14-Jun-2011 ae

Implement "global" mode for ipfw nat. It is similar to natd(8)
"globalport" option for multiple NAT instances.

If ipfw rule contains "global" keyword instead of nat_number, then
for each outgoing packet ipfw_nat looks up translation state in all
configured nat instances. If an entry is found, packet aliased
according to that entry, otherwise packet is passed unchanged.

User can specify "skip_global" option in NAT configuration to exclude
an instance from the lookup in global mode.

PR: kern/157867
Submitted by: Alexander V. Chernikov (previous version)
Tested by: Eugene Grosbein


# 222473 30-May-2011 ae

Add tablearg support for ipfw setfib.

PR: kern/156410
MFC after: 2 weeks


# 220804 18-Apr-2011 glebius

More whitespace fixes.

Checked with: md5, diff -x -w


# 220802 18-Apr-2011 glebius

Whitespace fixes.

Checked with: md5, diff -w


# 215179 12-Nov-2010 luigi

The first customer of the SO_USER_COOKIE option:
the "sockarg" ipfw option matches packets associated to
a local socket and with a non-zero so_user_cookie value.
The value is made available as tablearg, so it can be used
as a skipto target or pipe number in ipfw/dummynet rules.

Code by Paul Joe, manpage by me.

Submitted by: Paul Joe
MFC after: 1 week


# 206846 19-Apr-2010 luigi

fix 64-bit build

Reported by: Robert Noland


# 206843 19-Apr-2010 luigi

Slightly different handling of printf/snprintf for unaligned uint64_t,
which should improve readability, and also to ease the port to
platforms that do not support %llu

MFC after: 3 days


# 206266 06-Apr-2010 ume

Set net.inet6.ip6.fw.enable as well.


# 205631 24-Mar-2010 luigi

fix another bug in "ipfw set N ..."

Submitted by: Marcin Wisnicki


# 205179 15-Mar-2010 luigi

print correctly commands of the form

ipfw add 100 allow ip from { 1.2.3.4 or 5.6.7.8 }

(note that the above example could be better written as

ipfw add 100 allow dst-ip 1.2.3.4,5.6.7.8

Submitted by: Riccardo Panicucci


# 205169 15-Mar-2010 luigi

Implement "lookup dscp N" which does a lookup of the DSCP (top 6 bits
of ip->ip_tos) in a table. This can be useful to direct traffic to
different pipes/queues according to the DSCP of the packet, as follows:

ipfw add 100 queue tablearg lookup dscp 3 // table 3 maps dscp->queue

This change is a no-op (but harmless) until the two-line kernel
side is committed, which will happen shortly.


# 204717 04-Mar-2010 luigi

fix handling of sets


# 204591 02-Mar-2010 luigi

Bring in the most recent version of ipfw and dummynet, developed
and tested over the past two months in the ipfw3-head branch. This
also happens to be the same code available in the Linux and Windows
ports of ipfw and dummynet.

The major enhancement is a completely restructured version of
dummynet, with support for different packet scheduling algorithms
(loadable at runtime), faster queue/pipe lookup, and a much cleaner
internal architecture and kernel/userland ABI which simplifies
future extensions.

In addition to the existing schedulers (FIFO and WF2Q+), we include
a Deficit Round Robin (DRR or RR for brevity) scheduler, and a new,
very fast version of WF2Q+ called QFQ.

Some test code is also present (in sys/netinet/ipfw/test) that
lets you build and test schedulers in userland.

Also, we have added a compatibility layer that understands requests
from the RELENG_7 and RELENG_8 versions of the /sbin/ipfw binaries,
and replies correctly (at least, it does its best; sometimes you
just cannot tell who sent the request and how to answer).
The compatibility layer should make it possible to MFC this code in a
relatively short time.

Some minor glitches (e.g. handling of ipfw set enable/disable,
and a workaround for a bug in RELENG_7's /sbin/ipfw) will be
fixed with separate commits.

CREDITS:
This work has been partly supported by the ONELAB2 project, and
mostly developed by Riccardo Panicucci and myself.
The code for the qfq scheduler is mostly from Fabio Checconi,
and Marta Carbone and Francesco Magno have helped with testing,
debugging and some bug fixes.


# 200567 15-Dec-2009 luigi

implement a new match option,

lookup {dst-ip|src-ip|dst-port|src-port|uid|jail} N

which searches the specified field in table N and sets tablearg
accordingly.
With dst-ip or src-ip the option replicates two existing options.
When used with other arguments, the option can be useful to
quickly dispatch traffic based on other fields.

Work supported by the Onelab project.

MFC after: 1 week


# 200183 06-Dec-2009 luigi

restore setting of sin_len (was removed in 1.146 last february) as
it seems that now it is necessary for 'forward' to work outside lo0.
The bug (and fix) was reported on 8.0. This patch probably applies
to RELENG_7 as well.
It seems that 'pf' has a similar bug.

Submitted by: Lytochkin Boris
MFC after: 3 days


# 193702 08-Jun-2009 luigi

add a missing format in a printf
Detected building with gcc 4.3.3

MFC after: 3 days


# 193516 05-Jun-2009 luigi

Several ipfw options and actions use a 16-bit argument to indicate
pipes, queues, tags, rule numbers and so on.
These are all different namespaces, and the only thing they have in
common is the fact they use a 16-bit slot to represent the argument.

There is some confusion in the code, mostly for historical reasons,
on how the values 0 and 65535 should be used. At the moment, 0 is
forbidden almost everywhere, while 65535 is used to represent a
'tablearg' argument, i.e. the result of the most recent table() lookup.

For now, try to use explicit constants for the min and max allowed
values, and do not overload the default rule number for that.

Also, make the MTAG_IPFW declaration only visible to the kernel.

NOTE: I think the issue needs to be revisited before 8.0 is out:
the 2^16 namespace limit for rule numbers and pipe/queue is
annoying, and we can easily bump the limit to 2^32 which gives
a lot more flexibility in partitioning the namespace.

MFC after: 5 days


# 190633 01-Apr-2009 piso

Implement an ipfw action to reassemble ip packets: reass.


# 188005 02-Feb-2009 luigi

Explain that we assume AF_INET and only use the addr and port field
from a struct sockaddr_in, so there is no need to initialize sin_len


# 187983 01-Feb-2009 luigi

put the altq-related functions into a separate file.
Minor cleanup of the includes used by the various source files,
including annotations of why certain headers are used.


# 187787 27-Jan-2009 luigi

fix printing of uint64_t values, so we can use WARNS=2


# 187770 27-Jan-2009 luigi

Put nat and ipv6 support in their own files.

Usual moving of code with no changes from ipfw2.c to the
newly created files, and addition of prototypes to ipfw2.h

I have added forward declarations for ipfw_insn_* in ipfw2.h
to avoid a global dependency on ip_fw.h


# 187769 27-Jan-2009 luigi

Put dummynet-related code in a separate file.
To this purpose, add prototypes for global functions in ipfw2.h
and move there also the list of tokens used in various places in the code.


# 187767 27-Jan-2009 luigi

Start splitting the monster file in smaller blocks.

In this episode:
- introduce a common header with a minimal set of common definitions;
- bring the main() function and options parser in main.c
- rename the main functions with an ipfw_ prefix

No code changes except for the introduction of a global variable,
resvd_set_number, which stores the RESVD_SET value from ip_fw.h
and is used to remove the dependency of main.c from ip_fw.h
(and the subtree of dependencies) for just a single constant.


# 187765 27-Jan-2009 luigi

put the usage() function inline, it was only 1 line and used once;
slightly reformat the help() text;
slightly correct the text for the 'extraneous filename' error message;


# 187764 27-Jan-2009 luigi

put all options in a single struct, and document them.

This will allow us to easily restore the original values when processing
commands from a file (where each individual line can have its own options).


# 187762 27-Jan-2009 luigi

remove a couple of rarely used #define;

change PRINT_UINT from a macro to a function (renaming is
postponed to reduce clutter)


# 187716 26-Jan-2009 luigi

wrap all malloc/calloc/realloc calls so they exit on failure
without having to check in each place.

Remove an wrong strdup from previous commit.


# 187713 26-Jan-2009 luigi

Some implementations of getopt() expect that argv[0] is always the
program name, and ignore that entry. ipfw2.c code instead skips
this entry and starts with options at offset 0, relying on a more
tolerant implementation of the library.

This change fixes the issue by always passing a program name
in the first entry to getopt. The motivation for this change
is to remove a potential compatibility issue should we use
a different getopt() implementation in the future.

No functional changes.

Submitted by: Marta Carbone (parts)
MFC after: 4 weeks


# 187604 22-Jan-2009 luigi

remove some useless #include,
document why timeconv.h is needed

MFC after: 3 days


# 187477 20-Jan-2009 luigi

Fix a number of (innocuous) warnings, and remove a useless test.
There are still several signed/unsigned warnings left, which
require a bit more study for a proper fix.

This file has grown beyond reasonable limits.

We really need to split it into separate components (ipv4, ipv6,
dummynet, nat, table, userland-kernel communication ...) so we can
make mainteinance easier.

MFC after: 1 weeks


# 186297 18-Dec-2008 piso

Honor the quiet (-q) option while adding a nat rule.

Submitted by: Andrey V. Elsukov<bu7cher@yandex.ru>
MFC after: 3 days


# 183890 14-Oct-2008 maxim

o Remove a debug code and restore an accidentally deleted code
in a previous commit.


# 183889 14-Oct-2008 maxim

o Do nothing in show_nat() for a test mode (-n). This prevents
show_nat() from endless loop and makes work ipfw -n nat <...>.

PR: bin/128064
Submitted by: sem
MFC after: 1 month


# 183415 27-Sep-2008 rik

Fix the build.

Noted by: ganbold@


# 183407 27-Sep-2008 rik

Add keyword all in addtion to the table number for the 'list' and the
'flush' actions on tables. Part of PR: 127058.

PR: 127058 (based on)
MFC after: 1 month


# 183263 22-Sep-2008 keramida

Unbreak the build.


# 183241 21-Sep-2008 rik

Add the check of the table number.


# 183228 21-Sep-2008 rik

Move table list to a separate function.


# 183209 20-Sep-2008 rik

Free allocated memory.


# 183208 20-Sep-2008 rik

Remove some unused variables.


# 183206 20-Sep-2008 rik

Style(9) the show_nat() function.


# 183205 20-Sep-2008 rik

Do not do the useless job for an empty table.

MFC after: 1 month


# 182823 06-Sep-2008 rik

Use IPFW_DEFAULT_RULE instead of hardcoded value since now it is
available.

MFC after: 5 days.


# 178916 10-May-2008 julian

Change two variables to size_t to improve portability.
Submitted by: Xin Li


# 178888 09-May-2008 julian

Add code to allow the system to handle multiple routing tables.
This particular implementation is designed to be fully backwards compatible
and to be MFC-able to 7.x (and 6.x)

Currently the only protocol that can make use of the multiple tables is IPv4
Similar functionality exists in OpenBSD and Linux.

From my notes:

-----

One thing where FreeBSD has been falling behind, and which by chance I
have some time to work on is "policy based routing", which allows
different
packet streams to be routed by more than just the destination address.

Constraints:
------------

I want to make some form of this available in the 6.x tree
(and by extension 7.x) , but FreeBSD in general needs it so I might as
well do it in -current and back port the portions I need.

One of the ways that this can be done is to have the ability to
instantiate multiple kernel routing tables (which I will now
refer to as "Forwarding Information Bases" or "FIBs" for political
correctness reasons). Which FIB a particular packet uses to make
the next hop decision can be decided by a number of mechanisms.
The policies these mechanisms implement are the "Policies" referred
to in "Policy based routing".

One of the constraints I have if I try to back port this work to
6.x is that it must be implemented as a EXTENSION to the existing
ABIs in 6.x so that third party applications do not need to be
recompiled in timespan of the branch.

This first version will not have some of the bells and whistles that
will come with later versions. It will, for example, be limited to 16
tables in the first commit.
Implementation method, Compatible version. (part 1)
-------------------------------
For this reason I have implemented a "sufficient subset" of a
multiple routing table solution in Perforce, and back-ported it
to 6.x. (also in Perforce though not always caught up with what I
have done in -current/P4). The subset allows a number of FIBs
to be defined at compile time (8 is sufficient for my purposes in 6.x)
and implements the changes needed to allow IPV4 to use them. I have not
done the changes for ipv6 simply because I do not need it, and I do not
have enough knowledge of ipv6 (e.g. neighbor discovery) needed to do it.

Other protocol families are left untouched and should there be
users with proprietary protocol families, they should continue to work
and be oblivious to the existence of the extra FIBs.

To understand how this is done, one must know that the current FIB
code starts everything off with a single dimensional array of
pointers to FIB head structures (One per protocol family), each of
which in turn points to the trie of routes available to that family.

The basic change in the ABI compatible version of the change is to
extent that array to be a 2 dimensional array, so that
instead of protocol family X looking at rt_tables[X] for the
table it needs, it looks at rt_tables[Y][X] when for all
protocol families except ipv4 Y is always 0.
Code that is unaware of the change always just sees the first row
of the table, which of course looks just like the one dimensional
array that existed before.

The entry points rtrequest(), rtalloc(), rtalloc1(), rtalloc_ign()
are all maintained, but refer only to the first row of the array,
so that existing callers in proprietary protocols can continue to
do the "right thing".
Some new entry points are added, for the exclusive use of ipv4 code
called in_rtrequest(), in_rtalloc(), in_rtalloc1() and in_rtalloc_ign(),
which have an extra argument which refers the code to the correct row.

In addition, there are some new entry points (currently called
rtalloc_fib() and friends) that check the Address family being
looked up and call either rtalloc() (and friends) if the protocol
is not IPv4 forcing the action to row 0 or to the appropriate row
if it IS IPv4 (and that info is available). These are for calling
from code that is not specific to any particular protocol. The way
these are implemented would change in the non ABI preserving code
to be added later.

One feature of the first version of the code is that for ipv4,
the interface routes show up automatically on all the FIBs, so
that no matter what FIB you select you always have the basic
direct attached hosts available to you. (rtinit() does this
automatically).

You CAN delete an interface route from one FIB should you want
to but by default it's there. ARP information is also available
in each FIB. It's assumed that the same machine would have the
same MAC address, regardless of which FIB you are using to get
to it.

This brings us as to how the correct FIB is selected for an outgoing
IPV4 packet.

Firstly, all packets have a FIB associated with them. if nothing
has been done to change it, it will be FIB 0. The FIB is changed
in the following ways.

Packets fall into one of a number of classes.

1/ locally generated packets, coming from a socket/PCB.
Such packets select a FIB from a number associated with the
socket/PCB. This in turn is inherited from the process,
but can be changed by a socket option. The process in turn
inherits it on fork. I have written a utility call setfib
that acts a bit like nice..

setfib -3 ping target.example.com # will use fib 3 for ping.

It is an obvious extension to make it a property of a jail
but I have not done so. It can be achieved by combining the setfib and
jail commands.

2/ packets received on an interface for forwarding.
By default these packets would use table 0,
(or possibly a number settable in a sysctl(not yet)).
but prior to routing the firewall can inspect them (see below).
(possibly in the future you may be able to associate a FIB
with packets received on an interface.. An ifconfig arg, but not yet.)

3/ packets inspected by a packet classifier, which can arbitrarily
associate a fib with it on a packet by packet basis.
A fib assigned to a packet by a packet classifier
(such as ipfw) would over-ride a fib associated by
a more default source. (such as cases 1 or 2).

4/ a tcp listen socket associated with a fib will generate
accept sockets that are associated with that same fib.

5/ Packets generated in response to some other packet (e.g. reset
or icmp packets). These should use the FIB associated with the
packet being reponded to.

6/ Packets generated during encapsulation.
gif, tun and other tunnel interfaces will encapsulate using the FIB
that was in effect withthe proces that set up the tunnel.
thus setfib 1 ifconfig gif0 [tunnel instructions]
will set the fib for the tunnel to use to be fib 1.

Routing messages would be associated with their
process, and thus select one FIB or another.
messages from the kernel would be associated with the fib they
refer to and would only be received by a routing socket associated
with that fib. (not yet implemented)

In addition Netstat has been edited to be able to cope with the
fact that the array is now 2 dimensional. (It looks in system
memory using libkvm (!)). Old versions of netstat see only the first FIB.

In addition two sysctls are added to give:
a) the number of FIBs compiled in (active)
b) the default FIB of the calling process.

Early testing experience:
-------------------------

Basically our (IronPort's) appliance does this functionality already
using ipfw fwd but that method has some drawbacks.

For example,
It can't fully simulate a routing table because it can't influence the
socket's choice of local address when a connect() is done.

Testing during the generating of these changes has been
remarkably smooth so far. Multiple tables have co-existed
with no notable side effects, and packets have been routes
accordingly.

ipfw has grown 2 new keywords:

setfib N ip from anay to any
count ip from any to any fib N

In pf there seems to be a requirement to be able to give symbolic names to the
fibs but I do not have that capacity. I am not sure if it is required.

SCTP has interestingly enough built in support for this, called VRFs
in Cisco parlance. it will be interesting to see how that handles it
when it suddenly actually does something.

Where to next:
--------------------

After committing the ABI compatible version and MFCing it, I'd
like to proceed in a forward direction in -current. this will
result in some roto-tilling in the routing code.

Firstly: the current code's idea of having a separate tree per
protocol family, all of the same format, and pointed to by the
1 dimensional array is a bit silly. Especially when one considers that
there is code that makes assumptions about every protocol having the
same internal structures there. Some protocols don't WANT that
sort of structure. (for example the whole idea of a netmask is foreign
to appletalk). This needs to be made opaque to the external code.

My suggested first change is to add routing method pointers to the
'domain' structure, along with information pointing the data.
instead of having an array of pointers to uniform structures,
there would be an array pointing to the 'domain' structures
for each protocol address domain (protocol family),
and the methods this reached would be called. The methods would have
an argument that gives FIB number, but the protocol would be free
to ignore it.

When the ABI can be changed it raises the possibilty of the
addition of a fib entry into the "struct route". Currently,
the structure contains the sockaddr of the desination, and the resulting
fib entry. To make this work fully, one could add a fib number
so that given an address and a fib, one can find the third element, the
fib entry.

Interaction with the ARP layer/ LL layer would need to be
revisited as well. Qing Li has been working on this already.

This work was sponsored by Ironport Systems/Cisco

Reviewed by: several including rwatson, bz and mlair (parts each)
Obtained from: Ironport systems/Cisco


# 176626 27-Feb-2008 dwmalone

Dummynet has a limit of 100 slots queue size (or 1MB, if you give
the limit in bytes) hard coded into both the kernel and userland.
Make both these limits a sysctl, so it is easy to change the limit.
If the userland part of ipfw finds that the sysctls don't exist,
it will just fall back to the traditional limits.

(100 packets is quite a small limit these days. If you want to test
TCP at 100Mbps, 100 packets can only accommodate a DBP of 12ms.)

Note these sysctls in the man page and warn against increasing them
without thinking first.

MFC after: 3 weeks


# 176517 24-Feb-2008 piso

Add table/tablearg support to ipfw's nat.

MFC After: 1 week


# 176445 21-Feb-2008 piso

-Fix display of nat range.
-Whitespace elimination.

Bug spotted by: Luiz Otavio O Souza
MFC After: 3 days


# 176393 18-Feb-2008 piso

Fix display of multiple nat rules.

Bug spotted by: Luiz Otavio O Souza
PR: 120734
MFC After: 3 days


# 176391 18-Feb-2008 julian

Instead of using a heuristic to decide whether to display
table 'values' as IP addresses, use an explicit argument (-i).
This is a 'POLA' issue. This is a low risk change and should be MFC'd
to RELENG_6 and RELENG 7. it might be put as an errata item for 6.3.
(not sure about 6.2).

Fix suggested by: Eugene Grosbein
PR: 120720
MFC After: 3 days


# 175659 25-Jan-2008 rwatson

Hide ipfw internal data structures behind IPFW_INTERNAL rather than
exposing them to all consumers of ip_fw.h. These structures are
used in both ipfw(8) and ipfw(4), but not part of the user<->kernel
interface for other applications to use, rather, shared
implementation.

MFC after: 3 days
Reported by: Paul Vixie <paul at vix dot com>


# 175511 20-Jan-2008 maxim

o Fix ipfw(8) command line parser bug: "ipfw nat 1 config if" requires an argument.

PR: bin/119815
Submitted by: Dierk Sacher
MFC after: 1 week


# 174713 17-Dec-2007 oleg

Calculate p.fs.lookup_step correctly. This should prevent zeroing of
w_q_lookup table (used in RED algorithm for (1 - w_q)^t computation).

MFC after: 1 months


# 173080 27-Oct-2007 maxim

o Fix indentation. No functional changes.


# 172801 19-Oct-2007 rpaulo

Comply with the removal of IPTOS_CE and IPTOS_ECT.
Discussed on freebsd-net with no objections.

Approved by: njl (mentor), rwatson


# 172306 23-Sep-2007 maxim

o Cosmetic: fix the issue when "ipfw(8) show" produces "not" twice:

$ ipfw -n add 1 allow layer2 not mac-type ip
00001 allow ip from any to any layer2 not not mac-type 0x0800

PR: bin/115372
Submitted by: Andrey V. Elsukov
Approved by: re (hrs)
MFC after: 3 weeks


# 171989 26-Aug-2007 maxim

o Fix bug I introduced in the previous commit (ipfw set extention):
pack a set number correctly.

Submitted by: oleg

o Plug a memory leak.

Submitted by: oleg and Andrey V. Elsukov
Approved by: re (kensmith)
MFC after: 1 week


# 170923 18-Jun-2007 maxim

o Make ipfw set more robust -- now it is possible:
- to show a specific set: ipfw set 3 show
- to delete rules from the set: ipfw set 9 delete 100 200 300
- to flush the set: ipfw set 4 flush
- to reset rules counters in the set: ipfw set 1 zero

PR: kern/113388
Submitted by: Andrey V. Elsukov
Approved by: re (kensmith)
MFC after: 6 weeks


# 169424 09-May-2007 maxim

o Teach get_mac_addr_mask() to not silently accept incorrect MAC
addresses.
o Swap a couple of magic 6s by ETHER_ADDR_LEN.

PR: bin/80913
Submitted by: Andrey V. Elsukov
MFC after: 1 month


# 169245 04-May-2007 bz

Add support for filtering on Routing Header Type 0 and
Mobile IPv6 Routing Header Type 2 in addition to filter
on the non-differentiated presence of any Routing Header.

MFC after: 3 weeks


# 169139 30-Apr-2007 maxim

o Make ipfw(8) show rules with mac/mac-type options correctly.

Before:

$ ipfw -n add 100 count icmp from any to any mac-type 0x01
00100 count icmp 0x0001
$ ipfw -n add 100 count icmp from any to any mac any any
00100 count icmp MAC any any any

After:

$ ipfw -n add 100 count icmp from any to any mac-type 0x01
00100 count icmp from any to any mac-type 0x0001
$ ipfw -n add 100 count icmp from any to any mac any any
00100 count icmp from any to any MAC any any

PR: bin/112244
Submitted by: Andrey V. Elsukov
MFC after: 1 month


# 168819 17-Apr-2007 maxim

o Add missed w/space in the error message.

Spotted by: Ivan Voras
MFC after: 1 week


# 165851 07-Jan-2007 mlaier

Fix a parsing bug when specifying more than one address with dotted decimal
netmask.

Reported by: Igor Anishchuk
PR: kern/107565
MFC after: 3 days


# 165648 29-Dec-2006 piso

Summer of Code 2005: improve libalias - part 2 of 2

With the second (and last) part of my previous Summer of Code work, we get:

-ipfw's in kernel nat

-redirect_* and LSNAT support

General information about nat syntax and some examples are available
in the ipfw (8) man page. The redirect and LSNAT syntax are identical
to natd, so please refer to natd (8) man page.

To enable in kernel nat in rc.conf, two options were added:

o firewall_nat_enable: equivalent to natd_enable

o firewall_nat_interface: equivalent to natd_interface

Remember to set net.inet.ip.fw.one_pass to 0, if you want the packet
to continue being checked by the firewall ruleset after being
(de)aliased.

NOTA BENE: due to some problems with libalias architecture, in kernel
nat won't work with TSO enabled nic, thus you have to disable TSO via
ifconfig (ifconfig foo0 -tso).

Approved by: glebius (mentor)


# 162773 29-Sep-2006 maxim

o Check for a required "pathname" argument presence.

PR: bin/95146
Submitted by: candy-sendpr@kgc.co.jp
MFC after: 3 weeks


# 162363 16-Sep-2006 jhay

Check the length of the ipv4 and ipv6 address lists. It must be less
than F_LEN_MASK.

MFC after: 5 days


# 162344 16-Sep-2006 jhay

Use bzero() to clear the whole ipfw_insn_icmp6 structure in fill_icmp6types(),
otherwise this command

ipfw add allow ipv6-icmp from any to 2002::1 icmp6types 1,2,128,129

turns into icmp6types 1,2,32,33,34,...94,95,128,129

PR: 102422 (part 1)
Submitted by: Andrey V. Elsukov <bu7cher at yandex.ru>
MFC after: 5 days


# 161550 23-Aug-2006 dwmalone

A pipe bandwidth of 10MBits/s should probably
be understood as 10Mbits/s not 10MBytes/s.

Submitted by: Gavin McCullagh <gavin.mccullagh@nuim.ie>
MFC after: 1 week


# 161483 20-Aug-2006 dwmalone

Regigle parens to try and get the intended affect. This should fix people
having trouble with the "me6" keyword. Also, we were using inet_pton on
the wrong variable in one place.

Reviewed by: mlaier (previous version of patch)
Obtained from: Sascha Blank (inet_pton change)
MFC after: 1 week


# 161456 18-Aug-2006 julian

comply with style police

Submitted by: ru
MFC after: 1 month


# 161424 17-Aug-2006 julian

Allow ipfw to forward to a destination that is specified by a table.
for example:
fwd tablearg ip from any to table(1)
where table 1 has entries of the form:
1.1.1.0/24 10.2.3.4
208.23.2.0/24 router2

This allows trivial implementation of a secondary routing table implemented
in the firewall layer.

I expect more work (under discussion with Glebius) to follow this to clean
up some of the messy parts of ipfw related to tables.

Reviewed by: Glebius
MFC after: 1 month


# 161001 05-Aug-2006 stefanf

Use the SLIST_NEXT macro instead of sle_next.

Checked with: cmp(1)


# 159636 15-Jun-2006 oleg

Add support of 'tablearg' feature for:
- 'tag' & 'untag' action parameters.
- 'tagged' & 'limit' rule options.
Rule examples:
pipe 1 tag tablearg ip from table(1) to any
allow ip from any to table(2) tagged tablearg
allow tcp from table(3) to any 25 setup limit src-addr tablearg

sbin/ipfw/ipfw2.c:
1) new macros
GET_UINT_ARG - support of 'tablearg' keyword, argument range checking.
PRINT_UINT_ARG - support of 'tablearg' keyword.
2) strtoport(): do not silently truncate/accept invalid port list expressions
like: '1,2-abc' or '1,2-3-4' or '1,2-3x4'. style(9) cleanup.

Approved by: glebius (mentor)
MFC after: 1 month


# 159160 02-Jun-2006 mlaier

Print dynamic rules for IPv6 as well.

PR: bin/98349
Submitted by: Mark Andrews
MFC after: 2 weeks


# 158879 24-May-2006 oleg

Implement internal (i.e. inside kernel) packet tagging using mbuf_tags(9).
Since tags are kept while packet resides in kernelspace, it's possible to
use other kernel facilities (like netgraph nodes) for altering those tags.

Submitted by: Andrey Elsukov <bu7cher at yandex dot ru>
Submitted by: Vadim Goncharov <vadimnuclight at tpu dot ru>
Approved by: glebius (mentor)
Idea from: OpenBSD PF
MFC after: 1 month


# 158553 14-May-2006 mlaier

For src/dest parsing take off the netmask before checking for AF with
inet_pton. This fixes cases like "fe02::/16".

PR: bin/91245
Reported by: Fredrik Lindberge


# 157335 31-Mar-2006 julian

Amazing.. two screwups in one commit.
I'm piling on thise pointy hats on top of each other.
At least they nest..


# 157332 31-Mar-2006 julian

I can't believe that no-one noticed that I broke ipfw table del
for over a month!
put {} around if clause with multiple statements


# 156315 05-Mar-2006 ume

Revert `proto ip' back to the previous behavior. The kernel side of
ipfw2 doesn't allow zero as protocol number.

MFC after: 3 days


# 155640 14-Feb-2006 julian

oops, mismerge from working sources.. not only add new code,
but remove old code!


# 155639 14-Feb-2006 julian

Stop ipfw from aborting when asked to delete a table entry that
doesn't exist or add one that is already present, if the -q flag
is set. Useful for "ipfw -q /dev/stdin" when the command above is
invoked from something like python or TCL to feed commands
down the throat of ipfw.
MFC in: 1 week


# 153374 13-Dec-2005 glebius

Add a new feature for optimizining ipfw rulesets - substitution of the
action argument with the value obtained from table lookup. The feature
is now applicable only to "pipe", "queue", "divert", "tee", "netgraph"
and "ngtee" rules.

An example usage:

ipfw pipe 1000 config bw 1000Kbyte/s
ipfw pipe 4000 config bw 4000Kbyte/s
ipfw table 1 add x.x.x.x 1000
ipfw table 1 add x.x.x.y 4000
ipfw pipe tablearg ip from table(1) to any

In the example above the rule will throw different packets to different pipes.

TODO:
- Support "skipto" action, but without searching all rules.
- Improve parser, so that it warns about bad rules. These are:
- "tablearg" argument to action, but no "table" in the rule. All
traffic will be blocked.
- "tablearg" argument to action, but "table" searches for entry with
a specific value. All traffic will be blocked.
- "tablearg" argument to action, and two "table" looks - for src and
for dst. The last lookup will match.


# 153266 09-Dec-2005 glebius

Cleanup _FreeBSD_version.


# 152923 29-Nov-2005 ume

We couldn't specify the rule for filtering tunnel traffic since an
IPv6 support was committed:

- Stop treating `ip' and `ipv6' as special in `proto' option as they
conflict with /etc/protocols.

- Disuse `ipv4' in `proto' option as it is corresponding to `ipv6'.

- When protocol is specified as numeric, treat it as it is even it is
41 (ipv6).

- Allow zero for protocol as it is valid number of `ip'.

Still, we cannot specify an IPv6 over an IPv4 tunnel like before such
as:

pass ipv6 from any to any

But, now, you can specify it like:

pass ip4 from any to any proto ipv6

PR: kern/89472
Reported by: Ga l Roualland <gael.roualland__at__dial.oleane.com>
MFC after: 1 week


# 152921 29-Nov-2005 glebius

Catch up with ip_dummynet.h rev. 1.38 and fix build.


# 152917 29-Nov-2005 glebius

Garbage-collect now unused struct _ipfw_insn_pipe and flush_pipe_ptrs(),
thus removing a few XXXes.
Document the ABI breakage in UPDATING.


# 149020 13-Aug-2005 bz

* Add dynamic sysctl for net.inet6.ip6.fw.
* Correct handling of IPv6 Extension Headers.
* Add unreach6 code.
* Add logging for IPv6.

Submitted by: sysctl handling derived from patch from ume needed for ip6fw
Obtained from: is_icmp6_query and send_reject6 derived from similar
functions of netinet6,ip6fw
Reviewed by: ume, gnn; silence on ipfw@
Test setup provided by: CK Software GmbH
MFC after: 6 days


# 147105 07-Jun-2005 mlaier

add_proto() now fills proto for us so stop to 'guess' the protocol from the
command and rather trust the value add_proto filled in. While here, fix an
oversight in the pretty printing of ip6/4 options.


# 146894 02-Jun-2005 mlaier

Add support for IPv4 only rules to IPFW2 now that it supports IPv6 as well.
This is the last requirement before we can retire ip6fw.

Reviewed by: dwhite, brooks(earlier version)
Submitted by: dwhite (manpage)
Silence from: -ipfw


# 146464 21-May-2005 mlaier

Unbreak handling of "ip[v]6" protocol and option flag. No more segfaults
and not every protocol is IPv6.


# 145567 26-Apr-2005 brooks

Fix a the previous commit. I wanted to remove the if and always run the
body not remove both.

Reported by: ceri
Pointy hat: brooks


# 145566 26-Apr-2005 brooks

Don't force IPv6 proto to be printed numericaly.

Noticed by: ceri


# 145246 18-Apr-2005 brooks

Add IPv6 support to IPFW and Dummynet.

Submitted by: Mariano Tortoriello and Raffaele De Lorenzo (via luigi)


# 144687 05-Apr-2005 brooks

Be more specific when complaining about bit masks.


# 141351 05-Feb-2005 glebius

Add a ng_ipfw node, implementing a quick and simple interface between
ipfw(4) and netgraph(4) facilities.

Reviewed by: andre, brooks, julian


# 140423 18-Jan-2005 glebius

Don't print extra " via ", if we have already printed one. While here,
slightly style brackets.

PR: misc/75297
MFC after: 1 week


# 140271 14-Jan-2005 brooks

Deprecate unmaintainable uses of strncmp to implement abbreviations.
This commit replaces those with two new functions that simplify the code
and produce warnings that the syntax is deprecated. A small number of
sensible abbreviations may be explicitly added based on user feedback.

There were previously three types of strncmp use in ipfw:
- Most commonly, strncmp(av, "string", sizeof(av)) was used to allow av
to match string or any shortened form of it. I have replaced this
with a new function _substrcmp(av, "string") which returns 0 if av
is a substring of "string", but emits a warning if av is not exactly
"string".

- The next type was two instances of strncmp(av, "by", 2) which allowed
the abbreviation of bytes to "by", "byt", etc. Unfortunately, it
also supported "bykHUygh&*g&*7*ui". I added a second new function
_substrcmp2(av, "by", "bytes") which acts like the strncmp did, but
complains if the user doesn't spell out the word "bytes".

- There is also one correct use of strncmp to match "table(" which might
have another token after it without a space.

Since I changed all the lines anyway, I also fixed the treatment of
strncmp's return as a boolean in many cases. I also modified a few
strcmp cases as well to be fully consistent.


# 139821 06-Jan-2005 brooks

Write some bit mask limits in hex rather than decimal so they look less
magic.


# 138072 24-Nov-2004 brooks

Remove a duplicate line from an apparent merge error in rev 1.63.


# 136247 08-Oct-2004 green

Commit forgotten documentation for "diverted" rules.


# 136075 02-Oct-2004 green

Add support to IPFW for matching by TCP data length.


# 136073 02-Oct-2004 green

Add support to IPFW for classification based on "diverted" status
(that is, input via a divert socket).


# 136072 02-Oct-2004 green

Remove accidentally-added O_DIVERTED section.


# 136071 02-Oct-2004 green

Add to IPFW the ability to do ALTQ classification/tagging.


# 135554 21-Sep-2004 csjp

Since "d" is an array of 32 bit values, it is more
correct to change the cast from unsigned int to uint32_t.

Pointed out by: luigi


# 135089 11-Sep-2004 csjp

Currently when ipfw(8) generates the micro-instructions for rules which
contain O_UID, O_GID and O_JAIL opcodes, the F_NOT or F_OR logical
operator bits get clobbered. Making it impossible to use the ``NOT'' or
``OR'' operators with uid, gid and jail based constraints.

The ipfw_insn instruction template contains a ``len'' element which
stores two pieces of information, the size of the instruction
(in 32-bit words) in the low 6 bits of "len" with the 2 remaining
bits to implement OR and NOT.

The current code clobbers the OR and NOT bits by initializing the
``len'' element to the size, rather than OR'ing the bits. This change
fixes this by changing the initialization of cmd->len to an OR operation
for the O_UID, O_GID and O_JAIL opcodes.

This may be a MFC candidate for RELENG_5.

Reviewed by: andre
Approved by: luigi
PR: kern/63961 (partially)


# 135036 10-Sep-2004 maxim

o Initialize a local variable and make gcc happy.

PR: bin/71485
Submitted by: Jukka A. Ukkonen


# 134475 29-Aug-2004 maxim

o Restore a historical ipfw1 logamount behaviour: rules with 'log'
keyword but without 'logamount' limit the amount of their log messages
by net.inet.ip.fw.verbose_limit sysctl value.

RELENG_5 candidate.

PR: kern/46080
Submitted by: Dan Pelleg
MFC after: 1 week


# 134225 23-Aug-2004 pjd

Fix 'show' command for pipes and queues.

PR: bin/70311
Submitted by: Pawel Malachowski <pawmal-posting@freebsd.lublin.pl>
MFC after: 3 days


# 133600 12-Aug-2004 csjp

Add the ability to associate ipfw rules with a specific prison ID.
Since the only thing truly unique about a prison is it's ID, I figured
this would be the most granular way of handling this.

This commit makes the following changes:

- Adds tokenizing and parsing for the ``jail'' command line option
to the ipfw(8) userspace utility.
- Append the ipfw opcode list with O_JAIL.
- While Iam here, add a comment informing others that if they
want to add additional opcodes, they should append them to the end
of the list to avoid ABI breakage.
- Add ``fw_prid'' to the ipfw ucred cache structure.
- When initializing ucred cache, if the process is jailed,
set fw_prid to the prison ID, otherwise set it to -1.
- Update man page to reflect these changes.

This change was a strong motivator behind the ucred caching
mechanism in ipfw.

A sample usage of this new functionality could be:

ipfw add count ip from any to any jail 2

It should be noted that because ucred based constraints
are only implemented for TCP and UDP packets, the same
applies for jail associations.

Conceptual head nod by: pjd
Reviewed by: rwatson
Approved by: bmilekic (mentor)


# 133387 09-Aug-2004 andre

New ipfw option "antispoof":

For incoming packets, the packet's source address is checked if it
belongs to a directly connected network. If the network is directly
connected, then the interface the packet came on in is compared to
the interface the network is connected to. When incoming interface
and directly connected interface are not the same, the packet does
not match.

Usage example:

ipfw add deny ip from any to any not antispoof in

Manpage education by: ru


# 130298 10-Jun-2004 ru

Fixed a bug spotted by compiling with -Wall.


# 130281 09-Jun-2004 ru

Introduce a new feature to IPFW2: lookup tables. These are useful
for handling large sparse address sets. Initial implementation by
Vsevolod Lobko <seva@ip.net.ua>, refined by me.

MFC after: 1 week


# 130013 02-Jun-2004 csjp

o Move NEED1 macro to the top of the source file.

o Add sanity checking to the firewall delete operation
which tells the user that a firewall rule
specification is required.

The previous behaviour was to exit without reporting any
errors to the user.

Approved by: bmilekic (mentor)


# 129389 18-May-2004 stefanf

Remove spurious semicolons.

Approved by: das (mentor)
Reviewed by: ipfw@


# 129058 08-May-2004 csjp

Remove redundant sanity check before add_mac() when adding
mac ipfw rules. The exact same sanity check is performed as
the first operation of add_mac(), so there is no sense
in doing it twice.

Approved by: bmilekic (mentor)
PR: bin/55981


# 128575 23-Apr-2004 andre

Add the option versrcreach to verify that a valid route to the
source address of a packet exists in the routing table. The
default route is ignored because it would match everything and
render the check pointless.

This option is very useful for routers with a complete view of
the Internet (BGP) in the routing table to reject packets with
spoofed or unrouteable source addresses.

Example:

ipfw add 1000 deny ip from any to any not versrcreach

also known in Cisco-speak as:

ip verify unicast source reachable-via any

Reviewed by: luigi


# 128067 09-Apr-2004 maxim

o Fix an incorrect parsing of 0.0.0.0/0 expression.

PR: kern/64778
MFC after: 6 weeks


# 124924 24-Jan-2004 maxim

o Pass a correct argument to errx(3).

PR: bin/61846
Submitted by: Eugene Grosbein
MFC after: 1 week


# 123804 24-Dec-2003 maxim

o Legitimate -f (force) flags for -p (preprocessor) case.

PR: bin/60433
Submitted: Bjoern A. Zeeb
MFC after: 3 weeks


# 123495 12-Dec-2003 luigi

Add a -b flag to /sbin/ipfw to print only action and comment for each
rule, thus omitting the entire body.
This makes the output a lot more readable for complex rulesets
(provided, of course, you have annotated your ruleset appropriately!)

MFC after: 3 days


# 121816 31-Oct-2003 brooks

Replace the if_name and if_unit members of struct ifnet with new members
if_xname, if_dname, and if_dunit. if_xname is the name of the interface
and if_dname/unit are the driver name and instance.

This change paves the way for interface renaming and enhanced pseudo
device creation and configuration symantics.

Approved By: re (in principle)
Reviewed By: njl, imp
Tested On: i386, amd64, sparc64
Obtained From: NetBSD (if_xname)


# 120715 03-Oct-2003 sam

remove include of route.h now that ip_dummynet.h no longer exposes
data structures that have an embedded struct route

Sponsored by: FreeBSD Foundation


# 119740 04-Sep-2003 tmm

Apply a bandaid to get this working on sparc64 again; the introduction
of do_cmd() broke things, because this function assumes that a socklen_t
is large enough to hold a pointer.
A real solution to this problem would be a rewrite of do_cmd() to
treat the optlen parameter consistently and not use it to carry
a pointer or integer dependent on the context.


# 119668 02-Sep-2003 maxim

Check an arguments count before proceed in sysctl_handler().

PR: bin/56298
Submitted by: Kang Liu <liukang@bjpu.edu.cn>
MFC after: 2 weeks

# We need a regression test suit for ipfw(2)/ipfw(8) badly.


# 117821 21-Jul-2003 maxim

o Initialize do_pipe before command parsing.

PR: bin/54649
Submitted by: Andy Gilligan <andy@evo6.org>
MFC after: 3 days


# 117655 15-Jul-2003 luigi

Userland side of:
Allow set 31 to be used for rules other than 65535.
Set 31 is still special because rules belonging to it are not deleted
by the "ipfw flush" command, but must be deleted explicitly with
"ipfw delete set 31" or by individual rule numbers.

This implement a flexible form of "persistent rules" which you might
want to have available even after an "ipfw flush".
Note that this change does not violate POLA, because you could not
use set 31 in a ruleset before this change.

Suggested by: Paul Richards


# 117626 15-Jul-2003 luigi

Make sure that comments are printed at the end of a rule.

Reported by: Patrick Tracanelli <eksffa@freebsdbrasil.com.br>


# 117577 14-Jul-2003 luigi

Fix one typo in help() string, remove whitespace at end of line and
other minor whitespace changes.

Replace u_char with uint8_t in a few places.


# 117544 14-Jul-2003 luigi

ccept of empty lines when reading from a file (this fixes a bug
introduced in the latest commits).

Also:

* update the 'ipfw -h' output;

* allow rules of the form "100 add allow ..." i.e. with the index first.
(requested by Paul Richards). This was an undocumented ipfw1 behaviour,
and it is left undocumented.

and minor code cleanups.


# 117472 12-Jul-2003 luigi

Add a '-T' flag to print the timestamp as numeric value instead
of converting it with ctime(). This is a lot more convenient for
postprocessing.

Submitted by: "Jacob S. Barrett" <jbarrett@amduat.net>


# 117469 12-Jul-2003 luigi

In random order:

* make the code compile with WARNS=5 (at least on i386), mostly
by adding 'const' specifier and replacing "void *" with "char *"
in places where pointer arithmetic was used.
This also spotted a few places where invalid tests (e.g. uint < 0)
were used.

* support ranges in "list" and "show" commands. Now you can say

ipfw show 100-1000 4000-8000

which is very convenient when you have large rulesets.

* implement comments in ipfw commands. These are implemented in the
kernel as O_NOP commands (which always match) whose body contains
the comment string. In userland, a comment is a C++-style comment:

ipfw add allow ip from me to any // i can talk to everybody

The choice of '//' versus '#' is somewhat arbitrary, but because
the preprocessor/readfile part of ipfw used to strip away '#',
I did not want to change this behaviour.

If a rule only contains a comment

ipfw add 1000 // this rule is just a comment

then it is stored as a 'count' rule (this is also to remind
the user that scanning through a rule is expensive).

* improve handling of flags (still to be completed).
ipfw_main() was written thinking of 'one rule per ipfw invocation',
and so flags are set and never cleared. With readfile/preprocessor
support, this changes and certain flags should be reset on each
line. For the time being, only fix handling of '-a' which
differentiates the "list" and "show" commands.

* rework the preprocessor support -- ipfw_main() already had most
of the parsing code, so i have moved in there the only missing
bit (stripping away '#' and comments) and removed the parsing
from ipfw_readfile().
Also, add some more options (such as -c, -N, -S) to the readfile
section.

MFC after: 3 days


# 117328 08-Jul-2003 luigi

A bunch of changes (mostly syntactic sugar, all backward compatible):

* Make the addr-set size optional (defaults to /24)
You can now write 1.2.3.0/24{56-80} or 1.2.3.0{56-80}
Also make the parser more strict.

* Support a new format for the list of addresses:
1.2.3.4,5.6.7.8/30,9.10.11.12/22,12.12.12.13, ...
which exploits the new capabilities of O_IP_SRC_MASK/O_IP_DST_MASK

* Allow spaces after commas to make lists of addresses more readable.
1.2.3.4, 5.6.7.8/30, 9.10.11.12/22, 12.12.12.13, ...

* ipfw will now accept full commands as a single argument and strip
extra leading/trailing whitespace as below:
ipfw "-q add allow ip from 1.2.3.4 to 5.6.7.8, 9.10.11.23 "
This should help in moving the body of ipfw into a library
that user programs can invoke.

* Cleanup some comments and data structures.

* Do not print rule counters for dynamic rules with ipfw -d list
(PR 51182)

* Improve 'ipfw -h' output (PR 46785)

* Add a '-n' flag to test the syntax of commands without actually
calling [gs]etsockopt() (PR 44238)

* Support the '-n' flag also with the preprocessors;

Manpage commit to follow.

MFC after: 3 days


# 117241 04-Jul-2003 luigi

Implement the 'ipsec' option to match packets coming out of an ipsec tunnel.
Should work with both regular and fast ipsec (mutually exclusive).
See manpage for more details.

Submitted by: Ari Suutari (ari.suutari@syncrontech.com)
Revised by: sam
MFC after: 1 week


# 116919 27-Jun-2003 luigi

remove extra whitespace and blank lines


# 116770 23-Jun-2003 luigi

Split some long lines to fit 80 columns (the code in RELENG_4
was already correct).


# 116716 23-Jun-2003 luigi

syntactic sugar: support range notation such as
1.2.3.4/24{5,6,7,10-20,60-90}
for set of ip addresses.
Previously you needed to specify every address in the range, which
was unconvenient and lead to very long lines.
Internally the set is still stored in the same way, just the
input and output routines are modified.

Manpage update still missing.

Perhaps a similar preprocessing step would be useful for port ranges.

MFC after: 3 days


# 116690 22-Jun-2003 luigi

Add support for multiple values and ranges for the "iplen", "ipttl",
"ipid" options. This feature has been requested by several users.
On passing, fix some minor bugs in the parser. This change is fully
backward compatible so if you have an old /sbin/ipfw and a new
kernel you are not in trouble (but you need to update /sbin/ipfw
if you want to use the new features).

Document the changes in the manpage.

Now you can write things like

ipfw add skipto 1000 iplen 0-500

which some people were asking to give preferential treatment to
short packets.

The 'MFC after' is just set as a reminder, because I still need
to merge the Alpha/Sparc64 fixes for ipfw2 (which unfortunately
change the size of certain kernel structures; not that it matters
a lot since ipfw2 is entirely optional and not the default...)

PR: bin/48015

MFC after: 1 week


# 116438 16-Jun-2003 maxim

o Pass a correct argument to printf(3).

PR: bin/51750
Submitted by: Vasil Dimov <vd@datamax.bg>
MFC after: 2 weeks


# 115793 03-Jun-2003 ticso

Change handling to support strong alignment architectures such as alpha and
sparc64.

PR: alpha/50658
Submitted by: rizzo
Tested on: alpha


# 112250 14-Mar-2003 cjc

Add a 'verrevpath' option that verifies the interface that a packet
comes in on is the same interface that we would route out of to get to
the packet's source address. Essentially automates an anti-spoofing
check using the information in the routing table.

Experimental. The usage and rule format for the feature may still be
subject to change.


# 112189 13-Mar-2003 maxim

o Partially revert rev. 1.103, fix 'ipfw show': dynamically adjust a
width of fields for packets and bytes counters.

PR: bin/47196
Reviewed by: -audit
Not objected by: luigi, des

o Use %llu instead of deprecated %qu convert specification for ipfw
packets and bytes counters.

Noted by: des
MFC after: 1 month


# 109126 12-Jan-2003 dillon

It turns out that we do not need to add a new ioctl to unbreak a
default-to-deny firewall. Simply turning off IPFW via a preexisting
sysctl does the job. To make it more apparent (since nobody picked up
on this in a week's worth of flames), the boolean sysctl's have been
integrated into the /sbin/ipfw command set in an obvious and straightforward
manner. For example, you can now do 'ipfw disable firewall' or
'ipfw enable firewall'. This is far easier to remember then the
net.inet.ip.fw.enable sysctl.

Reviewed by: imp
MFC after: 3 days


# 108533 01-Jan-2003 schweikh

Correct typos, mostly s/ a / an / where appropriate. Some whitespace cleanup,
especially in troff files.


# 108231 23-Dec-2002 kbyanc

Make preprocessor support more generic by passing all command-line options
after -p except for the last (the ruleset file to process) to the
preprocessor for interpretation. This allows command-line options besides
-U and -D to be passed to cpp(1) and m4(1) as well as making it easier to
use other preprocessors.

Sponsored By: NTT Multimedia Communications Labs
MFC after: 1 week


# 107291 26-Nov-2002 keramida

Align timestamps when -t is used in ipfw and ipfw2.

PR: kern/44843
Approved by: re (jhb)


# 107289 26-Nov-2002 luigi

Fix a kernel panic with rules of the type

prob 0.5 pipe NN ....

due to the generation of an invalid ipfw instruction sequence.
No ABI change, but you need to upgrade /sbin/ipfw to generate the
correct code.

Approved by: re


# 106505 06-Nov-2002 maxim

Kill EOL whitespaces, style(9) fix.


# 106504 06-Nov-2002 maxim

Fix UID/GID options parsing.

PR: bin/42579
Submitted by: Belousov Oleg <oleg@belousov.com>
Approved by: luigi
MFC after: 2 weeks


# 105887 24-Oct-2002 mux

Fix ipfw2 panics on 64-bit platforms.

Quoting luigi:

In order to make the userland code fully 64-bit clean it may
be necessary to commit other changes that may or may not cause
a minor change in the ABI.

Reviewed by: luigi


# 103241 11-Sep-2002 luigi

Store the port number in "fwd" rules in host format, same as ipfw1
has always done.

Technically, this is the wrong format, but it reduces the diffs in
-stable. Someday, when we get rid of ipfw1, I will put the port number
in the proper format both in kernel and userland.

MFC after: 3 days
(with re@ permission)


# 102098 19-Aug-2002 luigi

One more (hopefully the last one) step in cleaning up the syntax,
following Julian's good suggestion: since you can specify any match
pattern as an option, rules now have the following format:

[<proto> from <src> to <dst>] [options]

i.e. the first part is now entirely optional (and left there just
for compatibility with ipfw1 rulesets).

Add a "-c" flag to show/list rules in the compact form
(i.e. without the "ip from any to any" part) when possible.
The default is to include it so that scripts processing ipfw's
canonical output will still work.
Note that as part of this cleanup (and to remove ambiguity), MAC
fields now can only be specified in the options part.

Update the manpage to reflect the syntax.

Clarify the behaviour when a match is attempted on fields which
are not present in the packet, e.g. port numbers on non TCP/UDP
packets, and the "not" operator is specified. E.g.

ipfw add allow not src-port 80

will match also ICMP packets because they do not have port numbers, so
"src-port 80" will fail and "not src-port 80" will succeed. For such
cases it is advised to insert further options to prevent undesired results
(e.g. in the case above, "ipfw add allow proto tcp not src-port 80").

We definitely need to rewrite the parser using lex and yacc!


# 102087 19-Aug-2002 luigi

Major cleanup of the parser and printing routines in an attempt to
render the syntax less ambiguous.

Now rules can be in one of these two forms

<action> <protocol> from <src> to <dst> [options]
<action> MAC dst-mac src-mac mac-type [options]

however you can now specify MAC and IP header fields as options e.g.

ipfw add allow all from any to any mac-type arp
ipfw add allow all from any to any { dst-ip me or src-ip me }

which makes complex expressions a lot easier to write and parse.
The "all from any to any" part is there just for backward compatibility.

Manpage updated accordingly.


# 101978 16-Aug-2002 luigi

sys/netinet/ip_fw2.c:

Implement the M_SKIP_FIREWALL bit in m_flags to avoid loops
for firewall-generated packets (the constant has to go in sys/mbuf.h).

Better comments on keepalive generation, and enforce dyn_rst_lifetime
and dyn_fin_lifetime to be less than dyn_keepalive_period.

Enforce limits (up to 64k) on the number of dynamic buckets, and
retry allocation with smaller sizes.

Raise default number of dynamic rules to 4096.

Improved handling of set of rules -- now you can atomically
enable/disable multiple sets, move rules from one set to another,
and swap sets.

sbin/ipfw/ipfw2.c:

userland support for "noerror" pipe attribute.

userland support for sets of rules.

minor improvements on rule parsing and printing.

sbin/ipfw/ipfw.8:

more documentation on ipfw2 extensions, differences from ipfw1
(so we can use the same manpage for both), stateful rules,
and some additional examples.
Feedback and more examples needed here.


# 101641 10-Aug-2002 luigi

Fix one parsing bug introduced by last commit, and correct parsing
and printing of or-blocks in address, ports and options lists.


# 101628 10-Aug-2002 luigi

One bugfix and one new feature.

The bugfix (ipfw2.c) makes the handling of port numbers with
a dash in the name, e.g. ftp-data, consistent with old ipfw:
use \\ before the - to consider it as part of the name and not
a range separator.

The new feature (all this description will go in the manpage):

each rule now belongs to one of 32 different sets, which can
be optionally specified in the following form:

ipfw add 100 set 23 allow ip from any to any

If "set N" is not specified, the rule belongs to set 0.

Individual sets can be disabled, enabled, and deleted with the commands:

ipfw disable set N
ipfw enable set N
ipfw delete set N

Enabling/disabling of a set is atomic. Rules belonging to a disabled
set are skipped during packet matching, and they are not listed
unless you use the '-S' flag in the show/list commands.
Note that dynamic rules, once created, are always active until
they expire or their parent rule is deleted.
Set 31 is reserved for the default rule and cannot be disabled.

All sets are enabled by default. The enable/disable status of the sets
can be shown with the command

ipfw show sets

Hopefully, this feature will make life easier to those who want to
have atomic ruleset addition/deletion/tests. Examples:

To add a set of rules atomically:

ipfw disable set 18
ipfw add ... set 18 ... # repeat as needed
ipfw enable set 18

To delete a set of rules atomically

ipfw disable set 18
ipfw delete set 18
ipfw enable set 18

To test a ruleset and disable it and regain control if something
goes wrong:

ipfw disable set 18
ipfw add ... set 18 ... # repeat as needed
ipfw enable set 18 ; echo "done "; sleep 30 && ipfw disable set 18

here if everything goes well, you press control-C before
the "sleep" terminates, and your ruleset will be left
active. Otherwise, e.g. if you cannot access your box,
the ruleset will be disabled after the sleep terminates.

I think there is only one more thing that one might want, namely
a command to assign all rules in set X to set Y, so one can
test a ruleset using the above mechanisms, and once it is
considered acceptable, make it part of an existing ruleset.


# 101295 04-Aug-2002 luigi

Fix generation of check-state rules, which i broke in last commit.


# 101117 31-Jul-2002 luigi

Forgot this one: properly initialize an address set when the set
size is less than 32 bits (/28 mask or more).
Also remove a debugging fprintf().


# 101116 31-Jul-2002 luigi

Two bugfixes:
+ the header file contains two different opcodes (O_IPOPTS and O_IPOPT)
for what is the same thing, and sure enough i used one in the kernel
and the other one in userland. Be consistent!

+ "keep-state" and "limit" must be the last match pattern in a rule,
so no matter how you enter them move them to the end of the rule.


# 99909 13-Jul-2002 luigi

A bunch of minor fixes:

* accept "icmptype" as an alias for "icmptypes";
* remove an extra whitespace after "log" rules;
* print correctly the "limit" masks;
* correct a typo in parsing dummynet arguments (this caused a coredump);
* do not allow specifying both "check-state" and "limit", they are
(and have always been) mutually exclusive;
* remove an extra print of the rule before installing it;
* make stdout buffered -- otherwise, if you log its output with syslog,
you will see one entry for each printf(). Rather unpleasant.


# 99603 08-Jul-2002 bde

Fixed some world breakage caused by not updating clients when <timeconv.h>
was split off from <time.h>. This became fatal here when -Werror was
reenabled.


# 99475 05-Jul-2002 luigi

Implement the last 2-3 missing instructions for ipfw,
now it should support all the instructions of the old ipfw.

Fix some bugs in the user interface, /sbin/ipfw.

Please check this code against your rulesets, so i can fix the
remaining bugs (if any, i think they will be mostly in /sbin/ipfw).

Once we have done a bit of testing, this code is ready to be MFC'ed,
together with a bunch of other changes (glue to ipfw, and also the
removal of some global variables) which have been in -current for
a couple of weeks now.

MFC after: 7 days


# 98943 27-Jun-2002 luigi

The new ipfw code.

This code makes use of variable-size kernel representation of rules
(exactly the same concept of BPF instructions, as used in the BSDI's
firewall), which makes firewall operation a lot faster, and the
code more readable and easier to extend and debug.

The interface with the rest of the system is unchanged, as witnessed
by this commit. The only extra kernel files that I am touching
are if_fw.h and ip_dummynet.c, which is quite tied to ipfw. In
userland I only had to touch those programs which manipulate the
internal representation of firewall rules).

The code is almost entirely new (and I believe I have written the
vast majority of those sections which were taken from the former
ip_fw.c), so rather than modifying the old ip_fw.c I decided to
create a new file, sys/netinet/ip_fw2.c . Same for the user
interface, which is in sbin/ipfw/ipfw2.c (it still compiles to
/sbin/ipfw). The old files are still there, and will be removed
in due time.

I have not renamed the header file because it would have required
touching a one-line change to a number of kernel files.

In terms of user interface, the new "ipfw" is supposed to accepts
the old syntax for ipfw rules (and produce the same output with
"ipfw show". Only a couple of the old options (out of some 30 of
them) has not been implemented, but they will be soon.

On the other hand, the new code has some very powerful extensions.
First, you can put "or" connectives between match fields (and soon
also between options), and write things like

ipfw add allow ip from { 1.2.3.4/27 or 5.6.7.8/30 } 10-23,25,1024-3000 to any

This should make rulesets slightly more compact (and lines longer!),
by condensing 2 or more of the old rules into single ones.

Also, as an example of how easy the rules can be extended, I have
implemented an 'address set' match pattern, where you can specify
an IP address in a format like this:

10.20.30.0/26{18,44,33,22,9}

which will match the set of hosts listed in braces belonging to the
subnet 10.20.30.0/26 . The match is done using a bitmap, so it is
essentially a constant time operation requiring a handful of CPU
instructions (and a very small amount of memmory -- for a full /24
subnet, the instruction only consumes 40 bytes).

Again, in this commit I have focused on functionality and tried
to minimize changes to the other parts of the system. Some performance
improvement can be achieved with minor changes to the interface of
ip_fw_chk_t. This will be done later when this code is settled.

The code is meant to compile unmodified on RELENG_4 (once the
PACKET_TAG_* changes have been merged), for this reason
you will see #ifdef __FreeBSD_version in a couple of places.
This should minimize errors when (hopefully soon) it will be time
to do the MFC.