nlmsg.c
/*
* (C) 2015 by Ken-ichirou MATSUZAWA <chamas@h4.dion.ne.jp>
*
* 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.
*/
#include <arpa/inet.h>
#include <libmnl/libmnl.h>
#include <libnetfilter_log/libnetfilter_log.h>
#include <errno.h>
#include "internal.h"
struct nlmsghdr *
nflog_nlmsg_put_header(char *buf, uint8_t type, uint8_t family, uint16_t gnum)
{
struct nlmsghdr *nlh = mnl_nlmsg_put_header(buf);
struct nfgenmsg *nfg;
nlh->nlmsg_type = (NFNL_SUBSYS_ULOG << 8) | type;
nlh->nlmsg_flags = NLM_F_REQUEST;
nfg = mnl_nlmsg_put_extra_header(nlh, sizeof(*nfg));
nfg->nfgen_family = family;
nfg->version = NFNETLINK_V0;
nfg->res_id = htons(gnum);
return nlh;
}
int nflog_attr_put_cfg_mode(struct nlmsghdr *nlh, uint8_t mode, uint32_t range)
{
struct nfulnl_msg_config_mode nfmode = {
.copy_mode = mode,
.copy_range = htonl(range)
};
mnl_attr_put(nlh, NFULA_CFG_MODE, sizeof(nfmode), &nfmode);
/* it may returns -1 in future */
return 0;
}
int nflog_attr_put_cfg_cmd(struct nlmsghdr *nlh, uint8_t cmd)
{
struct nfulnl_msg_config_cmd nfcmd = {
.command = cmd
};
mnl_attr_put(nlh, NFULA_CFG_CMD, sizeof(nfcmd), &nfcmd);
/* it may returns -1 in future */
return 0;
}
static int nflog_parse_attr_cb(const struct nlattr *attr, void *data)
{
const struct nlattr **tb = data;
int type = mnl_attr_get_type(attr);
/* skip unsupported attribute in user-space */
if (mnl_attr_type_valid(attr, NFULA_MAX) < 0)
return MNL_CB_OK;
switch(type) {
case NFULA_HWTYPE: /* hardware type */
case NFULA_HWLEN: /* hardware header length */
if (mnl_attr_validate(attr, MNL_TYPE_U16) < 0)
return MNL_CB_ERROR;
break;
case NFULA_MARK: /* __u32 nfmark */
case NFULA_IFINDEX_INDEV: /* __u32 ifindex */
case NFULA_IFINDEX_OUTDEV: /* __u32 ifindex */
case NFULA_IFINDEX_PHYSINDEV: /* __u32 ifindex */
case NFULA_IFINDEX_PHYSOUTDEV: /* __u32 ifindex */
case NFULA_UID: /* user id of socket */
case NFULA_SEQ: /* instance-local sequence number */
case NFULA_SEQ_GLOBAL: /* global sequence number */
case NFULA_GID: /* group id of socket */
case NFULA_CT_INFO: /* enum ip_conntrack_info */
if (mnl_attr_validate(attr, MNL_TYPE_U32) < 0)
return MNL_CB_ERROR;
break;
case NFULA_PACKET_HDR:
if (mnl_attr_validate2(attr, MNL_TYPE_UNSPEC,
sizeof(struct nfulnl_msg_packet_hdr)) < 0) {
return MNL_CB_ERROR;
}
break;
case NFULA_TIMESTAMP: /* nfulnl_msg_packet_timestamp */
if (mnl_attr_validate2(attr, MNL_TYPE_UNSPEC,
sizeof(struct nfulnl_msg_packet_timestamp)) < 0) {
return MNL_CB_ERROR;
}
break;
case NFULA_HWADDR: /* nfulnl_msg_packet_hw */
if (mnl_attr_validate2(attr, MNL_TYPE_UNSPEC,
sizeof(struct nfulnl_msg_packet_hw)) < 0) {
return MNL_CB_ERROR;
}
break;
case NFULA_PREFIX: /* string prefix */
if (mnl_attr_validate(attr, MNL_TYPE_NUL_STRING) < 0)
return MNL_CB_ERROR;
break;
case NFULA_HWHEADER: /* hardware header */
case NFULA_PAYLOAD: /* opaque data payload */
case NFULA_CT: /* nf_conntrack_netlink.h */
break;
}
tb[type] = attr;
return MNL_CB_OK;
}
int nflog_nlmsg_parse(const struct nlmsghdr *nlh, struct nlattr **attr)
{
return mnl_attr_parse(nlh, sizeof(struct nfgenmsg),
nflog_parse_attr_cb, attr);
}
int nflog_nlmsg_snprintf(char *buf, size_t bufsiz, const struct nlmsghdr *nlh,
struct nlattr **attr, enum nflog_output_type type,
uint32_t flags)
{
/* This is a hack to re-use the existing old API code. */
struct nflog_data nfad = {
.nfa = (struct nfattr **)&attr[1],
};
int ret;
switch (type) {
case NFLOG_OUTPUT_XML:
ret = nflog_snprintf_xml(buf, bufsiz, &nfad, flags);
break;
default:
ret = -1;
errno = EOPNOTSUPP;
break;
}
return ret;
}
nflog_snprintf_xml
int nflog_snprintf_xml(char *buf, size_t rem, struct nflog_data *tb, int flags)
Definition libnetfilter_log.c:1066
nflog_nlmsg_snprintf
int nflog_nlmsg_snprintf(char *buf, size_t bufsiz, const struct nlmsghdr *nlh, struct nlattr **attr, enum nflog_output_type type, uint32_t flags)
Definition nlmsg.c:196
nflog_nlmsg_parse
int nflog_nlmsg_parse(const struct nlmsghdr *nlh, struct nlattr **attr)
Definition nlmsg.c:162
nflog_attr_put_cfg_cmd
int nflog_attr_put_cfg_cmd(struct nlmsghdr *nlh, uint8_t cmd)
Definition nlmsg.c:84
nflog_attr_put_cfg_mode
int nflog_attr_put_cfg_mode(struct nlmsghdr *nlh, uint8_t mode, uint32_t range)
Definition nlmsg.c:64
nflog_nlmsg_put_header
struct nlmsghdr * nflog_nlmsg_put_header(char *buf, uint8_t type, uint8_t family, uint16_t gnum)
Definition nlmsg.c:39