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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
|
#compdef nft
# ------------------------------------------------------------------------------
# Description
# -----------
#
# Completion script for nft 0.9.0 (https://www.netfilter.org/projects/nftables/index.html).
#
# ------------------------------------------------------------------------------
# Authors
# -------
#
# * Markus Richter ( https://github.com/mqus , <mqus@disroot.org>)
#
# ------------------------------------------------------------------------------
_nft(){
local -a rules states prev args families options descriptors
local state="start" line nextstate cmd_obj cmd_subcmd cmd_fam cmd_tab cmd_chain #curcontext="$curcontext"
options=(
'(-)'{-h,--help}'[show help]' \
'(-)'{-v,--version}'[print version information]' \
"(-i --interactive)"{-i,--interactive}'[read input from interactive CLI]: :->end' \
"(-f --file)"{-f,--file}'[read input from <filename>]:nftables rule file:_files' \
'(-c --check -n --numeric -N)'{-c,--check}"[check command's validity without actually applying the changes]" \
'(-j --json)'{-j,--json}'[format output in json]' \
'(-c --check -N)*'{-n,--numeric}'[can be specified up to 3 times, Shows 1:network addresses(default behaviour), 2:internet services (port numbers) and 3:protocols, user IDs, and group IDs numerically]' \
'(-s --stateless)'{-s,--stateless}'[omit stateful information of ruleset]' \
'(-N -n --numeric -c --check)'-N'[translate IP addresses to names]' \
'(-a --handle)'{-a,--handle}'[output rule handle]' \
'(-e --echo)'{-e,--echo}'[echo what has been added, inserted or replaced]' \
{-I,--includepath}'[add specified directory to the paths searched for include files]:include directory [/usr/share]:include directory:_directories'
)
# start a state machine. The state is modified by _arguments if the
# current argument (descriptors) cannot be completed. Each state has to define is successive state and the
# 'descriptors' for _arguments, which essentially tells _arguments how to complete
local _i=0
while true;do
(( _i+=1 ))
#Guard for endless loops
[[ $_i -gt 100 ]] && return 1
descriptors=()
nextstate="end"
case $state in
(start)
##if line is empty (at the start) or ends with semicolon, autocomplete subcommands,
# else if we are after a space,complete a semicolon (end the current nft command) and start anew
if [[ $line[1] = "" || $line[1] =~ ';$' ]] ; then
descriptors=( ":: :_nft_subcommands" )
nextstate="category"
else
if [[ $words =~ ' $' ]]; then
descriptors=(':: :(\;)')
else
descriptors=(':argument: ')
fi
nextstate="start"
fi
;;
(category)
case $line[1] in
(add | list | flush | delete | create | rename | insert | replace | reset)
descriptors=( ":: :_nft_${line[1]}" )
nextstate=$line[1]
;;
(monitor)
descriptors=( ":: : _nft_mon_filter" )
nextstate="mon1"
;;
(export)
descriptors=( ":: :(ruleset)" ":: :_nft_out_format" )
nextstate="preend"
;;
(describe)
descriptors=( ":expression: ")
nextstate="start" #x restart
;;
(*)
return 1;
;;
esac
#descriptors=( "(ruleset)" )
#nextstate="end"
;;
(mon1)
case $line[1] in
(new | destroy)
# descriptors=( ":: :_nft_mon_keywords" ":: :_nft_out_format")
descriptors=( ":: : _nft_mon_keywords")
nextstate="mon1"
;;
(tables | chains | sets | rules | elements | ruleset)
descriptors=( ":: : _nft_out_format")
nextstate="preend"
;;
esac
;;
#all completions for create and insert match with the completions of add
(create | insert)
state="add"
;|
#all completions for reset and flush match with the completions of list
(reset | flush)
state="list"
;|
#(add(^table)/create(^table)/delete/flush(^ruleset)/insert/list(^ruleset)/rename/replace)[family]table
(reset | delete | insert | rename | replace | add | create | flush | list)
if [[ $state = "add" && $line[1] = "table" ]]; then
descriptors=( ":: :_nft_families" ":table name:")
nextstate="start" #x restart
elif [[ $state = "list" && ( $line[1] = "ruleset" || $line[1] = "tables" ) ]]; then
descriptors=( ":: :_nft_families")
nextstate="start" #x restart
elif [[ $state = "delete" && $line[1] = "table" ]]; then
descriptors=(": : _nft_table all-handle")
nextstate="tcomplete-delete-table"
else
cmd_obj=$line[1]
cmd_subcmd=$state
descriptors=(": : _nft_table all")
nextstate="tcomplete"
fi
;;
(tcomplete-delete-table)
# if only a family was completed, complete the table name.
case $line[1] in
(arp | bridge | inet | ip | ip6 | netdev)
descriptors=(": : _nft_table ${line[1]}-handle")
cmd_fam=$line[1]
;;
# if 'handle' was completed, complete the handle number.
(handle)
descriptors=(": : _nft_table_handle_all " )
;;
# else, complete nothing and go to the next state. default family is 'ip'
(*)
descriptors=()
cmd_fam="ip"
;;
esac
nextstate="delete-table"
;;
(tcomplete)
# if only a family was completed, complete the table name.
case $line[1] in
(arp | bridge | inet | ip | ip6 | netdev)
descriptors=(": : _nft_table ${line[1]}")
cmd_fam=$line[1]
;;
# else, complete nothing and go to the next state. default family is 'ip'
(*)
descriptors=()
cmd_fam="ip"
;;
esac
nextstate="$cmd_subcmd-$cmd_obj"
;;
(list-table)
descriptors=(":: :(\;)")
nextstate="start"
;;
(delete-table)
#if family AND handle were input, complete handle number for given family.
if [[ $line[1] == "handle" ]]; then
descriptors=(":table handle: _nft_table_handle $cmd_fam" )
else
descriptors=()
fi
nextstate="start"
;;
(delete-chain | delete-set | delete-quota | delete-counter | delete-ct\\ helper)
cmd_tab=$line[1]
descriptors=(": : _nft_object $cmd_fam $cmd_tab $cmd_obj true")
nextstate="delete-obj-handle"
;;
(delete-obj-handle)
if [[ $line[1] == "handle" ]]; then
descriptors=(": : _nft_object_handle $cmd_fam $cmd_tab $cmd_obj")
else
descriptors=(": :(\;)")
fi
nextstate="start"
;;
(add-chain)
descriptors=(":chain name:")
nextstate="start"
;;
(rename-chain)
cmd_tab=$line[1]
descriptors=(": : _nft_object $cmd_fam $cmd_tab chain false")
nextstate="add-chain"
;;
(replace-rule | delete-rule)
cmd_tab=$line[1]
descriptors=(": : _nft_object $cmd_fam $cmd_tab chain false")
nextstate="repdel-rule"
;;
(repdel-rule)
cmd_chain=$line[1]
descriptors=(": :(handle)" ": : _nft_rule_handle $cmd_fam $cmd_tab ${line[1]}")
if [[ $cmd_subcmd = "replace" ]];then
nextstate="rule-stmt"
else
nextstate="start"
fi
;;
(add-rule)
cmd_tab=$line[1]
descriptors=(": : _nft_object $cmd_fam $cmd_tab chain false")
nextstate="add-rule-2"
;;
(add-rule-2)
cmd_chain=$line[1]
descriptors=(": :(handle index position)")
nextstate="add-rule-3"
;;
(add-rule-3)
case $line[1] in
(index | position)
descriptors=(":${line[1]}:")
;;
(handle)
descriptors=(": : _nft_rule_handle $cmd_fam $cmd_tab $cmd_chain")
;;
(*)
descriptors=()
;;
esac
nextstate="rule-stmt"
;;
(rule-stmt)
#TODO
# _nft_rule $cmd_fam $cmd_tab $cmd_chain\
# && return 0;
descriptors=":expression: "
nextstate="start"
;;
(list-set | list-map | delete-map | list-chain | list-flowtable | delete-flowtable | list-ct\\ helper | list-counter | list-quota | list-meter)
cmd_tab=$line[1]
descriptors=(": : _nft_object $cmd_fam $cmd_tab $cmd_obj false")
nextstate="start"
;;
(add-element | delete-element)
cmd_tab=$line[1]
descriptors=(": : _nft_object $cmd_fam $cmd_tab '\(map\|set\)' false")
nextstate="start"
;;
#TODO:
#(add-set | add-map)
#(add-flowtable)
#("add-ct\ helper")
#(add-counter)
#(add-quota)
(*)
return 1;
;;
esac
_arguments -C -s \
"${options[@]}" \
"${descriptors[@]}" \
"*:: :->$nextstate" \
&& return 0;
done
} # end _nft
_nft_subcommands(){
local commands=(
'add:add a table, chain, rule, set, map, or object'
'list:list a ruleset, table, chain, set, map, or object'
'flush:flush (delete everything from) a ruleset, table, chain, set, or map'
'export:print the ruleset in a machine readable format (json or xml)'
'delete:delete a table, chain, rule, set, element, map, or object'
'create:similar to add but returns an error for existing chain'
'rename:rename the specified chain'
'insert:similar to the add command, but the rule is prepended to the beginning of the chain or before the rule at the given position'
'replace:similar to the add command, but replaces the specified rule'
'reset:list-and-reset stateful object'
'monitor:listen to Netlink events'
'describe:show information about the type of an expression and its data type'
)
_describe -t commands 'nft subcommand' commands "$@"
}
_nft_mon_filter(){
local monitor_filters=(
'new:show only events of created objects'
'destroy:show only events of deleted objects'
)
_describe -t monitor_filters 'nft monitor' monitor_filters -J monitor_filters "$@"
_nft_mon_keywords
}
_nft_mon_keywords(){
local monitor_keywords=(
'tables:show table events'
'chains:show chain events'
'sets:show set events'
'rules:show rule events'
'elements:show only events of element objects'
'ruleset:show ruleset events, such as table, chain, rule, set, counters and quotas'
)
_describe -t monitor_keywords 'nft monitor' monitor_keywords -J monitor_keywords "$@"
_nft_out_format
}
_nft_out_format(){
local monitor_format=(
'json:format output to JSON'
'xml:format output to XML'
)
_describe -t monitor_format "output format" monitor_format -J monitor_format "$@"
}
_nft_add(){
local commands=(
'table:add a new table'
'flowtable:add a new flowtable'
'chain:add a chain to a table'
'rule:add a rule to an existing chain'
'set:add a set to a table'
'map:add a map to a table'
'element:add one or multiple element(s) to a set or map'
'ct\ helper:add a ct helper to a table'
'counter:add a named counter to a table'
'quota:add a named quota helper to a table'
)
_describe -t commands 'nft add' commands "$@"
}
_nft_create(){
local commands=(
"table:add a table, but return an error if it already exists"
"chain:add a chain to a table, but return an error if it already exists"
"flowtable:add a flowtable, but return an error if it already exists"
)
_describe -t commands 'nft create' commands "$@"
}
_nft_delete(){
local commands=(
"table:delete the specified table"
"chain:delete the specified chain, chain must be empty and mustn't be used as jump target"
"rule:delete the specified rule, rule must be referable to by a handle"
"set:delete the specified set"
"map:delete the specified map"
"element:delete element(s) from the specified set/map"
"flowtable:delete the specified flowtable"
"ct\ helper:delete the specified ct helper"
"counter:delete the specified counter"
"quota:delete the specified quota"
)
_describe -t commands 'nft delete' commands "$@"
}
_nft_flush(){
local commands=(
"ruleset:clear the whole ruleset, including removing all tables and containing objects"
"table:flush all chains and rules of the specified table"
"chain:flush all rules of the specified chain"
"set:remove all elements from the specified set"
"map:remove all elements from the specified map"
)
_describe -t commands 'nft flush' commands "$@"
}
_nft_insert(){
local commands=(
"rule:prepend a rule to the beginning of the chain or before the rule with the given handle"
)
_describe -t commands 'nft insert' commands "$@"
}
_nft_list(){
local commands=(
"ruleset:print the ruleset in human-readable format"
"tables:list all tables (undocumented)"
"table:list all chains and rules of the specified table"
"chain:list all rules of the specified chain"
"set:display the elements in the specified set"
"map:display the elements in the specified map"
"flowtable:list all flowtables"
"ct\ helper:display stateful information the ct helper holds"
"counter:display stateful information the counter holds"
"quota:display stateful information the quota holds"
)
_describe -t commands 'nft list' commands "$@"
}
_nft_rename(){
local commands=(
"chain:replace a chain"
)
_describe -t commands 'nft rename' commands "$@"
}
_nft_replace(){
local commands=(
"rule:replace a rule"
)
_describe -t commands 'nft replace' commands "$@"
}
_nft_reset(){
local commands=(
'ct\ helper:reset and list a ct helper to a table'
'counter:reset and list a counter from a table'
'quota:reset and list a quota object a table'
)
_describe -t commands 'nft reset' commands "$@"
}
_nft_families(){
local families=(
"ip:IPv4 address family"
"ip6:IPv6 address family"
"inet:internet (IPv4+IPv6) address family"
"arp:ARP address family, handling IPv4 ARP packets"
"bridge:Bridge address family, handling packets which traverse a bridge device"
"netdev:Netdev address family, handling packets from ingress"
)
_describe -t families 'nft families' families "$@"
}
_nft_table(){
# complete the names of tables and the families of existing tables
#$1 can be: all all-handle <family> <family>-handle
local tables=()
if [[ "$1" =~ "^all" ]]; then
local families=( ${(f)"$(_call_program -p tables nft list tables 2>/dev/null \
| cut -d\ -f2 )"} )
# ip is the default family, search also for table names there
1="${1/all/ip}"
_describe -t families "family" families -J "family"
fi
if [[ "$1" =~ "-handle$" ]]; then
tables=("handle:address the table by handle")
#remove -handle from $1 to be able to complete table names
1="${1/-handle/}"
_describe -t tables "table" tables -V "handle"
fi
case $1 in
(arp | bridge | inet | ip | ip6 | netdev)
tables=( ${(f)"$(_call_program -p tables nft -a list ruleset 2>/dev/null \
| grep '^table '"$1" | sed 's/table // ;s/{ # handle // ;s/\(\S*\) \(\S*\) \(\S*\)/\2:type \1, handle \3/' )"} )
_describe -t tables "table" tables -V "table-name"
;;
esac
}
_nft_table_handle(){
# complete the handles of tables with the specified family (with the table name in the description)
#$1:protocol family
local tables=( ${(f)"$(_call_program -p tables nft -a list ruleset 2>/dev/null \
| grep '^table '"$1" | sed 's/table // ;s/{ # handle // ;s/\(\S*\) \(\S*\) \(\S*\)/\3:\2(type \1)/' )"} )
echo $1 > /tmp/znfttab
_describe -t tables "table handle" tables
}
_nft_table_handle_all(){
# complete the handles of tables of all families (with the table name in the description)
local tables=( ${(f)"$(_call_program -p tables nft -a list ruleset 2>/dev/null \
| grep '^table' | sed 's/table // ;s/{ # handle // ;s/\(\S*\) \(\S*\) \(\S*\)/\3:\2(type \1)/' )"} )
_describe -t tables "table handle" tables
}
_nft_object(){
# complete the names of objects contained directly in a table (with the handle number in the description)
#$1:protocol family
#$2:table
#$3:object type (chain/set/map/flowtable/ct helper/counter/quota/meter)
#$4:include 'handle'?
local objects=( ${(f)"$(_call_program -p objects nft -a list table $1 $2 2>/dev/null\
| grep ""\^\\s\*$3"" | sed 's/\s*'"$3"' // ;s/ { # \(.*\)/:(\1)/' )"} )
if $4 ;then
objects+=( "handle:address $3 by handle")
fi
_describe -t objects "$3" objects
}
_nft_object_handle(){
# complete handles of objects contained directly in a table (with the name in the description)
#$1:protocol family
#$2:table
#$3:object type (chain/set/ct helper/counter/quota)
local handles=( ${(f)"$(_call_program -p handles nft -a list table $1 $2 2>/dev/null\
| grep ""\^\\s\*$3"" | sed 's/\s*'"$3"' // ;s/ { # handle// ;s/\(\S*\) \(\S*\)/\2:\1/' )"} )
_describe -t handles "$3-handle" handles
}
_nft_rule_handle(){
# complete the handles of rules (and put the rule into the description)
#$1:protocol family
#$2:table
#$3:chain name
local rules=( ${(f)"$(_call_program -p nft-rule-handle nft -a list chain $1 $2 $3 2>/dev/null \
|grep -v '^\s*\(table\|chain\|type\|\}\)'|sed 's/^\s*\(.*\) # handle \(\S*\)$/\2:\1/' )"} )
# don't sort those entries alphabetically, so they get shown in the order they are executed in nftables
_describe -t rules "rule" rules -V "rules"
}
#currently, only the `nft` command is covered by this script.
_nft "$@"
|