# IPFW and IPv6 wrong syntax



## minimike (Sep 15, 2011)

Hi there,

Just for understanding could anybody correct my wrong rules for IPv6 with IPFW on FreeBSD 8 stable?


```
ipfw -f add 23 allow ip6 tcp from any to fe80::210:18ff:fe33:6bb8 22 in
ipfw -f add 24 allow ip6 tcp from fe80::210:18ff:fe33:6bb8 to any 22 out
```

(the addresses are faked about security reasons)


----------



## francis (Sep 18, 2011)

Hi, I think in this rule should be *ipv6* instead of *ip6*. I will give you another example, for a rule which allow all *IPv6* packets through the firewall.

```
ipfw -f add 23 allow [B][color="Red"]ipv6[/color][/B] tcp from any to fe80::210:18ff:fe33:6bb8 22 in
```

If you still have problems, please read the perfect description for ipfw's manual and of course ipfw(8). You can also try for example apply some sysctl options; _net.inet6.ip6.fw.enable: 1_ which is responsible for _"enables the firewall.  Setting this variable to 0 lets you run your machine without firewall even if compiled in. Also provides the same functionality ... for the IPv6 case"._ Best regards!

__________________
According to the *RusDyr* allegations described below; probably you're right, I do not use ipfw firewall for a very long time, so I should not give any advice on this topic. Best regards!


----------



## RusDyr (Nov 7, 2011)

Very strange idea to give hints, that's not approved or tested by himself, don't you think so?
_ipv6_ and _ip6_ directives are both allowed, there is no difference *at all*.
What's really the problem is setting IP protocol and IPv4 protocol together. 
So while ipfw developers don't change this (I agree with you, it's QUITE STRANGE behavior), you could you instead of yours 

Incorrect IPFW:

```
ipfw -f add 23 allow ip6 tcp from any to fe80::210:18ff:fe33:6bb8 22 in
ipfw -f add 24 allow ip6 tcp from fe80::210:18ff:fe33:6bb8 to any 22 out
```
this one:
Correct IPFW:

```
ipfw -f add 23 allow tcp from any to fe80::210:18ff:fe33:6bb8 22 in
ipfw -f add 24 allow tcp from fe80::210:18ff:fe33:6bb8 to any 22 out
```

It will work.


----------



## RusDyr (Nov 7, 2011)

After long and thoroughly reading "man ipfw" I found best solution.  
Forget old format, it's obsolete and is only for backward compatibility:


> The rule body has the following format:
> 
> [proto from src to dst] [options]
> 
> ...



So all you need is setting your protocol options *AFTER* your rule, this way:
New format IPFW:

```
ipfw -f add 23 allow ip6 from any to fe80::210:18ff:fe33:6bb8 22 in proto tcp
ipfw -f add 24 allow ip6 from fe80::210:18ff:fe33:6bb8 to any 22 out proto tcp
```

Or even:

```
ipfw -f add 23 allow ip6 from any to any proto tcp dst-ip6 fe80::210:18ff:fe33:6bb8 dst-port 22 in
ipfw -f add 24 allow ip6 from any to any proto tcp src-ip6 fe80::210:18ff:fe33:6bb8 src-port 22 out
```

Enjoy!


----------



## minimike (Nov 21, 2011)

Thanks guys. That has helped me so much. Currently I'm writing all Rules for my Servers


----------



## RusDyr (Nov 21, 2011)

Push "Thanks" button. )


----------



## ensō (May 24, 2017)

Though it's not displayed in the example below I've tried your solution but it doesn't seem to work on v11? Help me out here guys what am I doing wrong? I only want to be able to use SSH over IPV6. Thanks

