summaryrefslogtreecommitdiffstats
path: root/filter/ulogd_filter_IFINDEX.c
blob: fbe8ccfc497cdd3f70d5f5534ca39ca558a62c6d (plain)
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
/* ulogd_filter_IFINDEX.c, Version $Revision: 1500 $
 *
 * ulogd interpreter plugin for ifindex to ifname conversion
 *
 * (C) 2005 by Harald Welte <laforge@gnumonks.org>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License version 2 
 *  as published by the Free Software Foundation
 *
 *  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; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * $Id: ulogd_filter_IFINDEX.c 1500 2005-10-03 16:54:02Z laforge $
 *
 */

#include <stdio.h>
#include <stdlib.h>
#include <ulogd/ulogd.h>

#include "rtnl.h"
#include "iftable.h"

static struct ulogd_key ifindex_keys[] = {
	{ 
		.type = ULOGD_RET_STRING,
		.flags = ULOGD_RETF_NONE,
		.name = "oob.in", 
	},
	{ 
		.type = ULOGD_RET_STRING,
		.flags = ULOGD_RETF_NONE,
		.name = "oob.out", 
	},
};

static struct ulogd_key ifindex_inp[] = {
	{ 
		.type = ULOGD_RET_UINT32,
		.name = "oob.ifindex_in", 
	},
	{
		.type = ULOGD_RET_UINT32,
		.name = "oob.ifindex_out",
	},
};

static int interp_ifindex(struct ulogd_pluginstance *pi)
{
	struct ulogd_key *ret = pi->output;
	struct ulogd_key *inp = pi->input;

	ret[0].u.value.ptr = ifindex_2name(inp[0].u.source->u.value.ui32);
	ret[0].flags |= ULOGD_RETF_VALID;
	ret[1].u.value.ptr = ifindex_2name(inp[1].u.source->u.value.ui32);
	ret[1].flags |= ULOGD_RETF_VALID;

	return 0;
}

/* we only need one global static cache of ifindex to ifname mappings, 
 * so all state is global (as opposed to per-instance local state in almost
 * all other plugins */
static struct ulogd_fd rtnl_fd = { .fd = -1 };
static int rtnl_users;

static int rtnl_read_cb(int fd, unsigned int what, void *param)
{
	if (!(what & ULOGD_FD_READ))
		return 0;

	rtnl_receive();
}

static int ifindex_start(struct ulogd_pluginstance *upi)
{
	int rc;

	/* if we're already initialized, inc usage count and exit */
	if (rtnl_fd.fd >= 0) {
		rtnl_users++;
		return 0;
	}

	/* if we reach here, we need to initialize */
	rtnl_fd.fd = rtnl_init();
	if (rtnl_fd.fd < 0)
		return rtnl_fd.fd;

	rc = iftable_init();
	if (rc < 0)
		goto out_rtnl;

	rtnl_fd.when = ULOGD_FD_READ;
	rtnl_fd.cb = &rtnl_read_cb;
	rc = ulogd_register_fd(&rtnl_fd);
	if (rc < 0)
		goto out_iftable;

	rtnl_users++;
	return 0;

out_iftable:
	iftable_fini();
out_rtnl:
	rtnl_fini();
	rtnl_fd.fd = -1;
	return rc;
}

static int ifindex_fini(struct ulogd_pluginstance *upi)
{
	if (--rtnl_users == 0) {
		ulogd_unregister_fd(&rtnl_fd);
		iftable_fini();
		rtnl_fini();
		rtnl_fd.fd = -1;
	}

	return 0;
}

static struct ulogd_plugin ifindex_plugin = {
	.name = "IFINDEX",
	.input = {
		.keys = ifindex_inp,
		.num_keys = ARRAY_SIZE(ifindex_inp),
		.type = ULOGD_DTYPE_PACKET,
		},
	.output = {
		.keys = ifindex_keys,
		.num_keys = ARRAY_SIZE(ifindex_keys),
		.type = ULOGD_DTYPE_PACKET,
		},
	.interp = &interp_ifindex,

	.start = &ifindex_start,
	.stop = &ifindex_fini,
};

void __attribute__ ((constructor)) init(void);

void init(void)
{
	ulogd_register_plugin(&ifindex_plugin);
}