/*
* (C) 2008-2012 by Pablo Neira Ayuso <pablo@netfilter.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published
* by the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*/
#include <limits.h> /* for INT_MAX */
#include <libmnl/libmnl.h>
#include <string.h>
#include <errno.h>
#include "internal.h"
/**
* \defgroup attr Netlink attribute helpers
*
* Netlink Type-Length-Value (TLV) attribute:
* \verbatim
|<-- 2 bytes -->|<-- 2 bytes -->|<-- variable -->|
-------------------------------------------------
| length | type | value |
-------------------------------------------------
|<--------- header ------------>|<-- payload --->|
\endverbatim
* The payload of the Netlink message contains sequences of attributes that are
* expressed in TLV format.
*
* @{
*/
/**
* mnl_attr_get_type - get type of netlink attribute
* \param attr pointer to netlink attribute
*
* This function returns the attribute type.
*/
EXPORT_SYMBOL(mnl_attr_get_type);
uint16_t mnl_attr_get_type(const struct nlattr *attr)
{
return attr->nla_type & NLA_TYPE_MASK;
}
/**
* mnl_attr_get_len - get length of netlink attribute
* \param attr pointer to netlink attribute
*
* This function returns the attribute length that is the attribute header
* plus the attribute payload.
*/
EXPORT_SYMBOL(mnl_attr_get_len);
uint16_t mnl_attr_get_len(const struct nlattr *attr)
{
return attr->nla_len;
}
/**
* mnl_attr_get_payload_len - get the attribute payload-value length
* \param attr pointer to netlink attribute
*
* This function returns the attribute payload-value length.
*/
EXPORT_SYMBOL(mnl_attr_get_payload_len);
uint16_t mnl_attr_get_payload_len(const struct nlattr *attr)
{
return attr->nla_len - MNL_ATTR_HDRLEN;
}
/**
* mnl_attr_get_payload - get pointer to the attribute payload
* \param attr pointer to netlink attribute
*
* This function return a pointer to the attribute payload.
*/
EXPORT_SYMBOL(mnl_attr_get_payload);
void *mnl_attr_get_payload(const struct nlattr *attr)
{
return (void *)attr + MNL_ATTR_HDRLEN;
}
/**
* mnl_attr_ok - check if there is room for an attribute in a buffer
* \param attr attribute that we want to check if there is room for
* \param len remaining bytes in a buffer that contains the attribute
*
* This function is used to check that a buffer, which is supposed to contain
* an attribute, has enough room for the attribute that it stores, i.e. this
* function can be used to verify that an attribute is neither malformed nor
* truncated.
*
* This function does not set errno in case of error since it is intended
* for iterations. Thus, it returns true on success and false on error.
*
* The len parameter may be negative in the case of malformed messages during
* attribute iteration, that is why we use a signed integer.
*/
EXPORT_SYMBOL(mnl_attr_ok);
bool mnl_attr_ok(const struct nlattr *attr, int len)
{
return len >= (int)sizeof(struct nlattr) &&
attr->nla_len >= sizeof(struct nlattr) &&
(int)attr->nla_len <= len;
}
/**
* mnl_attr_next - get the next attribute in the payload of a netlink message
* \param attr pointer to the current attribute
*
* This function returns a pointer to the next attribute after the one passed
* as parameter. You have to use mnl_attr_ok() to ensure that the next
* attribute is valid.
*/
EXPORT_SYMBOL(mnl_attr_next);
struct nlattr *mnl_attr_next(const struct nlattr *attr)
{
return (struct nlattr *)((void *)attr + MNL_ALIGN(attr->nla_len));
}
/**
* mnl_attr_type_valid - check if the attribute type is valid
* \param attr pointer to attribute to be checked
* \param max maximum attribute type
*
* This function allows to check if the attribute type is higher than the
* maximum supported type. If the attribute type is invalid, this function
* returns -1 and errno is explicitly set. On success, this function returns 1.
*
* Strict attribute checking in user-space is not a good idea since you may
* run an old application with a newer kernel that supports new attributes.
* This leads to backward compatibility breakages in user-space. Better check
* if you support an attribute, if not, skip it.
*/
EXPORT_SYMBOL(mnl_attr_type_valid);
int mnl_attr_type_valid(const struct nlattr *attr, uint16_t max)
{
if (mnl_attr_get_type(attr) > max) {
errno = EOPNOTSUPP;
return -1;
}
return 1;
}
static int __mnl_attr_validate(const struct nlattr *attr,
enum mnl_attr_data_type type, size_t exp_len)
{
uint16_t attr_len = mnl_attr_get_payload_len(attr);
const char *attr_data = mnl_attr_get_payload(attr);
if (attr_len < exp_len) {
errno = ERANGE;
return -1;
}
switch(type) {
case MNL_TYPE_FLAG:
if (attr_len > 0) {
errno = ERANGE;
return -1;
}
break;
case MNL_TYPE_NUL_STRING:
if (attr_len == 0) {
errno = ERANGE;
return -1;
}
if (attr_data[attr_len-1] != '\0') {
errno = EINVAL;
return -1;
}
break;
case MNL_TYPE_STRING:
if (attr_len == 0) {
errno = ERANGE;
return -1;
}
break;
case MNL_TYPE_NESTED:
/* empty nested attributes are OK. */
if (attr_len == 0)
break;
/* if not empty, they must contain one header, eg. flag */
if (attr_len < MNL_ATTR_HDRLEN) {
errno = ERANGE;
return -1;
}
break;
default:
/* make gcc happy. */
break;
}
if (exp_len && attr_len > exp_len) {
errno = ERANGE;
return -1;
}
return 0;
}
static const size_t mnl_attr_data_type_len[MNL_TYPE_MAX] = {
[MNL_TYPE_U8] = sizeof(uint8_t),
[MNL_TYPE_U16] = sizeof(uint16_t),
[MNL_TYPE_U32] = sizeof(uint32_t),
[MNL_TYPE_U64] = sizeof(uint64_t),
[MNL_TYPE_MSECS] = sizeof(uint64_t),
};
/**
* mnl_attr_validate - validate netlink attribute (simplified version)
* \param attr pointer to netlink attribute that we want to validate
* \param type data type (see enum mnl_attr_data_type)
*
* The validation is based on the data type. Specifically, it checks that
* integers (u8, u16, u32 and u64) have enough room for them. This function
* returns -1 in case of error, and errno is explicitly set.
*/
EXPORT_SYMBOL(mnl_attr_validate);
int mnl_attr_validate(const struct nlattr *attr, enum mnl_attr_data_type type)
{
int exp_len;
if (type >= MNL_TYPE_MAX) {
errno = EINVAL;
return -1;
}
exp_len = mnl_attr_data_type_len[type];
return __mnl_attr_validate(attr, type, exp_len);
}
/**
* mnl_attr_validate2 - validate netlink attribute (extended version)
* \param attr pointer to netlink attribute that we want to validate
* \param type attribute type (see enum mnl_attr_data_type)
* \param exp_len expected attribute data size
*
* This function allows to perform a more accurate validation for attributes
* whose size is variable. If the size of the attribute is not what we expect,
* this functions returns -1 and errno is explicitly set.
*/
EXPORT_SYMBOL(mnl_attr_validate2);
int mnl_attr_validate2(const struct nlattr *attr, enum mnl_attr_data_type type,
size_t exp_len)
{
if (type >= MNL_TYPE_MAX) {
errno = EINVAL;
return -1;
}
return __mnl_attr_validate(attr, type, exp_len);
}
/**
* mnl_attr_parse - parse attributes
* \param nlh pointer to netlink message
* \param offset offset to start parsing from (if payload is after any header)
* \param cb callback function that is called for each attribute
* \param data pointer to data that is passed to the callback function
*
* This function allows to iterate over the sequence of attributes that compose
* the Netlink message. You can then put the attribute in an array as it
* usually happens at this stage or you can use any other data structure (such
* as lists or trees).
*
* This function propagates the return value of the callback, which can be
* MNL_CB_ERROR, MNL_CB_OK or MNL_CB_STOP.
*/
EXPORT_SYMBOL(mnl_attr_parse);
int mnl_attr_parse(const struct nlmsghdr *nlh, unsigned int
没有合适的资源?快使用搜索试试~ 我知道了~
libmnl_1.0.4官方源码
需积分: 1 45 下载量 141 浏览量
2022-03-29
16:36:27
上传
评论 1
收藏 363KB GZ 举报
温馨提示
共72个文件
c:22个
in:14个
am:12个
libmnl_1.0.4官方源码文件,未有任何改动,可用于ethtool配置
资源详情
资源评论
资源推荐
收起资源包目录
libmnl-1.0.4.orig.tar.gz (72个子文件)
libmnl-1.0.4
Makefile.am 243B
Makefile.in 29KB
configure 406KB
src
Makefile.am 233B
Makefile.in 21KB
socket.c 11KB
nlmsg.c 18KB
attr.c 25KB
callback.c 5KB
libmnl.map 2KB
internal.h 243B
config.h.in 2KB
configure.ac 1KB
build-aux
compile 7KB
ltmain.sh 277KB
depcomp 23KB
install-sh 14KB
missing 7KB
config.guess 42KB
config.sub 35KB
doxygen.cfg.in 5KB
COPYING 26KB
examples
Makefile.am 38B
Makefile.in 18KB
rtnl
Makefile.am 1014B
Makefile.in 23KB
rtnl-route-event.c 8KB
rtnl-link-dump.c 3KB
rtnl-route-dump.c 8KB
rtnl-link-set.c 2KB
rtnl-link-dump3.c 2KB
rtnl-addr-dump.c 3KB
rtnl-link-dump2.c 2KB
rtnl-link-event.c 2KB
rtnl-route-add.c 3KB
netfilter
Makefile.am 594B
Makefile.in 21KB
nfct-create-batch.c 5KB
nf-log.c 5KB
nfct-daemon.c 9KB
nfct-event.c 5KB
nf-queue.c 6KB
nfct-dump.c 7KB
genl
Makefile.am 278B
Makefile.in 19KB
genl-family-get.c 5KB
genl-group-events.c 1KB
kobject
Makefile.am 152B
Makefile.in 18KB
kobject-event.c 967B
include
Makefile.am 23B
Makefile.in 18KB
libmnl
Makefile.am 30B
Makefile.in 16KB
libmnl.h 8KB
linux
Makefile.am 56B
Makefile.in 18KB
netlink.h 5KB
netfilter
Makefile.am 39B
Makefile.in 14KB
nfnetlink_conntrack.h 5KB
socket.h 738B
libmnl.pc.in 309B
Make_global.am 1KB
m4
ltsugar.m4 4KB
ltoptions.m4 12KB
ltversion.m4 690B
lt~obsolete.m4 6KB
gcc4_visibility.m4 616B
libtool.m4 280KB
README 1KB
aclocal.m4 41KB
共 72 条
- 1
The_Someone
- 粉丝: 166
- 资源: 14
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0