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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
|
#!/bin/bash
# Author: Joe Maimon
# Released to public domain
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the Free
# Software Foundation; either version 2 of the License, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
# more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; see the file COPYING; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#
PROGNAME=`basename $0`
VERSION="0.0.6"
#api fields
EV_ID="eventid"
EV_TYPE="type"
EV_OTYPE="old_type"
EV_NUMNHS="num_nhs"
EV_INT="interface"
EV_LADDR="local_address"
EV_VCINIT="vc_initiated"
EV_LNBMA="local_nbma"
EV_LCERT="local_cert"
EV_RADDR="remote_addr"
EV_RNBMA="remote_nbma"
EV_RCERT="remote_cert"
usage()
{
echo "Usage: $PROGNAME [-s nhrp-sock] [-d] [-i interface-name] [-t table] [-e execute-cmd] [-u user] [-g group] [-r] [-l logfile]"
echo ""
echo "-s nhrp-sock file"
echo "-i interface-name to execute on, may be repeated multiple times"
echo "-t tableid to execute on for immdiate preceeding interface"
echo "-e execute command for immmediate preceeding interface"
echo " The command will be passed the following arguments $EV_ID $EV_TYPE $EV_INT $EV_LNMBA $EV_RADDR $EV_RNBMA int_table"
echo "-u user to own the sock"
echo "-g group to own the sock"
echo "-r send rejection (testing)"
echo "-l logfile to record conversation with nhrpd"
echo "-d daemonize"
exit 1
}
declare -A EXECARR
declare -A TABLEARR
declare -Ag NHRPEVENT
SOCK="/var/run/frr/nhrp.sock"
USER="frr"
GROUP="frr"
DAEMON=0
j=0
RESULT="accept"
while getopts rds:i:u:g:l:t:e: opt; do
case "$opt" in
d)
DAEMON=1
;;
s)
SOCK="$OPTARG"
;;
i)
INTARR[((j++))]="$OPTARG"
;;
e)
if [[ "$j" == "0" ]] || [[ "${INTARR[((j-1))]}" == "" ]]; then
echo "execute argument must follow interface argument"
usage
fi
EXECARR["${INTARR[((j-1))]}"]="$OPTARG"
;;
t)
if [[ "$j" == "0" ]] || [[ "${INTARR[((j-1))]}" == "" ]]; then
echo "execute argument must follow interface argument"
usage
fi
TABLEARR["${INTARR[((j-1))]}"]="$OPTARG"
;;
u)
USER="$OPTARG"
;;
g)
GROUP="$OPTARG"
;;
r)
RESULT="reject"
;;
l)
EVLOGFILE="${OPTARG}"
;;
esac;
done
if [[ "$EVLOGFILE" != "" ]]; then
if [[ ! -w "${EVLOGFILE}" ]]; then
touch "$EVLOGFILE" || ( echo "Cannot write to logfile $EVLOGFILE" ; usage )
fi
echo -e "PROG: $0 Startup\nPROG: Arguments $*" >> $EVLOGFILE
fi
function mainloop()
{
if [[ "$EVLOGFILE" != "" ]]; then
echo -e "PROG: `date -R`\nPROG: Starting mainloop" >> $EVLOGFILE
fi
coproc socat - UNIX-LISTEN:$SOCK,unlink-early,setuid-early=$USER,unlink-close=0 || exit 1
test -S $SOCK && chown $USER:$GROUP $SOCK
OLDIFS="$IFS"
TABLE="table "
while read -r S; do
if [[ "$EVLOGFILE" != "" ]]; then
echo "IN: $S" >> $EVLOGFILE
fi
if [[ "$S" == "" ]]; then
if [[ "${NHRPEVENT[$EV_ID]}" != "" ]]; then
OUTMSG="eventid=${NHRPEVENT[$EV_ID]}\nresult=$RESULT\n"
echo -e "$OUTMSG" >&"${COPROC[1]}"
if [[ "$EVLOGFILE" != "" ]]; then
echo -e "OUT:\n${OUTMSG}" >> $EVLOGFILE;
fi
fi
for((i=0;i<${#INTARR[@]};i++)); do
if [[ "${NHRPEVENT[$EV_INT]}" == "" ]]; then break; fi
if [[ "${INTARR[$i]}" != "${NHRPEVENT[$EV_INT]}" ]]; then continue; fi
EVINT="${NHRPEVENT[$EV_INT]}"
if [[ "${NHRPEVENT[$EV_RADDR]}" == "" ]]; then break; fi
if [[ "${NHRPEVENT[$EV_RNBMA]}" == "" ]]; then break; fi
if [[ "${NHRPEVENT[$EV_TYPE]}" != "dynamic" ]]; then break; fi
INTEXEC=${EXECARR["$EVINT"]}
INTABLE=${TABLEARR["$EVINT"]}
unset CMD
unset CMDEND
CMDADD="ip neigh add "
CMDREPL="ip neigh replace"
CMDBEG="$CMDADD"
if [[ "$INTEXEC" != "" ]]; then
CMD="$INTEXEC ${NHRPEVENT[$EV_ID]:-nil}"
CMD="$CMD ${NHRPEVENT[$EV_TYPE]:-nil}"
CMD="$CMD ${NHRPEVENT[$EV_INT]:-nil}"
CMD="$CMD ${NHRPEVENT[$EV_LNBMA]:-nil}"
CMD="$CMD ${NHRPEVENT[$EV_RADDR]:-nil}"
CMD="$CMD ${NHRPEVENT[$EV_RNBMA]:-nil}"
CMD="$CMD ${INTABLE:-nil}"
unset CMDBEG
else
CMDTAB="${INTABLE:+${TABLE}${INTABLE}}"
CMDEND="$CMDEND ${NHRPEVENT[$EV_RADDR]} dev $EVINT lladdr ${NHRPEVENT[$EV_RNBMA]} nud noarp"
CMD="$CMDEND"
fi
unset CMDTAB
for ((k=0;k<2;k++)); do
for ((l=0;l<2;l++)); do
if [[ "$EVLOGFILE" != "" ]]; then
echo "PROG: Executing $CMD" >> $EVLOGFILE
CMDOUT=`$CMDBEG $CMD $CMDTAB 2>&1`
CMDRET="$?"
if [[ "$CMDOUT" != "" ]]; then
echo "PROG: Execution output: $CMDOUT" >> $EVLOGFILE
fi
else
$CMDBEG $CMD $CMDTAB
fi
if [[ "$CMDTAB" == "" ]] || [[ "$INTEXEC" != "" ]]; then break; fi
done
if [[ "$INTEXEC" != "" ]] || [[ "$CMDRET" == "0" ]]; then
break
fi
CMDBEG="$CMDREPL"
done
break
done
unset NHRPEVENT
declare -Ag NHRPEVENT
continue
continue;
fi
IFS="${IFS}="
SA=($S)
IFS="$OLDIFS"
eval NHRPEVENT[${SA[0]}]="\"${SA[1]}\""
done <&"${COPROC[0]}"
if [[ "$COPROC_PID" != "" ]]; then kill "$COPROC_PID"; fi
}
while true; do
mainloop $*
if [[ "$DAEMON" == "0" ]]; then
break;
fi
sleep 10
done
|