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
|
/* ulogd_filter_IP2STR.c
*
* ulogd interpreter plugin for internal IP storage format to string conversion
*
* (C) 2008 by Eric Leblond <eric@inl.fr>
*
* Based on ulogd_filter_IFINDEX.c 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
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <arpa/inet.h>
#include <ulogd/ulogd.h>
#include <netinet/if_ether.h>
enum input_keys {
KEY_OOB_FAMILY,
KEY_OOB_PROTOCOL,
KEY_IP_SADDR,
START_KEY = KEY_IP_SADDR,
KEY_IP_DADDR,
KEY_ORIG_IP_SADDR,
KEY_ORIG_IP_DADDR,
KEY_REPLY_IP_SADDR,
KEY_REPLY_IP_DADDR,
KEY_ARP_SPA,
KEY_ARP_TPA,
MAX_KEY = KEY_ARP_TPA,
};
static struct ulogd_key ip2str_inp[] = {
[KEY_OOB_FAMILY] = {
.type = ULOGD_RET_UINT8,
.flags = ULOGD_RETF_NONE,
.name = "oob.family",
},
[KEY_OOB_PROTOCOL] = {
.type = ULOGD_RET_UINT16,
.flags = ULOGD_RETF_NONE,
.name = "oob.protocol",
},
[KEY_IP_SADDR] = {
.type = ULOGD_RET_IPADDR,
.flags = ULOGD_RETF_NONE|ULOGD_KEYF_OPTIONAL,
.name = "ip.saddr",
},
[KEY_IP_DADDR] = {
.type = ULOGD_RET_IPADDR,
.flags = ULOGD_RETF_NONE|ULOGD_KEYF_OPTIONAL,
.name = "ip.daddr",
},
[KEY_ORIG_IP_SADDR] = {
.type = ULOGD_RET_IPADDR,
.flags = ULOGD_RETF_NONE|ULOGD_KEYF_OPTIONAL,
.name = "orig.ip.saddr",
},
[KEY_ORIG_IP_DADDR] = {
.type = ULOGD_RET_IPADDR,
.flags = ULOGD_RETF_NONE|ULOGD_KEYF_OPTIONAL,
.name = "orig.ip.daddr",
},
[KEY_REPLY_IP_SADDR] = {
.type = ULOGD_RET_IPADDR,
.flags = ULOGD_RETF_NONE|ULOGD_KEYF_OPTIONAL,
.name = "reply.ip.saddr",
},
[KEY_REPLY_IP_DADDR] = {
.type = ULOGD_RET_IPADDR,
.flags = ULOGD_RETF_NONE|ULOGD_KEYF_OPTIONAL,
.name = "reply.ip.daddr",
},
[KEY_ARP_SPA] = {
.type = ULOGD_RET_IPADDR,
.flags = ULOGD_RETF_NONE|ULOGD_KEYF_OPTIONAL,
.name = "arp.saddr",
},
[KEY_ARP_TPA] = {
.type = ULOGD_RET_IPADDR,
.flags = ULOGD_RETF_NONE|ULOGD_KEYF_OPTIONAL,
.name = "arp.daddr",
},
};
static struct ulogd_key ip2str_keys[] = {
{
.type = ULOGD_RET_STRING,
.name = "ip.saddr.str",
.cim_name = "src_ip",
},
{
.type = ULOGD_RET_STRING,
.name = "ip.daddr.str",
.cim_name = "dest_ip",
},
{
.type = ULOGD_RET_STRING,
.name = "orig.ip.saddr.str",
.cim_name = "src_ip",
},
{
.type = ULOGD_RET_STRING,
.name = "orig.ip.daddr.str",
.cim_name = "dest_ip",
},
{
.type = ULOGD_RET_STRING,
.name = "reply.ip.saddr.str",
},
{
.type = ULOGD_RET_STRING,
.name = "reply.ip.daddr.str",
},
{
.type = ULOGD_RET_STRING,
.name = "arp.saddr.str",
},
{
.type = ULOGD_RET_STRING,
.name = "arp.daddr.str",
},
};
static char ipstr_array[MAX_KEY - START_KEY + 1][INET6_ADDRSTRLEN];
static int ip2str(struct ulogd_key *inp, int index, int oindex)
{
char family = ikey_get_u8(&inp[KEY_OOB_FAMILY]);
char convfamily = family;
if (family == AF_BRIDGE) {
if (!pp_is_valid(inp, KEY_OOB_PROTOCOL)) {
ulogd_log(ULOGD_NOTICE,
"No protocol inside AF_BRIDGE packet\n");
return ULOGD_IRET_ERR;
}
switch (ikey_get_u16(&inp[KEY_OOB_PROTOCOL])) {
case ETH_P_IPV6:
convfamily = AF_INET6;
break;
case ETH_P_IP:
convfamily = AF_INET;
break;
case ETH_P_ARP:
convfamily = AF_INET;
break;
default:
ulogd_log(ULOGD_NOTICE,
"Unknown protocol inside AF_BRIDGE packet\n");
return ULOGD_IRET_ERR;
}
}
switch (convfamily) {
uint32_t ip;
case AF_INET6:
inet_ntop(AF_INET6,
ikey_get_u128(&inp[index]),
ipstr_array[oindex], sizeof(ipstr_array[oindex]));
break;
case AF_INET:
ip = ikey_get_u32(&inp[index]);
inet_ntop(AF_INET, &ip,
ipstr_array[oindex], sizeof(ipstr_array[oindex]));
break;
default:
/* TODO error handling */
ulogd_log(ULOGD_NOTICE, "Unknown protocol family\n");
return ULOGD_IRET_ERR;
}
return ULOGD_IRET_OK;
}
static int interp_ip2str(struct ulogd_pluginstance *pi)
{
struct ulogd_key *ret = pi->output.keys;
struct ulogd_key *inp = pi->input.keys;
int i;
int fret;
/* Iter on all addr fields */
for (i = START_KEY; i <= MAX_KEY; i++) {
if (pp_is_valid(inp, i)) {
fret = ip2str(inp, i, i - START_KEY);
if (fret != ULOGD_IRET_OK)
return fret;
okey_set_ptr(&ret[i - START_KEY],
ipstr_array[i-START_KEY]);
}
}
return ULOGD_IRET_OK;
}
static struct ulogd_plugin ip2str_plugin = {
.name = "IP2STR",
.input = {
.keys = ip2str_inp,
.num_keys = ARRAY_SIZE(ip2str_inp),
.type = ULOGD_DTYPE_PACKET | ULOGD_DTYPE_FLOW,
},
.output = {
.keys = ip2str_keys,
.num_keys = ARRAY_SIZE(ip2str_keys),
.type = ULOGD_DTYPE_PACKET | ULOGD_DTYPE_FLOW,
},
.interp = &interp_ip2str,
.version = VERSION,
};
void __attribute__ ((constructor)) init(void);
void init(void)
{
ulogd_register_plugin(&ip2str_plugin);
}
|