xref: /freebsd/libexec/rc/rc.d/ipfw_netflow (revision 06c3fb2749bda94cb5201f81ffdb8fa6c3161b2e)
1#!/bin/sh
2#
3#
4
5# PROVIDE: ipfw_netflow
6# REQUIRE: ipfw
7# KEYWORD: nojailvnet
8
9. /etc/rc.subr
10. /etc/network.subr
11
12name="ipfw_netflow"
13desc="firewall, ipfw, netflow"
14rcvar="${name}_enable"
15start_cmd="${name}_start"
16stop_cmd="${name}_stop"
17start_precmd="${name}_test"
18status_cmd="${name}_status"
19required_modules="ipfw ng_netflow ng_ipfw"
20extra_commands="status"
21
22: ${ipfw_netflow_hook:=9995}
23: ${ipfw_netflow_rule:=01000}
24: ${ipfw_netflow_ip:=127.0.0.1}
25: ${ipfw_netflow_port:=9995}
26: ${ipfw_netflow_version:=}
27
28ipfw_netflow_test()
29{
30    if [ "${ipfw_netflow_version}" != "" ] && [ "${ipfw_netflow_version}" != 9 ]; then
31	err 1 "Unknown netflow version \'${ipfw_netflow_version}\'"
32    fi
33    case "${ipfw_netflow_hook}" in
34	[!0-9]*)
35	    err 1 "Bad value \"${ipfw_netflow_hook}\": Hook must be numerical"
36    esac
37    case "${ipfw_netflow_rule}" in
38	[!0-9]*)
39	    err 1 "Bad value \"${ipfw_netflow_rule}\": Rule number must be numerical"
40    esac
41}
42
43ipfw_netflow_is_running()
44{
45	ngctl show netflow: > /dev/null 2>&1 && return 0 || return 1
46}
47
48ipfw_netflow_status()
49{
50	ipfw_netflow_is_running && echo "ipfw_netflow is active" || echo "ipfw_netflow is not active"
51}
52
53ipfw_netflow_start()
54{
55	ipfw_netflow_is_running && err 1 "ipfw_netflow is already active"
56	ipfw add ${ipfw_netflow_rule} ngtee ${ipfw_netflow_hook} ip from any to any ${ipfw_netflow_fib:+fib ${ipfw_netflow_fib}}
57	ngctl -f - <<-EOF
58	mkpeer ipfw: netflow ${ipfw_netflow_hook} iface0
59	name ipfw:${ipfw_netflow_hook} netflow
60	mkpeer netflow: ksocket export${ipfw_netflow_version} inet/dgram/udp
61	msg netflow: setdlt {iface=0 dlt=12}
62	name netflow:export${ipfw_netflow_version} netflow_export
63	msg netflow:export${ipfw_netflow_version} connect inet/${ipfw_netflow_ip}:${ipfw_netflow_port}
64EOF
65}
66
67ipfw_netflow_stop()
68{
69    ipfw_netflow_is_running || err 1 "ipfw_netflow is not active"
70    ngctl shutdown netflow:
71    ipfw delete ${ipfw_netflow_rule}
72}
73
74load_rc_config $name
75
76run_rc_command $*
77