Chapter 17. System Management Tools

 < Day Day Up > 

16.4. The Mac OS X Firewall

Mac OS X uses FreeBSD's ipfw2 (IP firewall, version 2) facility to control how incoming and outgoing packets are routed through the system. You use the ipfw utility to define a set of rules that controls this. The default rules allow all traffic from any location to come into your computer, using the following ipfw rule (65535 is the priority level of the rule, the lowest priority possible):

     65535 allow ip from any to any 

To define this rule yourself, you'd issue the following command:

     # ipfw add 65535 allow ip from any to any 

Mac OS X uses ipfw and several other tools to share your Internet connection and to protect your Macintosh from malicious hackers.

16.4.1. Internet Sharing

When you turn on Internet Sharing (System Preferences Sharing Internet), Mac OS X executes /usr/libexec/InternetSharing, which does quite a bit under the hood. It uses ifconfig to configure the interface, ipfw and natd to handle port redirection, bootpd to offer DHCP services to client machines, and named to handle DNS lookups. Here is how these are started, along with equivalent invocations you could run as root, if you wanted to do this all by hand. ifconfig

If you're sharing the connection over a port, such as the Ethernet port (en0 on many systems), it probably has a self-assigned IP address. Internet Sharing sets this to the first host on whatever subnet it is using (the default is

     # ifconfig en0 ipfw/natd

Mac OS X adds a firewall rule with a high priority (00010), which diverts any traffic coming into port 8668 on en1 (the AirPort adapter on many systems):

     # ipfw add 00010 divert 8668 ip from any to any via en1 

It also sets the net.inet.ip.forwarding sysctl to 1, which enables IP forwarding.

The Network Address Translation Daemon (natd) listens on this port. InternetSharing starts it with this command, where IP_ADDRESS is the IP address you want to share and INTERFACE is the network interface (both the IP address and interface here correspond to "Share your connection from" in the Internet Sharing preference pane). INTERFACE2 should be the interface you're sharing the connection to:

     # /usr/sbin/natd -alias_address IP_ADDRESS -interface INTERFACE \         -use_sockets -same_ports -unregistered_only -dynamic -clamp_mss \      -enable_natportmap -natportmap_interface INTERFACE2 

So, if your AirPort adapter (en1) was assigned the IP address, and you shared that connection to another computer plugged into your Ethernet port (en0), natd would be invoked like so:

     # /usr/sbin/natd -alias_address -interface en1 \       -use_sockets -same_ports -unregistered_only -dynamic -clamp_mss \       -enable_natportmap -natportmap_interface en0 bootpd

This is a combined BOOTP and DHCP server. You can find the DHCP configuration in Directory Services, under /config/dhcp/subnets. Here is a dump of the settings, which are created when the firewall is started, and deleted when it is stopped:

     $ nidump -r /config/dhcp /     {       "name" = ( "dhcp" );       "bootp_enabled" = ( "" );       "dhcp_enabled" = ( "en0" );       "reply_threshold_seconds" = ( "4" );       "detect_other_dhcp_server" = ( "1" );       CHILDREN = (         {           "name" = ( "subnets" );           CHILDREN = (             {               "name" = ( "192.168.2" );               "net_address" = ( "" );               "net_mask" = ( "" );               "dhcp_router" = ( "" );               "lease_max" = ( "3600" );               "client_types" = ( "dhcp" );               "dhcp_domain_name_server" = ( "" );               "net_range" = ( "", "" );               "_creator" = ( "" );             }           )         }       )     } 

To allocate addresses in a different subnet, you'd need to change each occurrence of 192.168.2 to a private subnet that conforms to the rules in RFC 1918 ( and load those into Directory Services. Then you'd need to start bootpd with this command:

     # /usr/libexec/bootpd -P named

This is the BIND (Berkeley Internet Name Domain) , which provides DNS services to client machines. When you start Internet Sharing, it creates a configuration file for named in /etc/ and runs named with that as its configuration file. Here is a trimmed-down version (comments and white space removed) of that file:

     controls { };     options {             directory "/var/named";             listen-on {;  };             forward first;             forwarders {;  };     };     zone "." IN {             type hint;             file "";     };     zone "localhost" IN {             type master;             file "";             allow-update { none; };     };     zone "" IN {             type master;             file "named.local";             allow-update { none; };     };     acl can_query {any;}; 

