반응형

 

아래의 

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 추가하는 코드는 아래와 같다.

https://github.com/strongswan/strongswan/blob/06e11b481b81d131d5e755d4adf287079d8753bd/src/libcharon/plugins/kernel_netlink/kernel_netlink_net.c

..
/*
 * 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;
}
반응형