summaryrefslogtreecommitdiff
blob: c7026bd02cf577cff259f5d49e60fae7832bfdd9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# ----------------------------------------------------------------------------
# GIPTables Firewall v1.1 http://www.giptables.org
# Copyright (C) 2002 Adrian Pascalau <apascalau@openna.com>
# NTP module
#
# ----------------------------------------------------------------------------
# This file is part of GIPTables Firewall
#
# GIPTables Firewall is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

# ----------------------------------------------------------------------------
# About NTP

# ntp             123/udp                         # Network Time Protocol

NTP_PORT="123"

# ----------------------------------------------------------------------------
# accept_ntp_request
# Usage: accept_ntp_request chain ntp_client_ipaddr ntp_server_ipaddr
#

accept_ntp_request ()
{
    local chain=$1
    local ntp_client_ipaddr=$2
    local ntp_server_ipaddr=$3

    $IPTABLES -A $chain -p udp \
              -s $ntp_client_ipaddr --sport $NTP_PORT \
              -d $ntp_server_ipaddr --dport $NTP_PORT \
              -m state --state NEW,ESTABLISHED \
              -j ACCEPT

    $IPTABLES -A $chain -p udp \
              -s $ntp_client_ipaddr --sport $UNPRIV_PORTS \
              -d $ntp_server_ipaddr --dport $NTP_PORT \
              -m state --state NEW,ESTABLISHED \
              -j ACCEPT
    return 0
}

# ----------------------------------------------------------------------------
# accept_ntp_reply
# Usage: accept_ntp_reply chain ntp_server_ipaddr ntp_client_ipaddr
#

accept_ntp_reply ()
{
    local chain=$1
    local ntp_server_ipaddr=$2
    local ntp_client_ipaddr=$3

    $IPTABLES -A $chain -p udp \
              -s $ntp_server_ipaddr --sport $NTP_PORT \
              -d $ntp_client_ipaddr --dport $NTP_PORT \
              -m state --state ESTABLISHED \
              -j ACCEPT

    $IPTABLES -A $chain -p udp \
              -s $ntp_server_ipaddr --sport $NTP_PORT \
              -d $ntp_client_ipaddr --dport $UNPRIV_PORTS \
              -m state --state ESTABLISHED \
              -j ACCEPT

    return 0
}

# ----------------------------------------------------------------------------
# snat_ntp_request
# Usage: snat_ntp_request interface if_ipaddr ntp_client_ipaddr ntp_server_ipaddr
#

snat_ntp_request ()
{
    local interface=$1
    local if_ipaddr=$2
    local ntp_client_ipaddr=$3
    local ntp_server_ipaddr=$4

    $IPTABLES -t nat -A POSTROUTING -o $interface -p udp \
              -s $ntp_client_ipaddr --sport $NTP_PORT \
              -d $ntp_server_ipaddr --dport $NTP_PORT \
              -j SNAT --to $if_ipaddr

    $IPTABLES -t nat -A POSTROUTING -o $interface -p udp \
              -s $ntp_client_ipaddr --sport $UNPRIV_PORTS \
              -d $ntp_server_ipaddr --dport $NTP_PORT \
              -j SNAT --to $if_ipaddr
    return 0
}

# ----------------------------------------------------------------------------
# dnat_ntp_request
# Usage: dnat_ntp_request interface if_ipaddr ntp_client_ipaddr ntp_server_ipaddr
#

dnat_ntp_request ()
{
    local interface=$1
    local if_ipaddr=$2
    local ntp_client_ipaddr=$3
    local ntp_server_ipaddr=$4

    $IPTABLES -t nat -A PREROUTING -i $interface -p udp \
              -s $ntp_client_ipaddr --sport $NTP_PORT \
              -d $if_ipaddr --dport $NTP_PORT \
              -j DNAT --to $ntp_server_ipaddr

    $IPTABLES -t nat -A PREROUTING -i $interface -p udp \
              -s $ntp_client_ipaddr --sport $UNPRIV_PORTS \
              -d $if_ipaddr --dport $NTP_PORT \
              -j DNAT --to $ntp_server_ipaddr
    return 0
}

# ----------------------------------------------------------------------------
# NTP outgoing client request
#

[ "$DEBUG" = "on" ] && echo -e "# NTP outgoing client request"

# Interface 0 NTP outgoing client request

[ "$INTERFACE0_NTP_CLIENT" == "yes" ] && \
[ "$DEBUG" = "on" ] && echo -e "# Interface 0 NTP outgoing client request"

[ "$INTERFACE0_NTP_CLIENT" == "yes" ] && \
for (( index = 0; index < "${#INTERFACE0_NTP_OUT_DST_IPADDR[@]}"; index++ ))
do

    accept_ntp_request interface0_out ${INTERFACE0_NTP_OUT_SRC_IPADDR[$index]} ${INTERFACE0_NTP_OUT_DST_IPADDR[$index]}
    accept_ntp_reply    interface0_in ${INTERFACE0_NTP_OUT_DST_IPADDR[$index]} ${INTERFACE0_NTP_OUT_SRC_IPADDR[$index]}

done

# Interface 1 NTP outgoing client request

