码迷,mamicode.com
首页 > Web开发 > 详细

Netlink 内核实现分析 4

时间:2019-05-12 01:47:46      阅读:211      评论:0      收藏:0      [点我收藏+]

标签:efi   \n   nta   dea   ati   init   htm   实现   fine   

netlink 库函数:

http://www.infradead.org/~tgr/libnl/doc/core.html#core_netlink_fundamentals

#define NETLINK_TEST            (31)

static
int s_send_ack_to_test(struct lua_nl_ack *ack)
{
    struct sk_buff *nl_skb;
    struct nlmsghdr *nlh;

    int ret;
    int len = sizeof(*ack);

    nl_skb = nlmsg_new(len, GFP_ATOMIC);
    if (!nl_skb) {
        pr_err("netlink: nlmsg_new fail\n");
        return -1;
    }

    nlh = nlmsg_put(nl_skb, 0, 0, NETLINK_WNS, len, 0);
    if(nlh == NULL) {
        pr_err("netlink: nlmsg_put fail\n");
        nlmsg_free(nl_skb);
        return -1;
    }

    memcpy(nlmsg_data(nlh), (void *)ack, len);
    ret = netlink_unicast(nlsk, nl_skb, NETLINK_WNS_LUA, MSG_DONTWAIT);
    
    return ret;
}

static
void test_input_cb(struct sk_buff *skb)
{
    struct nlmsghdr *nlh = NULL;
    struct lua_nl_req *req = NULL;
    struct lua_nl_ack ack;

    nlh = (struct nlmsghdr *)skb->data;

    if (NLMSG_OK(nlh, skb->len) && skb->len >= NLMSG_HDRLEN + sizeof(struct lua_nl_req)) {
        req = (struct lua_nl_req *)NLMSG_DATA(nlh);

        (void)s_get_ip_mac(req, &ack);

        if (s_send_ack_to_test(&ack) < 0) {
            pr_err("netlink: s_send_ack_to_lua error\n");
        }

        //TODO: netlink消息失败的情况需要再仔细考虑下,可能导致阻塞
        
    }
    else {
        pr_err("netlink: parameters error\n");
    }

    return ;
}

static struct netlink_kernel_cfg cfg = {
    .input = test_input_cb,
};

static
int s_init_netlink()
{
    nlsk = netlink_kernel_create(&init_net, NETLINK_TEST, THIS_MODULE, &cfg);
    if (!nlsk) {
        pr_err("netlink: init fail\n");
        return -1;
    }

    return 0;
}

 

Netlink 内核实现分析 4

标签:efi   \n   nta   dea   ati   init   htm   实现   fine   

原文地址:https://www.cnblogs.com/codestack/p/10850608.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!