219ae19373248adac0887b86ca08ee438e8d0447
[grml-network.git] / sbin / grml-router
1 #!/bin/sh
2 # Filename:      grml-router
3 # Purpose:       set up your box as NAT-router
4 # Authors:       grml-team (grml.org), Ulrich Dangel <schula@grml.org>, Michael Prokop <mika@grml.org>
5 # Bug-Reports:   see http://grml.org/bugs/
6 # License:       This file is licensed under the GPL v2.
7 ################################################################################
8
9 CONFIG_FILE=/etc/grml/routersetup
10 . /etc/grml/lsb-functions
11 . /etc/grml/net-functions
12 . /etc/grml/script-functions
13
14 if [ $UID != 0 ]; then
15    eerror "Error: become root before starting $0"
16    exit 100
17 fi
18
19 if ! [ -r $CONFIG_FILE ] ; then
20   eerror "$CONFIG_FILE could not be read."
21   exit 1
22 fi
23
24 . $CONFIG_FILE
25
26 if [ -z "$OUTDEV" ] ; then
27     eewarn "Outgoing device is not set in $CONFIG_FILE"
28     eewarn "Setting Outgoing device to auto"
29     OUTDEV=auto
30 fi
31
32 case "$OUTDEV" in
33     auto|default)
34     OUTDEV=$(defaultGWDev)
35     if [ -z "$OUTDEV" ] ; then
36             eerror "The outgoing device could not be determined."
37             eerror "Please adjust OUTDEV in $CONFIG_FILE"
38             exit 1
39     fi
40     ;;
41 esac
42
43 [ -n "$IPTABLES" ] || IPTABLES=/sbin/iptables
44
45 check4progs $IPTABLES || exit 1
46
47 case "$1" in
48     start)
49         einfo "Adjusting kernel variables (net.ipv4.*)"
50           sysctl -w net.ipv4.conf.all.rp_filter=1    1>/dev/null && \
51           sysctl -w net.ipv4.conf.all.log_martians=1 1>/dev/null && \
52           sysctl -w net.ipv4.ip_forward=1            1>/dev/null
53         eend $?
54         einfo "Setting up iptables rule"
55           eindent
56
57             einfo "Adding masquarade rule"
58             $IPTABLES -t nat -A POSTROUTING -o $OUTDEV -j MASQUERADE
59             eend $?
60           eoutdent
61    ;;
62
63    stop)
64        einfo "Resetting kernel variables"
65          sysctl -w net.ipv4.ip_forward=0            1>/dev/null && \
66          sysctl -w net.ipv4.conf.all.log_martians=0 1>/dev/null
67        eend $?
68
69        einfo "Removing iptables rule"
70          $IPTABLES -t nat -D POSTROUTING -o $OUTDEV -j MASQUERADE
71        eend $?
72    ;;
73
74    restart)
75         $0 stop
76         sleep 1
77         $0 start
78    ;;
79
80    info)
81     einfo "$0 - script which turns on router capabilities (NAT)"
82     einfo "Configure it via $CONFIG_FILE" ; eend 0
83    ;;
84
85    *)
86     echo "Usage: $0 {start|stop|restart|info}"
87     exit 1
88    ;;
89 esac
90
91 ## END OF FILE #################################################################
92 # vim: ft=sh expandtab ai