]> git.ipfire.org Git - people/stevee/network.git/blob - functions.ip
firewall: Add basic IPv6 ruleset generation and macros.
[people/stevee/network.git] / functions.ip
1 #!/bin/bash
2 ###############################################################################
3 # #
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2010 Michael Tremer & Christian Schmidt #
6 # #
7 # This program is free software: you can redistribute it and/or modify #
8 # it under the terms of the GNU General Public License as published by #
9 # the Free Software Foundation, either version 3 of the License, or #
10 # (at your option) any later version. #
11 # #
12 # This program is distributed in the hope that it will be useful, #
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
15 # GNU General Public License for more details. #
16 # #
17 # You should have received a copy of the GNU General Public License #
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. #
19 # #
20 ###############################################################################
21
22 # A list of supported versions of the IP protocol
23 IP_SUPPORTED_PROTOCOLS=""
24
25 function ip_split_prefix() {
26 local address=${1}
27 assert isset address
28
29 echo "${address%%/*}"
30 }
31
32 function ip_get_prefix() {
33 local address=${1}
34 assert isset address
35
36 # Break if no prefix is provided
37 [[ ${address} =~ \/ ]] || return ${EXIT_OK}
38
39 echo "${address##*/}"
40 }
41
42 function ip_detect_protocol() {
43 local address=${1}
44
45 assert isset address
46
47 local protocol
48 for protocol in ${IP_SUPPORTED_PROTOCOLS}; do
49 if ${protocol}_is_valid ${address}; then
50 echo "${protocol}"
51 return ${EXIT_OK}
52 fi
53 done
54
55 log DEBUG "Protocol version of address '${address}' could not be detected."
56
57 return ${EXIT_ERROR}
58 }
59
60 function ip_protocol_is_supported() {
61 local proto=${1}
62
63 assert isset proto
64
65 listmatch ${proto} ${IP_SUPPORTED_PROTOCOLS}
66 }
67
68 function ip_is_valid() {
69 local address=${1}
70 assert isset address
71
72 local proto=$(ip_detect_protocol ${address})
73 isset proto && return ${EXIT_TRUE} || return ${EXIT_FALSE}
74 }
75
76 function ip_address_add() {
77 local device=${1}
78 local address=${2}
79
80 assert isset address
81 assert device_exists ${device}
82
83 local prefix=$(ip_get_prefix ${address})
84 address=$(ip_split_prefix ${address})
85
86 assert isset prefix
87
88 # Detect the protocol version
89 local protocol=$(ip_detect_protocol ${address}/${prefix})
90 assert ip_protocol_is_supported ${protocol}
91
92 case "${protocol}" in
93 ipv4)
94 if ipv4_detect_duplicate ${device} ${address}; then
95 error_log "Duplicate address detected on zone '${device}' (${address})."
96 error_log "Cannot continue."
97 return ${EXIT_ERROR}
98 fi
99 ;;
100 esac
101
102 if ! device_has_ip ${device} ${address}/${prefix}; then
103 assert ip addr add ${address}/${prefix} dev ${device}
104
105 log DEBUG "IP address '${address}' (${protocol}) was successfully configured on device '${device}'."
106
107 case "${protocol}" in
108 ipv4)
109 # Announce our new address to the neighbours
110 ipv4_update_neighbours ${device} ${address}
111 ;;
112 esac
113 else
114 log DEBUG "IP address '${address}' (${protocol}) was already configured on device '${device}'."
115 fi
116
117 return ${EXIT_OK}
118 }
119
120 function ip_address_del() {
121 local device=${1}
122 local address=${2}
123
124 assert isset address
125 assert device_exists ${device}
126
127 local prefix=$(ip_get_prefix ${address})
128 address=$(ip_split_prefix ${address})
129
130 assert isset prefix
131
132 # Detect the protocol version
133 local protocol=$(ip_detect_protocol ${address}/${prefix})
134 assert ip_protocol_is_supported ${protocol}
135
136 if device_has_ip ${device} ${address}/${prefix}; then
137 assert ip addr del ${address}/${prefix} dev ${device}
138
139 log DEBUG "IP address '${address}' (${protocol}) was successfully removed from device '${device}'."
140 else
141 log DEBUG "IP address '${address}' (${protocol}) was not configured on device '${device}'."
142 fi
143
144 return ${EXIT_OK}
145 }