5c1c484c47f7fb48f1cb705b200d589df77a60f6
[openwrt/openwrt.git] / package / network / config / vxlan / files / vxlan.sh
1 #!/bin/sh
2
3 [ -n "$INCLUDE_ONLY" ] || {
4 . /lib/functions.sh
5 . /lib/functions/network.sh
6 . ../netifd-proto.sh
7 init_proto "$@"
8 }
9
10 proto_vxlan_setup_peer() {
11 type bridge &> /dev/null || {
12 proto_notify_error "$cfg" "MISSING_BRIDGE_COMMAND"
13 exit
14 }
15
16 local peer_config="$1"
17
18 local vxlan
19 local lladdr
20 local dst
21 local src_vni
22 local vni
23 local port
24 local via
25
26 config_get vxlan "${peer_config}" "vxlan"
27 config_get lladdr "${peer_config}" "lladdr"
28 config_get dst "${peer_config}" "dst"
29 config_get src_vni "${peer_config}" "src_vni"
30 config_get vni "${peer_config}" "vni"
31 config_get port "${peer_config}" "port"
32 config_get via "${peer_config}" "via"
33
34 [ "$cfg" = "$vxlan" ] || {
35 # This peer section belongs to another device
36 return
37 }
38
39 [ -n "${dst}" ] || {
40 proto_notify_error "$cfg" "MISSING_PEER_ADDRESS"
41 exit
42 }
43
44 bridge fdb append \
45 ${lladdr:-00:00:00:00:00:00} \
46 dev ${cfg} \
47 dst ${dst} \
48 ${src_vni:+src_vni $src_vni} \
49 ${vni:+vni $vni} \
50 ${port:+port $port} \
51 ${via:+via $via}
52 }
53
54 vxlan_generic_setup() {
55 local cfg="$1"
56 local mode="$2"
57 local local="$3"
58 local remote="$4"
59
60 local link="$cfg"
61
62 local port vid ttl tos mtu macaddr zone rxcsum txcsum srcportmin srcportmax ageing maxaddress learning rsc proxy l2miss l3miss gbp
63 json_get_vars port vid ttl tos mtu macaddr zone rxcsum txcsum srcportmin srcportmax ageing maxaddress learning rsc proxy l2miss l3miss gbp
64
65 proto_init_update "$link" 1
66
67 proto_add_tunnel
68 json_add_string mode "$mode"
69
70 [ -n "$tunlink" ] && json_add_string link "$tunlink"
71 [ -n "$local" ] && json_add_string local "$local"
72 [ -n "$remote" ] && json_add_string remote "$remote"
73
74 [ -n "$ttl" ] && json_add_int ttl "$ttl"
75 [ -n "$tos" ] && json_add_string tos "$tos"
76 [ -n "$mtu" ] && json_add_int mtu "$mtu"
77
78 json_add_object 'data'
79 [ -n "$port" ] && json_add_int port "$port"
80 [ -n "$vid" ] && json_add_int id "$vid"
81 [ -n "$srcportmin" ] && json_add_int srcportmin "$srcportmin"
82 [ -n "$srcportmax" ] && json_add_int srcportmax "$srcportmax"
83 [ -n "$ageing" ] && json_add_int ageing "$ageing"
84 [ -n "$maxaddress" ] && json_add_int maxaddress "$maxaddress"
85 [ -n "$macaddr" ] && json_add_string macaddr "$macaddr"
86 [ -n "$rxcsum" ] && json_add_boolean rxcsum "$rxcsum"
87 [ -n "$txcsum" ] && json_add_boolean txcsum "$txcsum"
88 [ -n "$learning" ] && json_add_boolean learning "$learning"
89 [ -n "$rsc" ] && json_add_boolean rsc "$rsc"
90 [ -n "$proxy" ] && json_add_boolean proxy "$proxy"
91 [ -n "$l2miss" ] && json_add_boolean l2miss "$l2miss"
92 [ -n "$l3miss" ] && json_add_boolean l3miss "$l3miss"
93 [ -n "$gbp" ] && json_add_boolean gbp "$gbp"
94
95 json_close_object
96
97 proto_close_tunnel
98
99 proto_add_data
100 [ -n "$zone" ] && json_add_string zone "$zone"
101 proto_close_data
102
103 proto_send_update "$cfg"
104
105 config_load network
106 config_foreach proto_vxlan_setup_peer "vxlan_peer"
107 }
108
109 proto_vxlan_setup() {
110 local cfg="$1"
111
112 local ipaddr peeraddr
113 json_get_vars ipaddr peeraddr tunlink
114
115 ( proto_add_host_dependency "$cfg" '' "$tunlink" )
116
117 [ -z "$ipaddr" ] && {
118 local wanif="$tunlink"
119 if [ -z "$wanif" ] && ! network_find_wan wanif; then
120 proto_notify_error "$cfg" "NO_WAN_LINK"
121 exit
122 fi
123
124 if ! network_get_ipaddr ipaddr "$wanif"; then
125 proto_notify_error "$cfg" "NO_WAN_LINK"
126 exit
127 fi
128 }
129
130 vxlan_generic_setup "$cfg" 'vxlan' "$ipaddr" "$peeraddr"
131 }
132
133 proto_vxlan6_setup() {
134 local cfg="$1"
135
136 local ip6addr peer6addr
137 json_get_vars ip6addr peer6addr tunlink
138
139 ( proto_add_host_dependency "$cfg" '' "$tunlink" )
140
141 [ -z "$ip6addr" ] && {
142 local wanif="$tunlink"
143 if [ -z "$wanif" ] && ! network_find_wan6 wanif; then
144 proto_notify_error "$cfg" "NO_WAN_LINK"
145 exit
146 fi
147
148 if ! network_get_ipaddr6 ip6addr "$wanif"; then
149 proto_notify_error "$cfg" "NO_WAN_LINK"
150 exit
151 fi
152 }
153
154 vxlan_generic_setup "$cfg" 'vxlan6' "$ip6addr" "$peer6addr"
155 }
156
157 proto_vxlan_teardown() {
158 local cfg="$1"
159 }
160
161 proto_vxlan6_teardown() {
162 local cfg="$1"
163 }
164
165 vxlan_generic_init_config() {
166 no_device=1
167 available=1
168
169 proto_config_add_string "tunlink"
170 proto_config_add_string "zone"
171
172 proto_config_add_int "vid"
173 proto_config_add_int "port"
174 proto_config_add_int "ttl"
175 proto_config_add_int "tos"
176 proto_config_add_int "mtu"
177 proto_config_add_int "srcportmin"
178 proto_config_add_int "srcportmax"
179 proto_config_add_int "ageing"
180 proto_config_add_int "maxaddress"
181 proto_config_add_boolean "rxcsum"
182 proto_config_add_boolean "txcsum"
183 proto_config_add_boolean "learning"
184 proto_config_add_boolean "rsc"
185 proto_config_add_boolean "proxy"
186 proto_config_add_boolean "l2miss"
187 proto_config_add_boolean "l3miss"
188 proto_config_add_boolean "gbp"
189 proto_config_add_string "macaddr"
190
191 }
192
193 proto_vxlan_init_config() {
194 vxlan_generic_init_config
195 proto_config_add_string "ipaddr"
196 proto_config_add_string "peeraddr"
197 }
198
199 proto_vxlan6_init_config() {
200 vxlan_generic_init_config
201 proto_config_add_string "ip6addr"
202 proto_config_add_string "peer6addr"
203 }
204
205 [ -n "$INCLUDE_ONLY" ] || {
206 add_protocol vxlan
207 add_protocol vxlan6
208 }