Linux/Linux
[Linux/Nelink] Netlink attribute 추가하는 코드 분석
2022. 6. 18. 09:59반응형
아래의
https://gist.github.com/stdk/6675dc6a9bb6a172731e9a0239e01263
넷링크 attribute 추가하는 코드는 아래와 같고
..
static struct rtattr * netlink_add_attribute(struct nlmsghdr *header,int maxlen,int type, void *data, int datalen) {
int len = RTA_SPACE(datalen);
if ((int)NLMSG_ALIGN(header->nlmsg_len) + len > maxlen)
return 0;
struct rtattr *attr = (struct rtattr*)(((char*)header) + NLMSG_ALIGN(header->nlmsg_len));
attr->rta_type = type;
attr->rta_len = len;
memcpy(RTA_DATA(attr), data, datalen);
header->nlmsg_len = NLMSG_ALIGN(header->nlmsg_len) + len;
return attr;
}
..
strongswan에서 넷링크 attribute 추가하는 코드는 아래와 같다.
..
/*
* Described in header
*/
void netlink_add_attribute(struct nlmsghdr *hdr, int rta_type, chunk_t data,
size_t buflen)
{
struct rtattr *rta;
if (NLMSG_ALIGN(hdr->nlmsg_len) + RTA_LENGTH(data.len) > buflen)
{
DBG1(DBG_KNL, "unable to add attribute, buffer too small");
return;
}
rta = (struct rtattr*)(((char*)hdr) + NLMSG_ALIGN(hdr->nlmsg_len));
rta->rta_type = rta_type;
rta->rta_len = RTA_LENGTH(data.len);
memcpy(RTA_DATA(rta), data.ptr, data.len);
hdr->nlmsg_len = NLMSG_ALIGN(hdr->nlmsg_len) + RTA_ALIGN(rta->rta_len);
}
..
void *netlink_nested_start(struct nlmsghdr *hdr, size_t buflen, int type)
{
return add_rtattr(hdr, buflen, type, 0);
}
/**
* Add an attribute to the given Netlink message
*/
static struct rtattr *add_rtattr(struct nlmsghdr *hdr, int buflen, int type,
int len)
{
struct rtattr *rta;
if (NLMSG_ALIGN(hdr->nlmsg_len) + RTA_LENGTH(len) > buflen)
{
DBG1(DBG_KNL, "unable to add attribute, buffer too small");
return NULL;
}
rta = ((void*)hdr) + NLMSG_ALIGN(hdr->nlmsg_len);
rta->rta_type = type;
rta->rta_len = RTA_LENGTH(len);
hdr->nlmsg_len = NLMSG_ALIGN(hdr->nlmsg_len) + RTA_ALIGN(rta->rta_len);
return rta;
}
반응형
'Linux > Linux' 카테고리의 다른 글
[Linux] xfrm command (0) | 2022.06.28 |
---|---|
libnl, Netlink library에 대해 (0) | 2022.06.25 |
Unix Domain Socket (AF_UNIX) (0) | 2022.06.07 |
[Linux/ip] GRE tunnel 생성하기 (ip tunnel 커맨드 이용) (2) | 2022.06.07 |
[Linux] ip header, ip tunnel, iphdr, ether_header 관련 커널 소스 분석 (0) | 2022.06.07 |