```
#!/bin/sh


# Flush all rules before we begin.

ipfw -q -f flush


# Set rules command prefix

cmd="ipfw -q add "


vif="vtnet0"


# allow all for localhost

$cmd 00010 allow ip from any to any via lo0


# checks stateful rules.  If marked as "keep-state" the packet has

# already passed through filters and is "OK" without futher

# rule matching

$cmd 00101 check-state


# allow DNS out

$cmd 00110 allow tcp from me to any dst-port 53 out via $vif setup keep-state

$cmd 00111 allow udp from me to any dst-port 53 out via $vif keep-state


# allow dhclient connection out (port numbers are important)

$cmd 00120 allow udp from me 68 to any dst-port 67 out via $vif keep-state


# allow HTTP HTTPS replies

$cmd 00200 allow tcp from any to any dst-port 80 out via $vif setup keep-state

$cmd 00220 allow tcp from any to any dst-port 443 out via $vif setup keep-state


# allow outbound mail

$cmd 00230 allow tcp from any to any dst-port 25 out via $vif setup keep-state

$cmd 00231 allow tcp from any to any dst-port 465 out via $vif setup keep-state

$cmd 00232 allow tcp from any to any dst-port 587 out via $vif setup keep-state


# allow icmp re: ping, et. al. 

# comment this out to disable ping, et.al.

$cmd 00250 allow icmp from any to any out via $vif keep-state


# alllow timeserver out

$cmd 00260 allow tcp from any to any dst-port 37 out via $vif setup keep-state


# allow ntp out

$cmd 00270 allow udp from any to any dst-port 123 out via $vif keep-state


# allow outbound SSH traffic

$cmd 00280 allow tcp from any to any dst-port 22 out via $vif setup keep-state


# otherwise deny outbound packets

# outbound catchall.  

$cmd 00299 deny log ip from any to any out via $vif


# inbound rules

# deny inbound traffic to restricted addresses

$cmd 00300 deny ip from 192.168.0.0/16 to any in via $vif

$cmd 00301 deny ip from 172.16.0.0/12 to any in via $vif

$cmd 00302 deny ip from 10.0.0.0/8 to any in via $vif

$cmd 00303 deny ip from 127.0.0.0/8 to any in via $vif

$cmd 00304 deny ip from 0.0.0.0/8 to any in via $vif

$cmd 00305 deny ip from 169.254.0.0/16 to any in via $vif

$cmd 00306 deny ip from 192.0.2.0/24 to any in via $vif

$cmd 00307 deny ip from 204.152.64.0/23 to any in via $vif

$cmd 00308 deny ip from 224.0.0.0/3 to any in via $vif


# deny inbound packets on these ports

# auth 113, netbios (services) 137/138/139, hosts-nameserver 81 

$cmd 00315 deny tcp from any to any dst-port 113 in via $vif

$cmd 00320 deny tcp from any to any dst-port 137 in via $vif

$cmd 00321 deny tcp from any to any dst-port 138 in via $vif

$cmd 00322 deny tcp from any to any dst-port 139 in via $vif

$cmd 00323 deny tcp from any to any dst-port 81 in via $vif


# deny partial packets

$cmd 00330 deny ip from any to any frag in via $vif

$cmd 00332 deny tcp from any to any established in via $vif


# allowing icmp re: ping, etc.

$cmd 00310 allow icmp from any to any in via $vif


# allowing inbound mail, dhcp, http, https

$cmd 00350 allow udp from any 53 to me in via $vif

$cmd 00360 allow tcp from any 53 to me in via $vif

$cmd 00370 allow udp from any 67 to me dst-port 68 in via $vif keep-state


$cmd 00400 allow tcp from any to me dst-port 80 in via $vif setup limit src-addr 2

$cmd 00410 allow tcp from any to me dst-port 443 in via $vif setup limit src-addr 2


# SSHguard puts offender addresses in table 22. Set up the table rule

# Please note the '\(22\)' syntax, necessary since it's run as shell command

$cmd 01000 deny ip from table\(22\) to any


# allow inbound ssh, mail. PROTECTED SERVICES: numbered ABOVE sshguard blacklist range 

$cmd 56420 allow tcp from any to me6 dst-port 22 in via $vif setup limit src-addr 2

$cmd 56530 allow tcp from any to any dst-port 25 in via $vif setup keep-state

$cmd 56531 allow tcp from any to any dst-port 465 in via $vif setup keep-state

$cmd 56532 allow tcp from any to any dst-port 587 in via $vif setup keep-state


# deny everything else, and log it

# inbound catchall

$cmd 56599 deny log ip from any to any in via $vif


# ipfw built-in default, don't uncomment

# $cmd 65535 deny ip from any to any
```


----------

