Debian Codegraph @

executor-scripts/linux/batman

raw
1 #!/bin/sh
2 #
3 # Maximilian Wilhelm <[email protected]>
4 # -- Wed 26 Aug 2020 08:15:58 PM CEST
5 #
6 # This executor is responsible for setting up the main B.A.T.M.A.N. adv. interface (eg. bat0)
7 # as well as managing settings of the underlying interfaces (hardifs).
8 #
9 # See interfaces-batman(5) for a list of supported options for hardifs as well as meshifs.
10 #
11 if [ "$VERBOSE" ]; then
12 set -x
13 fi
14
15 if ! which batctl >/dev/null 2>&1; then
16 echo "Error: batctl utility not found!" >&2
17 exit 1
18 fi
19
20 #
21 # Compatiblity glue: Newer versions of batctl print a deprecation
22 # warning when called with -m <batif>. Avoid spamming the log and
23 # producting SPAM by silently handling this here.
24 mesh_if_param="-m"
25 if batctl -h 2>&1 | grep -q "meshif"; then
26 mesh_if_param="meshif"
27 fi
28
29 #
30 # Functions to manage main B.A.T.M.A.N. adv. interface
31 batctl_if () {
32 for iface in ${IF_BATMAN_IFACES}; do
33 ${MOCK} batctl "${mesh_if_param}" "${IFACE}" interface "$1" "${iface}"
34 done
35 }
36
37 set_meshif_options () {
38 # We only care for options of format IF_BATMAN_<OPTION_NAME>
39 env | grep '^IF_BATMAN_[A-Z0-9_]\+' | while IFS="=" read opt value; do
40 # Members, ignore-regex, routing-algo, and throughput_override are handled seperately
41 if [ "${opt}" = "IF_BATMAN_IFACES" -o \
42 "${opt}" = "IF_BATMAN_IFACES_IGNORE_REGEX" -o \
43 "${opt}" = "IF_BATMAN_ROUTING_ALGO" -o \
44 "${opt}" = "IF_BATMAN_THROUGHPUT_OVERRIDE" ]; then
45 continue
46 fi
47
48 # Convert options for the actual name
49 real_opt=$(echo "${opt}" | sed -e 's/^IF_BATMAN_\([A-Z0-9_]\+\)/\1/' | tr '[A-Z]' '[a-z]')
50
51 ${MOCK} batctl "${mesh_if_param}" "${IFACE}" "${real_opt}" "${value}"
52 done
53
54 }
55
56 set_routing_algo () {
57 if [ "${IF_BATMAN_ROUTING_ALGO}" != "BATMAN_IV" -a "${IF_BATMAN_ROUTING_ALGO}" != "BATMAN_V" ]; then
58 echo "Invalid routing algorithm \"$1\"."
59 return
60 fi
61
62 batctl ra "${IF_BATMAN_ROUTING_ALGO}"
63 }
64
65
66 #
67 # Functions to manage B.A.T.M.A.N. adv. underlay interfaces (hardifs)
68 set_hardif_options () {
69 # Query hardif parameter manually for now
70 for hardif in ${IF_BATMAN_IFACES}; do
71 penalty=$(ifquery -p "batman-hop-penalty" "${hardif}")
72 if [ "${penalty}" ]; then
73 ${MOCK} batctl hardif "${hardif}" hop_penalty "${penalty}"
74 fi
75
76 throughput=$(ifquery -p "batman-throughput-override" "${hardif}")
77 if [ "${throughput}" ]; then
78 ${MOCK} batctl hardif "${hardif}" throughput_override "${througput}"
79 fi
80 done
81 }
82
83
84 case "${PHASE}" in
85 depend)
86 if [ "${IF_BATMAN_IFACES}" ]; then
87 echo "${IF_BATMAN_IFACES}"
88 fi
89 ;;
90
91 create)
92 # Main B.A.T.M.A.N. adv. interface
93 if [ "${IF_BATMAN_IFACES}" ]; then
94 if [ "${IF_BATMAN_ROUTING_ALGO}" ]; then
95 set_routing_algo
96 fi
97
98 if [ ! -d "/sys/class/net/${IFACE}" ]; then
99 batctl "${mesh_if_param}" "${IFACE}" interface create || true
100 fi
101 fi
102 ;;
103
104 pre-up)
105 # Main B.A.T.M.A.N. adv. interface (meshif)
106 if [ "${IF_BATMAN_IFACES}" ]; then
107 batctl_if add
108 set_meshif_options
109 set_hardif_options
110 fi
111 ;;
112
113 destroy)
114 if [ "${IF_BATMAN_IFACES}" -a -d "/sys/class/net/${IFACE}" ]; then
115 # Newer versions of batctl provide the "interface destroy" API, try to use it
116 if ! batctl "${mesh_if_param}" "${IFACE}" interface destroy 2>/dev/null; then
117 # Fall back to old style member interface removal
118 batctl_if del
119 fi
120 fi
121 ;;
122
123 *)
124 exit 0
125 ;;
126 esac