[ -n "$INTERFACE1" ] && [ "$INTERFACE1_NTP_CLIENT" == "yes" ] && \
[ "$DEBUG" = "on" ] && echo -e "# Interface 1 NTP outgoing client request"

[ -n "$INTERFACE1" ] && [ "$INTERFACE1_NTP_CLIENT" == "yes" ] && \
for (( index = 0; index < "${#INTERFACE1_NTP_OUT_DST_IPADDR[@]}"; index++ ))
do

    accept_ntp_request interface1_out ${INTERFACE1_NTP_OUT_SRC_IPADDR[$index]} ${INTERFACE1_NTP_OUT_DST_IPADDR[$index]}
    accept_ntp_reply    interface1_in ${INTERFACE1_NTP_OUT_DST_IPADDR[$index]} ${INTERFACE1_NTP_OUT_SRC_IPADDR[$index]}

done

# Network 1 NTP forwarded outgoing client request

[ -n "$INTERFACE1" ] && [ "$NETWORK1_NTP_CLIENT" == "yes" ] && \
[ "$DEBUG" = "on" ] && echo -e "# Network 1 NTP forwarded outgoing client request"

[ -n "$INTERFACE1" ] && [ "$NETWORK1_NTP_CLIENT" == "yes" ] && \
for (( index = 0; index < "${#NETWORK1_NTP_OUT_DST_IPADDR[@]}"; index++ ))
do

    [ "$NETWORK1_NAT" == "yes" ] && \
    snat_ntp_request $INTERFACE0 $INTERFACE0_IPADDR ${NETWORK1_NTP_OUT_SRC_IPADDR[$index]} ${NETWORK1_NTP_OUT_DST_IPADDR[$index]}
    accept_ntp_request           network1_out ${NETWORK1_NTP_OUT_SRC_IPADDR[$index]} ${NETWORK1_NTP_OUT_DST_IPADDR[$index]}
    accept_ntp_reply             network1_in ${NETWORK1_NTP_OUT_DST_IPADDR[$index]} ${NETWORK1_NTP_OUT_SRC_IPADDR[$index]}

done

# ----------------------------------------------------------------------------
# NTP incoming client request
#

[ "$DEBUG" = "on" ] && echo -e "# NTP incoming client request"

# Interface 0 NTP incoming client request

[ "$INTERFACE0_NTP_SERVER" == "yes" ] && \
[ "$DEBUG" = "on" ] && echo -e "# Interface 0 NTP incoming client request"

[ "$INTERFACE0_NTP_SERVER" == "yes" ] && \
for (( index = 0; index < "${#INTERFACE0_NTP_IN_SRC_IPADDR[@]}"; index++ ))
do

    accept_ntp_request interface0_in ${INTERFACE0_NTP_IN_SRC_IPADDR[$index]} ${INTERFACE0_NTP_IN_DST_IPADDR[$index]}
    accept_ntp_reply  interface0_out ${INTERFACE0_NTP_IN_DST_IPADDR[$index]} ${INTERFACE0_NTP_IN_SRC_IPADDR[$index]}

done

# Interface 1 NTP incoming client request

[ -n "$INTERFACE1" ] && [ "$INTERFACE1_NTP_SERVER" == "yes" ] && \
[ "$DEBUG" = "on" ] && echo -e "# Interface 1 NTP incoming client request"

[ -n "$INTERFACE1" ] && [ "$INTERFACE1_NTP_SERVER" == "yes" ] && \
for (( index = 0; index < "${#INTERFACE1_NTP_IN_SRC_IPADDR[@]}"; index++ ))
do

    accept_ntp_request interface1_in ${INTERFACE1_NTP_IN_SRC_IPADDR[$index]} ${INTERFACE1_NTP_IN_DST_IPADDR[$index]}
    accept_ntp_reply  interface1_out ${INTERFACE1_NTP_IN_DST_IPADDR[$index]} ${INTERFACE1_NTP_IN_SRC_IPADDR[$index]}

done

# Network 1 NTP forwarded incoming client request

[ -n "$INTERFACE1" ] && [ "$NETWORK1_NTP_SERVER" == "yes" ] && \
[ "$DEBUG" = "on" ] && echo -e "# Network 1 NTP forwarded incoming client request"

[ -n "$INTERFACE1" ] && [ "$NETWORK1_NTP_SERVER" == "yes" ] && \
for (( index = 0; index < "${#NETWORK1_NTP_IN_SRC_IPADDR[@]}"; index++ ))
do

    [ "$NETWORK1_NAT" == "yes" ] && \
    dnat_ntp_request $INTERFACE0 $INTERFACE0_IPADDR ${NETWORK1_NTP_IN_SRC_IPADDR[$index]} ${NETWORK1_NTP_IN_DST_IPADDR[$index]}
    accept_ntp_request                  network1_in ${NETWORK1_NTP_IN_SRC_IPADDR[$index]} ${NETWORK1_NTP_IN_DST_IPADDR[$index]}
    accept_ntp_reply                   network1_out ${NETWORK1_NTP_IN_DST_IPADDR[$index]} ${NETWORK1_NTP_IN_SRC_IPADDR[$index]}

done

# ----------------------------------------------------------------------------
# End of file