You'd need to create this file and run named against it if you wanted to start it by hand. Be sure to change any occurrences of 192.168.2 to the appropriate subnet if you changed the DHCP settings. The forwarders setting should be set to the name of your Mac's DNS server. Internet Sharing launches named as:

     # /usr/sbin/named -c /tmp/ -f 

For more information, see the respective manpages for these commands, as well as the Advanced Networking section of the FreeBSD Handbook (

16.4.2. The Mac OS X Firewall

When you enable the firewall (System Preferences Sharing Firewall), Mac OS X sets up the following rules to keep traffic from getting into your computer:

     02000 allow ip from any to any via lo*     02010 deny ip from to any in     02020 deny ip from any to in     02030 deny ip from to any in     02040 deny tcp from any to in     02050 allow tcp from any to any out     02060 allow tcp from any to any established     65535 allow ip from any to any 

In addition, the firewall sets up rules for any services you have enabled in the Sharing tab, such as this one, which allows SSH connections by explicitly permitting traffic on port 22:

     02070 allow tcp from any to any dst-port 22 in 

Tiger introduces some advanced capabilities (press Advanced in the Firewall preference pane to set these, as shown in Figure 16-5).

Figure 16-5. Configuring advanced firewall options Enable Firewall Logging

If you've enabled this in the advanced settings, this rule will also be in effect, which logs anything that's not permitted:

     12190 deny log tcp from any to any 

The Mac OS X firewall is configured such that only the protocols you specify are allowed through. So, if something is not checked, it won't be permitted through. Here's what you'd see in the firewall log (/var/log/ipfw.log) if you tried to FTP from to a machine ( whose firewall didn't permit it:

     Apr  3 15:30:49 brian-jepsons-powerbook-g4-15 ipfw:  12190 Deny TCP in via en1 Block UDP Traffic

This option puts a number of rules into effect, which blocks UDP (User Datagram Protocol) traffic on all but a few essential ports (noted in the # comments):

     20310 allow udp from any to any dst-port 53 in   # DNS     20320 allow udp from any to any dst-port 68 in   # bootpc     20321 allow udp from any 67 to me in             # bootps     20322 allow udp from any 5353 to me in           # Bonjour     20340 allow udp from any to any dst-port 137 in  # Samba     20350 allow udp from any to any dst-port 427 in  # SLP     20360 allow udp from any to any dst-port 631 in  # CUPS     20370 allow udp from any to any dst-port 5353 in # Bonjour     22000 allow udp from any to any dst-port 123 in  # network time     30510 allow udp from me to any out keep-state     30520 allow udp from any to any in frag     35000 deny log udp from any to any in Enable Stealth Mode

This option renders your server somewhat invisible by setting the following sysctls to 1:

     net.inet.tcp.blackhole     net.inet.udp.blackhole     net.inet.tcp.log_in_vain     net.inet.udp.log_in_vain 

This sets both the blackhole (don't reply at all to connections; act as if there's not even a server there) and log_in_vain (log all those rejections in /var/log/ipfw.log) options for UDP and TCP traffic. It also sets the following firewall rule to deny ICMP echo requests:

     20000 deny icmp from any to me in icmptypes 8 

16.4.3. Add Your Own Rules

You can add your own packet filter rules by clicking the New button on the Firewall tab. You can also add your own firewall rules using the ipfw utility, but the Firewall tab remains disabled until you reboot or clear the rules with sudo ipfw flush.

For example, you could add a custom firewall rule such as this one, which permits Telnet from only one host (

     # ipfw add 02075 allow tcp from to any dst-port 23 in 

However, if you navigated to the Firewall preferences pane, you'd get the message shown in Figure 16-6. (You'll also get this message if you're running an application that uses ipfw to its own twisted ends, such as Virtual PC.) You can get things back to normal by deleting the rule:

     # ipfw delete 02075 

Figure 16-6. You won't be able to make changes until you put things back the way you found them

You may need to quit and restart the System Preferences application before it notices that you've reset the firewall to the default rules. For more information, see the ipfw manpage.

     < Day Day Up > 

    Mac OS X Tiger for Unix Geeks
    Mac OS X Tiger for Unix Geeks
    ISBN: 0596009127
    EAN: 2147483647
    Year: 2006
    Pages: 176 © 2008-2017.
    If you may any questions please contact us: