netlink通信
<h2>概述</h2>
<p>> 参考文档:<a href="https://mp.weixin.qq.com/s/eyCUkVhMgICnfHbtoAEoag?version=4.1.28.6010&platform=win&nwr_flag=1#wechat_redirect">https://mp.weixin.qq.com/s/eyCUkVhMgICnfHbtoAEoag?version=4.1.28.6010&platform=win&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-&gt;state = SS_UNCONNECTED;
if (sock-&gt;type != SOCK_RAW &amp;&amp; sock-&gt;type != SOCK_DGRAM)
return -ESOCKTNOSUPPORT;
if (protocol &lt; 0 || protocol &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(&quot;net-pf-%d-proto-%d&quot;, PF_NETLINK, protocol);
netlink_lock_table();
}
#endif
if (nl_table[protocol].registered &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 &lt; 0)
goto out;
err = __netlink_create(net, sock, cb_mutex, protocol, kern);
if (err &lt; 0)
goto out_module;
local_bh_disable();
sock_prot_inuse_add(net, &amp;netlink_proto, 1);
local_bh_enable();
nlk = nlk_sk(sock-&gt;sk);
nlk-&gt;module = module;
nlk-&gt;netlink_bind = bind;
nlk-&gt;netlink_unbind = unbind;
out:
return err;
out_module:
module_put(module);
goto out;
}</code></pre>