lldpd: Allow neighbour filtering
[openwrt/staging/robimarko.git] / package / network / services / lldpd / files / lldpd.init
1 #!/bin/sh /etc/rc.common
2 # Copyright (C) 2008-2015 OpenWrt.org
3
4 START=90
5 STOP=01
6
7 USE_PROCD=1
8 LLDPDBIN=/usr/sbin/lldpd
9 LLDPCLI=/usr/sbin/lldpcli
10 LLDPSOCKET=/var/run/lldpd.socket
11 LLDPD_CONF=/tmp/lldpd.conf
12 LLDPD_CONFS_DIR=/tmp/lldpd.d
13
14 LLDPD_RUN=/var/run/lldpd
15 LLDPD_RESTART_HASH=${LLDPD_RUN}/lldpd.restart_hash
16
17 . "$IPKG_INSTROOT/lib/functions/network.sh"
18
19 find_release_info()
20 {
21 [ -s /etc/os-release ] && . /etc/os-release
22 [ -z "$PRETTY_NAME" ] && [ -s /etc/openwrt_version ] && \
23 PRETTY_NAME="$(cat /etc/openwrt_version)"
24
25 echo "${PRETTY_NAME:-Unknown OpenWrt release} @ $(cat /proc/sys/kernel/hostname)"
26 }
27
28 get_config_restart_hash() {
29 local var="$1"
30 local _string _hash v
31
32 config_load 'lldpd'
33
34 config_get v 'config' 'lldp_class'; append _string "$v" ","
35 config_get v 'config' 'agentxsocket'; append _string "$v" ","
36 config_get v 'config' 'cid_interface'; append _string "$v" ","
37 config_get v 'config' 'filter'; append _string "$v" ","
38 config_get_bool v 'config' 'readonly_mode'; append _string "$v" ","
39 config_get_bool v 'config' 'enable_lldp' 1; append _string "$v" ","
40 config_get_bool v 'config' 'enable_cdp'; append _string "$v" ","
41 config_get_bool v 'config' 'enable_edp'; append _string "$v" ","
42 config_get_bool v 'config' 'enable_fdp'; append _string "$v" ","
43 config_get_bool v 'config' 'enable_sonmp'; append _string "$v" ","
44
45 _hash=`echo -n "${_string}" | md5sum | awk '{ print \$1 }'`
46 export -n "$var=$_hash"
47 }
48
49 get_config_cid_ifaces() {
50 local _ifaces
51 config_get _ifaces 'config' 'cid_interface'
52
53 local _iface _ifnames=""
54 for _iface in $_ifaces; do
55 local _ifname=""
56 if network_get_device _ifname "$_iface" || [ -e "/sys/class/net/$_iface" ]; then
57 append _ifnames "${_ifname:-$_iface}" ","
58 fi
59 done
60
61 export -n "${1}=$_ifnames"
62 }
63
64 write_lldpd_conf()
65 {
66 local lldp_description
67
68 config_load 'lldpd'
69 config_get lldp_description 'config' 'lldp_description' "$(find_release_info)"
70
71 local lldp_hostname
72 config_get lldp_hostname 'config' 'lldp_hostname' "$(cat /proc/sys/kernel/hostname)"
73
74 local ifaces
75 config_get ifaces 'config' 'interface'
76
77 local iface ifnames=""
78 for iface in $ifaces; do
79 local ifname=""
80 if network_get_device ifname "$iface" || [ -e "/sys/class/net/$iface" ]; then
81 append ifnames "${ifname:-$iface}" ","
82 fi
83 done
84
85 local lldp_mgmt_ip
86 config_get lldp_mgmt_ip 'config' 'lldp_mgmt_ip'
87
88 local lldp_syscapabilities
89 config_get lldp_syscapabilities 'config' 'lldp_syscapabilities'
90
91 # Clear out the config file first
92 echo -n > "$LLDPD_CONF"
93 [ -n "$ifnames" ] && echo "configure system interface pattern" "$ifnames" >> "$LLDPD_CONF"
94 [ -n "$lldp_description" ] && echo "configure system description" "\"$lldp_description\"" >> "$LLDPD_CONF"
95 [ -n "$lldp_hostname" ] && echo "configure system hostname" "\"$lldp_hostname\"" >> "$LLDPD_CONF"
96 [ -n "$lldp_mgmt_ip" ] && echo "configure system ip management pattern" "\"$lldp_mgmt_ip\"" >> "$LLDPD_CONF"
97 [ -n "$lldp_syscapabilities" ] && echo "configure system capabilities enabled" "\"$lldp_syscapabilities\"" >> "$LLDPD_CONF"
98
99 # Since lldpd's sysconfdir is /tmp, we'll symlink /etc/lldpd.d to /tmp/$LLDPD_CONFS_DIR
100 [ -e $LLDPD_CONFS_DIR ] || ln -s /etc/lldpd.d $LLDPD_CONFS_DIR
101 }
102
103 start_service() {
104
105 local enable_cdp
106 local enable_fdp
107 local enable_sonmp
108 local enable_edp
109 local lldp_class
110 local lldp_location
111 local readonly_mode
112 local agentxsocket
113 local filter
114
115 config_load 'lldpd'
116 config_get_bool enable_cdp 'config' 'enable_cdp' 0
117 config_get_bool enable_fdp 'config' 'enable_fdp' 0
118 config_get_bool enable_sonmp 'config' 'enable_sonmp' 0
119 config_get_bool enable_edp 'config' 'enable_edp' 0
120 config_get lldp_class 'config' 'lldp_class'
121 config_get lldp_location 'config' 'lldp_location'
122 config_get_bool readonly_mode 'config' 'readonly_mode' 0
123 config_get agentxsocket 'config' 'agentxsocket'
124 config_get filter 'config' 'filter' 15
125
126 mkdir -p ${LLDPD_RUN}
127 chown lldp:lldp ${LLDPD_RUN}
128
129 # When lldpd starts, it also loads up what we write in this config file
130 write_lldpd_conf
131
132 procd_open_instance
133 procd_set_param command ${LLDPDBIN}
134 procd_append_param command -d
135
136 [ $enable_cdp -gt 0 ] && procd_append_param command '-c'
137 [ $enable_fdp -gt 0 ] && procd_append_param command '-f'
138 [ $enable_sonmp -gt 0 ] && procd_append_param command '-s'
139 [ $enable_edp -gt 0 ] && procd_append_param command '-e'
140 [ $readonly_mode -gt 0 ] && procd_append_param command '-r'
141 [ -n "$lldp_class" ] && procd_append_param command -M "$lldp_class"
142 [ -n "$agentxsocket" ] && procd_append_param command -x -X "$agentxsocket"
143 [ -n "$filter" ] && procd_append_param command -H "$filter"
144
145 # ChassisID interfaces
146 local ifnames
147 get_config_cid_ifaces ifnames
148 [ -n "$ifnames" ] && procd_append_param command -C "$ifnames"
149
150 # Overwrite default configuration locations processed by lldpcli at start
151 procd_append_param command -O "$LLDPD_CONF"
152
153 local restart_hash
154 get_config_restart_hash restart_hash
155 echo -n "$restart_hash" > $LLDPD_RESTART_HASH
156
157 # set auto respawn behavior
158 procd_set_param respawn
159 procd_close_instance
160 }
161
162 service_triggers() {
163 procd_add_config_trigger "config.change" "lldpd" /etc/init.d/lldpd reload
164 }
165
166 reload_service() {
167 running || return 1
168
169 local running_hash=""
170 local config_hash=""
171
172 get_config_restart_hash config_hash
173 if [ -f ${LLDPD_RESTART_HASH} ]; then running_hash=`cat $LLDPD_RESTART_HASH`; fi
174
175 if [ "x$running_hash" != "x$config_hash" ]; then
176 # Restart LLDPd
177 # Some parameters can't be configured at runtime
178 restart
179 return 0
180 fi
181
182 $LLDPCLI -u $LLDPSOCKET &> /dev/null <<-EOF
183 pause
184 unconfigure lldp custom-tlv
185 unconfigure system interface pattern
186 unconfigure system description
187 unconfigure system hostname
188 unconfigure system ip management pattern
189 EOF
190 # Rewrite lldpd.conf
191 # If something changed it should be included by the lldpcli call
192 write_lldpd_conf
193 $LLDPCLI -u $LLDPSOCKET -c $LLDPD_CONF -c $LLDPD_CONFS_DIR &> /dev/null
194 # Broadcast update over the wire
195 $LLDPCLI -u $LLDPSOCKET &> /dev/null <<-EOF
196 resume
197 update
198 EOF
199 return 0
200 }
201
202 stop_service() {
203 rm -rf ${LLDPD_RUN} $LLDPSOCKET 2>/dev/null
204 }
205