公开学习文档

公开学习文档


netlink通信

<h2>概述</h2> <p>&gt; 参考文档:<a href="https://mp.weixin.qq.com/s/eyCUkVhMgICnfHbtoAEoag?version=4.1.28.6010&amp;platform=win&amp;nwr_flag=1#wechat_redirect">https://mp.weixin.qq.com/s/eyCUkVhMgICnfHbtoAEoag?version=4.1.28.6010&amp;platform=win&amp;nwr_flag=1#wechat_redirect</a></p> <h2>分析</h2> <pre><code class="language-c">// file: net/netlink/af_netlink.c static const struct net_proto_family netlink_family_ops = { .family = PF_NETLINK, .create = netlink_create, .owner = THIS_MODULE, /* for consistency 8) */ }; static int netlink_create(struct net *net, struct socket *sock, int protocol, int kern) { struct module *module = NULL; struct mutex *cb_mutex; struct netlink_sock *nlk; int (*bind)(struct net *net, int group); void (*unbind)(struct net *net, int group); int err = 0; sock-&amp;gt;state = SS_UNCONNECTED; if (sock-&amp;gt;type != SOCK_RAW &amp;amp;&amp;amp; sock-&amp;gt;type != SOCK_DGRAM) return -ESOCKTNOSUPPORT; if (protocol &amp;lt; 0 || protocol &amp;gt;= MAX_LINKS) return -EPROTONOSUPPORT; protocol = array_index_nospec(protocol, MAX_LINKS); netlink_lock_table(); #ifdef CONFIG_MODULES if (!nl_table[protocol].registered) { netlink_unlock_table(); request_module(&amp;quot;net-pf-%d-proto-%d&amp;quot;, PF_NETLINK, protocol); netlink_lock_table(); } #endif if (nl_table[protocol].registered &amp;amp;&amp;amp; try_module_get(nl_table[protocol].module)) module = nl_table[protocol].module; else err = -EPROTONOSUPPORT; cb_mutex = nl_table[protocol].cb_mutex; bind = nl_table[protocol].bind; unbind = nl_table[protocol].unbind; netlink_unlock_table(); if (err &amp;lt; 0) goto out; err = __netlink_create(net, sock, cb_mutex, protocol, kern); if (err &amp;lt; 0) goto out_module; local_bh_disable(); sock_prot_inuse_add(net, &amp;amp;netlink_proto, 1); local_bh_enable(); nlk = nlk_sk(sock-&amp;gt;sk); nlk-&amp;gt;module = module; nlk-&amp;gt;netlink_bind = bind; nlk-&amp;gt;netlink_unbind = unbind; out: return err; out_module: module_put(module); goto out; }</code></pre>

页面列表

ITEM_HTML