File: batman

package info (click to toggle)
ifupdown-ng 0.12.1-8
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 964 kB
  • sloc: ansic: 3,572; sh: 980; makefile: 233
file content (126 lines) | stat: -rw-r--r-- 3,290 bytes parent folder | download | duplicates (4)
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
#!/bin/sh
#
# Maximilian Wilhelm <max@sdn.clinic>
#  --  Wed 26 Aug 2020 08:15:58 PM CEST
#
# This executor is responsible for setting up the main B.A.T.M.A.N. adv. interface (eg. bat0)
# as well as managing settings of the underlying interfaces (hardifs).
#
# See interfaces-batman(5) for a list of supported options for hardifs as well as meshifs.
#
if [ "$VERBOSE" ]; then
	set -x
fi

if ! which batctl >/dev/null 2>&1; then
	echo "Error: batctl utility not found!" >&2
	exit 1
fi

#
# Compatiblity glue: Newer versions of batctl print a deprecation
# warning when called with -m <batif>. Avoid spamming the log and
# producting SPAM by silently handling this here.
mesh_if_param="-m"
if batctl -h 2>&1 | grep -q "meshif"; then
        mesh_if_param="meshif"
fi

#
# Functions to manage main B.A.T.M.A.N. adv. interface
batctl_if () {
	for iface in ${IF_BATMAN_IFACES}; do
		${MOCK} batctl "${mesh_if_param}" "${IFACE}" interface "$1" "${iface}"
	done
}

set_meshif_options () {
	# We only care for options of format IF_BATMAN_<OPTION_NAME>
	env | grep '^IF_BATMAN_[A-Z0-9_]\+' | while IFS="=" read opt value; do
		# Members, ignore-regex, routing-algo, and throughput_override are handled seperately
		if [ "${opt}" = "IF_BATMAN_IFACES" -o \
		     "${opt}" = "IF_BATMAN_IFACES_IGNORE_REGEX" -o \
		     "${opt}" = "IF_BATMAN_ROUTING_ALGO" -o \
		     "${opt}" = "IF_BATMAN_THROUGHPUT_OVERRIDE" ]; then
			continue
		fi

		# Convert options for the actual name
		real_opt=$(echo "${opt}" | sed -e 's/^IF_BATMAN_\([A-Z0-9_]\+\)/\1/' | tr '[A-Z]' '[a-z]')

		${MOCK} batctl "${mesh_if_param}" "${IFACE}" "${real_opt}" "${value}"
	done

}

set_routing_algo () {
	if [ "${IF_BATMAN_ROUTING_ALGO}" != "BATMAN_IV" -a "${IF_BATMAN_ROUTING_ALGO}" != "BATMAN_V" ]; then
		echo "Invalid routing algorithm \"$1\"."
		return
	fi

	batctl ra "${IF_BATMAN_ROUTING_ALGO}"
}


#
# Functions to manage B.A.T.M.A.N. adv. underlay interfaces (hardifs)
set_hardif_options () {
	# Query hardif parameter manually for now
	for hardif in ${IF_BATMAN_IFACES}; do
		penalty=$(ifquery -p "batman-hop-penalty" "${hardif}")
		if [ "${penalty}" ]; then
			${MOCK} batctl hardif "${hardif}" hop_penalty "${penalty}"
		fi

		throughput=$(ifquery -p "batman-throughput-override" "${hardif}")
		if [ "${throughput}" ]; then
			${MOCK} batctl hardif "${hardif}" throughput_override "${througput}"
		fi
	done
}


case "${PHASE}" in
	depend)
		if [ "${IF_BATMAN_IFACES}" ]; then
			echo "${IF_BATMAN_IFACES}"
		fi
		;;

	create)
		# Main B.A.T.M.A.N. adv. interface
		if [ "${IF_BATMAN_IFACES}" ]; then
			if [ "${IF_BATMAN_ROUTING_ALGO}" ]; then
				set_routing_algo
			fi

			if [ ! -d "/sys/class/net/${IFACE}" ]; then
				batctl "${mesh_if_param}" "${IFACE}" interface create || true
			fi
		fi
		;;

	pre-up)
		# Main B.A.T.M.A.N. adv. interface (meshif)
		if [ "${IF_BATMAN_IFACES}" ]; then
			batctl_if add
			set_meshif_options
			set_hardif_options
		fi
		;;

	destroy)
		if [ "${IF_BATMAN_IFACES}" -a -d "/sys/class/net/${IFACE}" ]; then
			# Newer versions of batctl provide the "interface destroy" API, try to use it
			if ! batctl "${mesh_if_param}" "${IFACE}" interface destroy 2>/dev/null; then
				# Fall back to old style member interface removal
				batctl_if del
			fi
		fi
		;;

	*)
		exit 0
		;;
esac