home *** CD-ROM | disk | FTP | other *** search
/ ftp.jcu.edu.au / 2014.06.ftp.jcu.edu.au.tar / ftp.jcu.edu.au / v6.3.2b / SWBD63 / fabos-6.3.2b-10.ppc.rpm / fabos-6.3.2b.10.cpio.gz / fabos-6.3.2b.10.cpio / fabos / libexec / createipf < prev    next >
Text File  |  2010-11-10  |  12KB  |  446 lines

  1. #!/bin/sh
  2. #
  3. # Usage: createipf.sh
  4. # Rule are defined as follows.
  5.  
  6. #    ----------         -------
  7. #    | INPUT  |-------->| TCP |------> Other protocol rules
  8. #    ----------         -------
  9. #        |
  10. #        |              -------
  11. #        -------------->| UDP |------> Other protocol rules
  12. #                       -------
  13. #
  14. # splitting rule processing in two levels makes it faster
  15. # to execute.
  16.  
  17. export PATH=/fabos/sbin:/fabos/bin:/bin:/usr/bin:/sbin
  18.  
  19. # update version whenever script is changed
  20.  
  21. # IPTABLES variable is not defined, hence will be null
  22. # this variable is kept if script is changed to use a 
  23. # path for iptables,  this variable can be set to proper
  24. # location
  25.  
  26. export debug_on=0
  27.  
  28. if [ $# != 2 ]; then
  29.     echo "ERROR: $0, Incorrect number of parameters."
  30.     #Not required.
  31.     #iptab_clear_rules;
  32.     exit 1
  33. fi
  34.  
  35. export POLICYTYPE=$1
  36. export CPSTATE=$2
  37.  
  38. version=v1.1
  39.  
  40. #NEED TO CREATE IP6TABLES ALSO.
  41. iptab_clear_rules()
  42. {
  43.         
  44.     if [ "$POLICYTYPE" = "v4" ]; then
  45.         iptables -P INPUT ACCEPT
  46.         iptables -P OUTPUT ACCEPT
  47.         iptables -P FORWARD ACCEPT
  48.  
  49.         iptables -F
  50.         iptables -X
  51.     elif [ "$POLICYTYPE" = "v6" ]; then
  52.         ip6tables -P INPUT ACCEPT
  53.         ip6tables -P OUTPUT ACCEPT
  54.         ip6tables -P FORWARD ACCEPT
  55.  
  56.         ip6tables -F
  57.         ip6tables -X
  58.     else
  59.         iptables -P INPUT ACCEPT
  60.         iptables -P OUTPUT ACCEPT
  61.         iptables -P FORWARD ACCEPT
  62.  
  63.         iptables -F
  64.         iptables -X
  65.         
  66.         ip6tables -P INPUT ACCEPT
  67.         ip6tables -P OUTPUT ACCEPT
  68.         ip6tables -P FORWARD ACCEPT
  69.  
  70.         ip6tables -F
  71.         ip6tables -X
  72.     fi
  73. }
  74.  
  75. function valid_ip()
  76. {
  77.     local  ip=$1
  78.     local  stat=1
  79.     IP_ADDR_VAL=$(echo "$ip" | grep -Ec '^(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])')
  80.     if ! [ $IP_ADDR_VAL -eq 0 ]; then
  81.         stat=0    
  82.     fi
  83.  
  84.     return $stat
  85. }
  86.  
  87. #USER DEFINED PARAMETERS
  88.  
  89. export ERRFILE="/dev/null"
  90. export INTERNAL_IFACE=eth1
  91. export INTERNAL_IFACE_BP=eth2
  92. export INTERNAL_IFACE_1250=inbd+
  93. export DCE_IFACE="veth+"
  94. export DCE_VLAN_IFACE="vlan+"
  95. export FC="fc"
  96. export SCRDIR="/fabos/libexec"
  97. export PRIV_NET=10.0.0.0/28 
  98. export PRIV_NET_BP=127.1.0.0/16 
  99. export INTERNAL_IP1=10.0.0.5
  100. export INTERNAL_IP2=10.0.0.6
  101.  
  102. export RULE_FILE=/tmp/netfilter.rules
  103. export PROTO_TCP=tcp
  104. export PROTO_UDP=udp
  105. export PROTO_ICMP=icmp
  106.  
  107. export R_TCP=tcp0
  108. export R_UDP=udp0
  109. export R_ICMP=icmp0
  110. export R_TELNET=telnet0
  111. export R_SSH=ssh0
  112. export R_HTTP=http0
  113. export R_APIRPCUDP=apirpcudp0
  114. export R_APIRPCTCP=apirpctcp0
  115. export R_APIPMAP=apipmap0
  116. export R_TCPCOMMON=tcpcommon0
  117. export R_UDPCOMMON=udpcommon0
  118.  
  119. export R_PRIV_NET=priv_net_rule
  120.  
  121. rule_chains="tcp0 udp0 telnet0 ssh0 http0 apirpctcp0 apirpcudp0 apipmap0 tcpcommon0 udpcommon0"
  122.  
  123. CREATECHAINS="createipfchains"
  124.  
  125. # Determine the system platform identifier.
  126. SWBD=`/sbin/sin | sed -n -e 's/^.\+\(SWBD\)\([[:digit:]]\{1,\}\).\+$/\2/gp' 2> /dev/null`
  127.  
  128. # Determine the state-sync transport based on the platform identifier.
  129. if [ $SWBD = 62 -o $SWBD = 77 ]; then
  130.     export INET_IFACE="bond0"
  131. else
  132.     export INET_IFACE="eth0"
  133. fi
  134.  
  135. echo_debug()
  136. {
  137.         echo $* 1>&2
  138. }
  139.  
  140. iptab_create_header()
  141. {
  142.     # Start generating rules file
  143.  
  144.     echo "# Generated by createiptab $version" > $RULE_FILE
  145.     echo "*filter" >> $RULE_FILE
  146.     echo ":INPUT DROP [0:0]" >> $RULE_FILE
  147.     echo ":FORWARD ACCEPT [0:0]" >> $RULE_FILE
  148.     echo ":OUTPUT ACCEPT [0:0]" >> $RULE_FILE
  149. }
  150.  
  151.  
  152.  
  153. iptab_create_rule_chains()
  154. {
  155.     for rule_item in $rule_chains
  156.     do 
  157.         echo ":$rule_item - [0:0]" >> $RULE_FILE
  158.     done
  159. }
  160.  
  161.  
  162. iptab_priv_net_create_rule_chain()
  163. {
  164.     echo ":$R_PRIV_NET - [0:0]" >> $RULE_FILE
  165. }
  166.  
  167.  
  168. # get switch type, term/Ulysses and cp type for Ulysses
  169. iptab_get_cp_association()
  170. {
  171.  
  172.     # Change the names to CPRUL and change it to ACTIVE, STANDBY and PIZZABO
  173.     if [ "$ischassis" = "Yes" ] && [ "$CPSTATE" = "Standby" ]; then
  174.         ACTIVECP=0    # Dual chassis stanby CP
  175.     elif [ "$ischassis" = "Yes" ]; then
  176.         ACTIVECP=1    # Dual chassis active CP 
  177.     else
  178.         ACTIVECP=2    # Single Chassis box 
  179.     fi
  180.  
  181.     export ACTIVECP
  182. }
  183.  
  184.  
  185.  
  186. iptab_priv_net_put_chain_rules()
  187. {
  188.     # DO check for Active and pizza box
  189.     if [ "$ACTIVECP" = "0" ]; then
  190.         echo "$IPTABLES -A OUTPUT -o $INTERNAL_IFACE -j $R_PRIV_NET"
  191.         echo "$IPTABLES -A OUTPUT -o $INTERNAL_IFACE_BP -j $R_PRIV_NET"
  192.         echo "$IPTABLES -A $R_PRIV_NET -s $PRIV_NET -d $PRIV_NET -j ACCEPT"
  193.         echo "$IPTABLES -A $R_PRIV_NET -s $PRIV_NET_BP -d $PRIV_NET_BP -j ACCEPT"
  194.         echo "$IPTABLES -A $R_PRIV_NET -j REJECT"
  195.     fi
  196.     if [ "$ACTIVECP" = "2" ]; then
  197.         #
  198.         # setup BP interface only on systems where it is available
  199.         #
  200.         /sbin/ifconfig $INTERNAL_IFACE_BP 1>$ERRFILE 2>$ERRFILE
  201.         if [ $? -eq 0 ]; then
  202.             echo "$IPTABLES -A OUTPUT -o $INTERNAL_IFACE_BP -j $R_PRIV_NET"
  203.             echo "$IPTABLES -A $R_PRIV_NET -s $PRIV_NET_BP -d $PRIV_NET_BP -j ACCEPT"
  204.             echo "$IPTABLES -A $R_PRIV_NET -j REJECT"
  205.         fi
  206.     fi
  207. }
  208.  
  209.  
  210. iptab_create_common_rules()
  211. {
  212.  
  213.     declare -a ipfcarr
  214.     ipfcarr=( `/fabos/cliexec/ipaddripfcshow`)                #  Loads contents
  215.     element_count=${#ipfcarr[*]}
  216.  
  217.     # default policy or all, eth1 should always be allowed because
  218.     # two CPs talk on this. Terminator does not need eth1 rule.
  219.  
  220.     # These two rules should not be removed at all, as this rule redirects
  221.     # all incoming packets to tcp and udp chains.   
  222.     echo "$IPTABLES -A INPUT -i $INET_IFACE -p $PROTO_TCP -j $R_TCP"
  223.     echo "$IPTABLES -A INPUT -i $INET_IFACE -p $PROTO_UDP -j $R_UDP"
  224.  
  225.     if [ "$POLICYTYPE" = "v4" ]; then
  226.         if [ "$CPSTATE" = "Active" ]; then
  227.         #       detaching FC IP address FC interface, and no specific interface is specified. 
  228.         #       defect # 25011 - because of ARP cache corruption, this association can not be
  229.         #       enforced,. ARP cache is corrupted because of Linux behaviour where it does
  230.         #       not support two physical interfaces on same subnet.
  231.  
  232.             for (( i=0; i<${element_count}; i++ ));
  233.             do
  234.                 IP=`echo ${ipfcarr[$i]} | cut -d / -f 1`
  235.                 NETMASK=`echo ${ipfcarr[$i]} | cut -d / -f 2`
  236.                 if ! [ -z $(echo $NETMASK | sed -e 's/[0-9]//g') ] || [ "$NETMASK" = "" ]; then
  237.                     ipfcarr[$i]=$IP
  238.                 fi
  239.  
  240.                 if valid_ip  ${ipfcarr[$i]}; then
  241.                     echo "$IPTABLES -A INPUT -p $PROTO_TCP -d ${ipfcarr[$i]} -j ACCEPT"
  242.                     echo "$IPTABLES -A INPUT -p $PROTO_UDP -d ${ipfcarr[$i]} -j ACCEPT"
  243.                 fi 
  244.             done
  245.         fi
  246.  
  247.         echo "$IPTABLES -A INPUT -i $INET_IFACE -p $PROTO_ICMP -m $PROTO_ICMP -d $INTERNAL_IP1 --icmp-type 8 -j DROP"
  248.         echo "$IPTABLES -A INPUT -i $INET_IFACE -p $PROTO_ICMP -m $PROTO_ICMP -d $INTERNAL_IP2 --icmp-type 8 -j DROP"
  249.         if [ "$ACTIVECP" == "2" ]; then
  250.             echo "$IPTABLES -A INPUT -i $INTERNAL_IFACE_1250 -p $PROTO_ICMP -m $PROTO_ICMP -d $INTERNAL_IP1 --icmp-type 8 -j DROP"
  251.             echo "$IPTABLES -A INPUT -i $INTERNAL_IFACE_1250 -p $PROTO_ICMP -m $PROTO_ICMP -d $INTERNAL_IP2 --icmp-type 8 -j DROP"
  252.         fi
  253.         # Explain about the ping request and responses which ping and try to find out
  254.         echo "$IPTABLES -A INPUT -p $PROTO_ICMP -m $PROTO_ICMP --icmp-type 0 -j ACCEPT"
  255.         echo "$IPTABLES -A INPUT -p $PROTO_ICMP -m $PROTO_ICMP --icmp-type 8 -j ACCEPT"
  256.         # Defect 221041, allow fragmentation-needed ICMP packets so we can tunnel management traffic
  257.         echo "$IPTABLES -A INPUT -p $PROTO_ICMP --icmp-type fragmentation-needed -j ACCEPT"
  258.     elif [ "$POLICYTYPE" = "v6" ]; then
  259.         # echo "$IPTABLES -A INPUT -p icmpv6 -j LOG"
  260.         # Future work: find about exact icmp types
  261.         echo "$IPTABLES -A INPUT -p icmpv6 -j ACCEPT"
  262.     fi
  263.     
  264.     #
  265.     # setup BP interface only on systems where it is available
  266.     #
  267.  
  268.     if [ "$POLICYTYPE" = "v4" ]; then
  269.         if /sbin/ifconfig $INTERNAL_IFACE_BP 1>$ERRFILE 2>$ERRFILE; then
  270.             echo "$IPTABLES -A INPUT -i $INTERNAL_IFACE_BP -j ACCEPT"
  271.             echo "$IPTABLES -A FORWARD -o $INTERNAL_IFACE_BP -j REJECT"
  272.             echo "$IPTABLES -A FORWARD -i $INTERNAL_IFACE_BP -j REJECT"
  273.         fi
  274.     fi
  275.  
  276. #
  277. # setup to always receive anything on DCE interfaces
  278. #
  279.         echo "$IPTABLES -A INPUT -i $DCE_IFACE -j ACCEPT" >> $RULE_FILE
  280.         echo "$IPTABLES -A INPUT -i $DCE_VLAN_IFACE -j ACCEPT" >> $RULE_FILE
  281.  
  282.     if [ "$ACTIVECP" != "2" ]; then
  283.         # set up forwarding rules, no forwarding from/to eth1
  284.         echo "$IPTABLES -A INPUT -i $INTERNAL_IFACE -j ACCEPT"
  285.         echo "$IPTABLES -A FORWARD -o $INTERNAL_IFACE -j REJECT"
  286.         echo "$IPTABLES -A FORWARD -i $INTERNAL_IFACE -j REJECT"
  287.  
  288.     #    echo "$IPTABLES -A INPUT -s $PRIV_NET -j REJECT"
  289.     #    echo "$IPTABLES -A INPUT -d $PRIV_NET -j REJECT"
  290.     fi
  291.  
  292.     echo "$IPTABLES -A INPUT -i lo -j ACCEPT"
  293.  
  294.     if [ "$ACTIVECP" == "2" ]; then # should not be chassis
  295.         #
  296.         # set up inbd rule for INPUT, inbd0 or inbd1 interface will not be up yet
  297.         #
  298.         echo "$IPTABLES -A INPUT -i $INTERNAL_IFACE_1250 -p $PROTO_TCP -j $R_TCP"
  299.         echo "$IPTABLES -A INPUT -i $INTERNAL_IFACE_1250 -p $PROTO_UDP -j $R_UDP"
  300.     fi
  301.  
  302.     #
  303.     # allow AH, ESP and IKE packets
  304.     #
  305.     echo "$IPTABLES -A INPUT -p udp --dport 500 --j ACCEPT"
  306.     echo "$IPTABLES -A INPUT -p esp -j ACCEPT"
  307.     echo "$IPTABLES -A INPUT -p ah -j ACCEPT"
  308.     echo "$IPTABLES -A OUTPUT -p udp --dport 500 --j ACCEPT"
  309.     echo "$IPTABLES -A OUTPUT -p esp -j ACCEPT"
  310.     echo "$IPTABLES -A OUTPUT -p ah  -j ACCEPT"
  311.  
  312.     # this is necessary so clients do not hang waiting for response.
  313.     # this was discovered with firmwaredownload
  314.     echo "$IPTABLES -A INPUT -j REJECT"
  315.  
  316.     # This is a policy to drop all incoming packets which will not hit.
  317.     echo "$IPTABLES -P INPUT DROP"
  318. } # iptab_create_common_rules
  319.  
  320. iptab_nat_create_v4_rules()
  321. {
  322. # Creating rules for nat table
  323. HOSTNAME=`hostname`
  324. HOSTFILE=/etc/hosts
  325. IP_ETH0=`sed -n -e 's/ .*'$HOSTNAME'.*//gp' $HOSTFILE`
  326. IP_TABLES=/sbin/iptables
  327.  
  328.     if [ "$ACTIVECP" = "2" ]; then
  329.         $IP_TABLES -t nat -vL 1>$ERRFILE 2>$ERRFILE
  330.         if [ $? -eq 0 ]; then
  331.             $IP_TABLES -t nat -A POSTROUTING -o $INTERNAL_IFACE_1250 -j SNAT --to $IP_ETH0
  332.         fi
  333.     fi
  334. } # iptab_nat_create_v4_rules
  335.  
  336. #///////////////////////////////
  337. ################################
  338. # start of this script
  339. ################################
  340. #///////////////////////////////
  341. POLICYFILE="/etc/fabos/ipfpolicy."$POLICYTYPE"."$CPSTATE".txt"
  342. POLICYFILE_OLD="/tmp/old_ipfpolicy."$POLICYTYPE"."$CPSTATE".txt"
  343.  
  344. NEW_IPFCFILE="/etc/fabos/ipfc.txt"
  345. OLD_IPFCFILE="/tmp/oldipfc.txt"
  346. OLD_4RULESFILE="/tmp/oldip4rule.txt"
  347. OLD_6RULESFILE="/tmp/oldip6rule.txt"
  348.  
  349.  
  350. # check if text policy file exists. If not, exit.
  351. if [ ! -f $POLICYFILE ]; then
  352.     exit 1
  353. fi
  354.  
  355. /fabos/cliexec/ipaddripfcshow >>$NEW_IPFCFILE
  356.  
  357. if [ -f $POLICYFILE_OLD ]; then
  358.    HASH_NEW=`/usr/bin/md5sum $POLICYFILE`
  359.    HASH_NEW=${HASH_NEW%% *}
  360.    HASH_OLD=`/usr/bin/md5sum $POLICYFILE_OLD`
  361.    HASH_OLD=${HASH_OLD%% *}
  362.    if [ "$HASH_NEW" = "$HASH_OLD" ]; then
  363.  
  364.     if [ ! -f $OLD_IPFCFILE ]; then
  365.         exit 0    
  366.     fi    
  367.     IPFCHASH_NEW=`/usr/bin/md5sum $NEW_IPFCFILE`
  368.     IPFCHASH_NEW=${IPFCHASH_NEW%% *}
  369.     IPFCHASH_OLD=`/usr/bin/md5sum $OLD_IPFCFILE`
  370.     IPFCHASH_OLD=${IPFCHASH_OLD%% *}
  371.     if [ "$IPFCHASH_NEW" = "$IPFCHASH_OLD" ]; then
  372.         exit 0
  373.     fi
  374.    fi
  375. fi
  376.  
  377. chassis_info=`getchassisconfig`
  378.  
  379. export ischassis=`echo $chassis_info | sed -n -e 's/.*Chassis based system: //p' | \
  380.     sed -n -e 's/ .*//p'`
  381.  
  382. iptab_create_header;
  383.  
  384. # get switch type, term/Ulysses and cp type for Ulysses
  385. iptab_get_cp_association;
  386.  
  387. # create rule chain names
  388. iptab_create_rule_chains ;
  389.  
  390. #SAGAR:Required only for ipv4 Stack
  391. if [ "$POLICYTYPE"    = "v4" ]; then
  392.     iptab_priv_net_create_rule_chain;
  393. fi
  394.  
  395. $SCRDIR/$CREATECHAINS $POLICYTYPE $CPSTATE;
  396.  
  397. if [ "$?" != "0" ]; then
  398.     iptab_clear_rules;
  399.     exit 1
  400. fi
  401.  
  402. if [ "$POLICYTYPE" = "v4" ]; then
  403.     iptab_priv_net_put_chain_rules >> $RULE_FILE
  404. fi
  405.  
  406. iptab_create_common_rules >> $RULE_FILE
  407.  
  408. echo "COMMIT" >> $RULE_FILE
  409. echo "# Completed" >> $RULE_FILE
  410.  
  411. if [ "$POLICYTYPE"    = "v4" ]; then
  412.     /sbin/iptables-restore < $RULE_FILE 1> /dev/null 2>&1
  413. else
  414.     /sbin/ip6tables-restore < $RULE_FILE 1> /dev/null 2>&1
  415. fi
  416.  
  417. if [ $? != 0 ]; then
  418.     iptab_clear_rules;
  419.     echo "ERROR: Failed to enforce new iptables rules"
  420.     if [ -f $RULE_FILE ]; then
  421.         ERRFILE="/tmp/oldrules.`/bin/date +\"%s\"`.txt"
  422.         if [ "$POLICYTYPE"    = "v4" ]; then
  423.             /sbin/iptables-restore < $RULE_FILE >> $ERRFILE 
  424.             /bin/mv $RULE_FILE $OLD_4RULESFILE
  425.         else
  426.              /sbin/ip6tables-restore < $RULE_FILE >> $ERRFILE
  427.             /bin/mv $RULE_FILE $OLD_6RULESFILE
  428.         fi
  429.     fi
  430.     exit 1
  431. fi
  432.  
  433.  
  434. /bin/mv $POLICYFILE $POLICYFILE_OLD
  435. /bin/mv $NEW_IPFCFILE $OLD_IPFCFILE
  436.  
  437. # add NAT table rule(s) for inbd interface
  438. if [ "$POLICYTYPE" = "v4" ]; then
  439.     iptab_nat_create_v4_rules;
  440. fi
  441.  
  442. # add forwarding rule, let it be commented, can be uncommented when needed
  443. # if uncommented, FORWARD rule also must be changed to ACCEPT
  444. # echo "1" > /proc/sys/net/ipv4/ip_forward
  445.