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