27ccd8f12759f69c7c226cac0c923335b8ccc6a8
[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 vxlan_generic_setup() {
11 local cfg="$1"
12 local mode="$2"
13 local local="$3"
14 local remote="$4"
15
16 local link="$cfg"
17
18 local port vid ttl tos mtu macaddr zone
19 json_get_vars port vid ttl tos mtu macaddr zone
20
21
22 proto_init_update "$link" 1
23
24 proto_add_tunnel
25 json_add_string mode "$mode"
26
27 [ -n "$tunlink" ] && json_add_string link "$tunlink"
28 [ -n "$local" ] && json_add_string local "$local"
29 [ -n "$remote" ] && json_add_string remote "$remote"
30
31 [ -n "$ttl" ] && json_add_int ttl "$ttl"
32 [ -n "$tos" ] && json_add_string tos "$tos"
33 [ -n "$mtu" ] && json_add_int mtu "$mtu"
34
35 json_add_object 'data'
36 [ -n "$port" ] && json_add_int port "$port"
37 [ -n "$vid" ] && json_add_int id "$vid"
38 [ -n "$macaddr" ] && json_add_string macaddr "$macaddr"
39 json_close_object
40
41 proto_close_tunnel
42
43 proto_add_data
44 [ -n "$zone" ] && json_add_string zone "$zone"
45 proto_close_data
46
47 proto_send_update "$cfg"
48 }
49
50 proto_vxlan_setup() {
51 local cfg="$1"
52
53 local ipaddr peeraddr
54 json_get_vars ipaddr peeraddr tunlink
55
56 [ -z "$peeraddr" ] && {
57 proto_notify_error "$cfg" "MISSING_ADDRESS"
58 proto_block_restart "$cfg"
59 exit
60 }
61
62 ( proto_add_host_dependency "$cfg" '' "$tunlink" )
63
64 [ -z "$ipaddr" ] && {
65 local wanif="$tunlink"
66 if [ -z "$wanif" ] && ! network_find_wan wanif; then
67 proto_notify_error "$cfg" "NO_WAN_LINK"
68 exit
69 fi
70
71 if ! network_get_ipaddr ipaddr "$wanif"; then
72 proto_notify_error "$cfg" "NO_WAN_LINK"
73 exit
74 fi
75 }
76
77 vxlan_generic_setup "$cfg" 'vxlan' "$ipaddr" "$peeraddr"
78 }
79
80 proto_vxlan6_setup() {
81 local cfg="$1"
82
83 local ip6addr peer6addr
84 json_get_vars ip6addr peer6addr tunlink
85
86 [ -z "$peer6addr" ] && {
87 proto_notify_error "$cfg" "MISSING_ADDRESS"
88 proto_block_restart "$cfg"
89 exit
90 }
91
92 ( proto_add_host_dependency "$cfg" '' "$tunlink" )
93
94 [ -z "$ip6addr" ] && {
95 local wanif="$tunlink"
96 if [ -z "$wanif" ] && ! network_find_wan6 wanif; then
97 proto_notify_error "$cfg" "NO_WAN_LINK"
98 exit
99 fi
100
101 if ! network_get_ipaddr6 ip6addr "$wanif"; then
102 proto_notify_error "$cfg" "NO_WAN_LINK"
103 exit
104 fi
105 }
106
107 vxlan_generic_setup "$cfg" 'vxlan6' "$ip6addr" "$peer6addr"
108 }
109
110 proto_vxlan_teardown() {
111 local cfg="$1"
112 }
113
114 proto_vxlan6_teardown() {
115 local cfg="$1"
116 }
117
118 vxlan_generic_init_config() {
119 no_device=1
120 available=1
121
122 proto_config_add_string "tunlink"
123 proto_config_add_string "zone"
124
125 proto_config_add_int "vid"
126 proto_config_add_int "port"
127 proto_config_add_int "ttl"
128 proto_config_add_int "tos"
129 proto_config_add_int "mtu"
130 proto_config_add_string "macaddr"
131 }
132
133 proto_vxlan_init_config() {
134 vxlan_generic_init_config
135 proto_config_add_string "ipaddr"
136 proto_config_add_string "peeraddr"
137 }
138
139 proto_vxlan6_init_config() {
140 vxlan_generic_init_config
141 proto_config_add_string "ip6addr"
142 proto_config_add_string "peer6addr"
143 }
144
145 [ -n "$INCLUDE_ONLY" ] || {
146 add_protocol vxlan
147 add_protocol vxlan6
148 }