{"id":4134,"date":"2012-08-19T16:20:16","date_gmt":"2012-08-19T08:20:16","guid":{"rendered":"http:\/\/edsionte.com\/techblog\/?p=4134"},"modified":"2012-08-24T20:49:01","modified_gmt":"2012-08-24T12:49:01","slug":"netlink%e7%bc%96%e7%a8%8b-%e7%94%a8%e6%88%b7%e4%b8%bb%e5%8a%a8%e5%8f%91%e8%b5%b7%e4%bc%9a%e8%af%9d","status":"publish","type":"post","link":"http:\/\/edsionte.com\/techblog\/archives\/4134","title":{"rendered":"Netlink\u7f16\u7a0b-\u7528\u6237\u4e3b\u52a8\u53d1\u8d77\u4f1a\u8bdd"},"content":{"rendered":"<p>Netlink\u662f\u4e00\u79cd\u5728\u5185\u6838\u6001\u548c\u7528\u6237\u6001\u53ef\u4ee5\u8fdb\u884c\u53cc\u5411\u901a\u4fe1\u7684\u673a\u5236\uff0c\u4e5f\u5c31\u662f\u8bf4\uff0c\u7528\u6237\u8fdb\u7a0b\u65e2\u53ef\u4ee5\u4f5c\u4e3a\u670d\u52a1\u5668\u7aef\u53c8\u53ef\u4ee5\u4f5c\u4e3a\u5ba2\u6237\u7aef\uff0c\u5185\u6838\u4e5f\u662f\u5982\u6b64\u3002\u7528\u6237\u8fdb\u7a0b\u548c\u5185\u6838\u8c01\u662f\u670d\u52a1\u5668\u7aef\u8c01\u662f\u5ba2\u6237\u7aef\uff0c\u8fd9\u4e2a\u95ee\u9898\u4e0e\u8c01\u5148\u4e3b\u52a8\u53d1\u8d77\u6570\u636e\u4ea4\u4e92\u4f1a\u8bdd\u6709\u5173\u3002<\/p>\n<p>\u7528\u6237\u8fdb\u7a0b\u4e3b\u52a8\u5411\u5185\u6838\u53d1\u8d77\u4f1a\u8bdd\u5728Linux\u5185\u6838\u4e2d\u5f88\u5e38\u89c1\uff0c\u6bd4\u5982\u7cfb\u7edf\u8c03\u7528\u3001\u5bf9\/proc\u7684\u64cd\u4f5c\u7b49\u3002\u672c\u6587\u901a\u8fc7\u8be6\u89e3\u4e00\u4e2a\u7b80\u5355\u7684\u5b9e\u4f8b\u7a0b\u5e8f\u6765\u8bf4\u660e\u7528\u6237\u8fdb\u7a0b\u901a\u8fc7netlink\u673a\u5236\u5982\u4f55\u4e3b\u52a8\u5411\u5185\u6838\u53d1\u8d77\u4f1a\u8bdd\u3002\u5728\u8be5\u7a0b\u5e8f\u4e2d\uff0c\u7528\u6237\u8fdb\u7a0b\u5411\u5185\u6838\u53d1\u9001\u4e00\u6bb5\u5b57\u7b26\u4e32\uff0c\u5185\u6838\u63a5\u6536\u5230\u540e\u518d\u5c06\u8be5\u5b57\u7b26\u4e32\u540e\u518d\u91cd\u65b0\u53d1\u7ed9\u7528\u6237\u8fdb\u7a0b\u3002<\/p>\n<h3>\u7528\u6237\u6001\u7a0b\u5e8f<\/h3>\n<p>netlink\u662f\u4e00\u79cd\u7279\u6b8a\u7684\u5957\u63a5\u5b57\uff0c\u5728\u7528\u6237\u6001\u9664\u4e86\u4e00\u4e9b\u53c2\u6570\u7684\u4f20\u9012\u5bf9\u5176\u4f7f\u7528\u7684\u65b9\u6cd5\u4e0e\u4e00\u822c\u5957\u63a5\u5b57\u65e0\u8f83\u5927\u5dee\u5f02\uff0c\u3002<\/p>\n<p><strong>1.\u5b8f\u4e0e\u6570\u636e\u7ed3\u6784\u7684\u5b9a\u4e49<\/strong><\/p>\n<p>\u5728\u4f7f\u7528netlink\u8fdb\u884c\u7528\u6237\u8fdb\u7a0b\u548c\u5185\u6838\u7684\u6570\u636e\u4ea4\u4e92\u65f6\uff0c\u6700\u91cd\u8981\u7684\u662f\u5b9a\u4e49\u597d\u901a\u4fe1\u534f\u8bae\u3002\u534f\u8bae\u4e00\u8bcd\u76f4\u767d\u7684\u8bf4\u5c31\u662f\u7528\u6237\u8fdb\u7a0b\u548c\u5185\u6838\u5e94\u8be5\u4ee5\u4ec0\u4e48\u6837\u7684\u5f62\u5f0f\u53d1\u9001\u6570\u636e\uff0c\u4ee5\u4ec0\u4e48\u6837\u7684\u5f62\u5f0f\u63a5\u6536\u6570\u636e\u3002\u800c\u8fd9\u4e2a\u201c\u5f62\u5f0f\u201d\u901a\u5e38\u5bf9\u5e94\u7a0b\u5e8f\u4e2d\u7684\u4e00\u4e2a\u7279\u5b9a\u6570\u636e\u7ed3\u6784\u3002<\/p>\n<p>\u672c\u6587\u6240\u6f14\u793a\u7684\u7a0b\u5e8f\u5e76\u6ca1\u6709\u4f7f\u7528netlink\u5df2\u6709\u7684\u901a\u4fe1\u534f\u8bae\uff0c\u56e0\u6b64\u6211\u4eec\u81ea\u5b9a\u4e49\u4e00\u79cd\u534f\u8bae\u7c7b\u578bNETLINK_TEST\u3002<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\n#define NETLINK_TEST 18\r\n#define MAX_PAYLOAD 1024\r\n\r\nstruct req {\r\n\tstruct nlmsghdr nlh;\r\n\tchar buf&#x5B;MAX_PAYLOAD];\r\n};\r\n<\/pre>\n<p>\u9664\u6b64\u4e4b\u5916\uff0c\u6211\u4eec\u5e94\u8be5\u518d\u81ea\u5b9a\u4e49\u4e00\u4e2a\u6570\u636e\u62a5\u7c7b\u578breq\uff0c\u8be5\u7ed3\u6784\u5305\u542b\u4e86netlink\u6570\u636e\u5305\u5934\u7ed3\u6784\u7684\u53d8\u91cfnlh\u548c\u4e00\u4e2aMAX_PAYLOAD\u5927\u5c0f\u7684\u7f13\u51b2\u533a\u3002\u8fd9\u91cc\u6211\u4eec\u4e3a\u4e86\u6f14\u793a\u7b80\u5355\uff0c\u5e76\u6ca1\u6709\u50cf\u4e0a\u6587\u4e2d\u63cf\u8ff0\u7684\u90a3\u6837\u5c06\u4e00\u4e2a\u7279\u5b9a\u6570\u636e\u7ed3\u6784\u4e0enlmsghdr\u5c01\u88c5\u8d77\u6765\u3002<\/p>\n<p><strong>2.\u521b\u5efanetlink\u5957\u63a5\u5b57<\/strong><\/p>\n<p>\u8981\u4f7f\u7528netlink\uff0c\u5fc5\u987b\u5148\u521b\u5efa\u4e00\u4e2anetlink\u5957\u63a5\u5b57\u3002\u521b\u5efa\u65b9\u6cd5\u540c\u6837\u91c7\u7528socket()\uff0c\u53ea\u662f\u8fd9\u91cc\u9700\u8981\u6ce8\u610f\u4f20\u9012\u7684\u53c2\u6570\uff1a<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\n\tint sock_fd;\r\n\tsock_fd = socket(PF_NETLINK, SOCK_RAW, NETLINK_TEST);\r\n\tif (sock_fd &lt; 0) {\r\n\t\teprint(errno, &quot;socket&quot;, __LINE__);\r\n\t\treturn errno;\r\n\t}\r\n<\/pre>\n<p>\u7b2c\u4e00\u4e2a\u53c2\u6570\u5fc5\u987b\u6307\u5b9a\u4e3aPF_NETLINK\u6216AF_NETLINK\u3002\u7b2c\u4e8c\u4e2a\u53c2\u6570\u5fc5\u987b\u6307\u5b9a\u4e3aSOCK_RAW\u6216SOCK_DGRAM\uff0c\u56e0\u4e3anetlink\u63d0\u4f9b\u7684\u662f\u4e00\u79cd\u65e0\u8fde\u63a5\u7684\u6570\u636e\u62a5\u670d\u52a1\u3002\u7b2c\u4e09\u4e2a\u53c2\u6570\u5219\u6307\u5b9a\u5177\u4f53\u7684\u534f\u8bae\u7c7b\u578b\uff0c\u6211\u4eec\u8fd9\u91cc\u4f7f\u7528\u81ea\u5b9a\u4e49\u7684\u534f\u8bae\u7c7b\u578bNETLINK_TEST\u3002<\/p>\n<p>\u53e6\u5916\uff0ceprint()\u662f\u4e00\u4e2a\u81ea\u5b9a\u4e49\u7684\u51fa\u9519\u5904\u7406\u51fd\u6570\uff0c\u5b9e\u73b0\u5982\u4e0b\uff1a<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\nvoid eprint(int err_no, char *str, int line)\r\n{\r\n\tprintf(&quot;Error %d in line %d:%s() with %s\\n&quot;, err_no, line, str, strerror(errno));\r\n}\r\n<\/pre>\n<p><strong>3.\u5c06\u672c\u5730\u5957\u63a5\u5b57\u4e0e\u6e90\u5730\u5740\u7ed1\u5b9a<\/strong><\/p>\n<p>\u5c06\u672c\u5730\u7684\u5957\u63a5\u5b57\u4e0e\u6e90\u5730\u5740\u8fdb\u884c\u7ed1\u5b9a\u901a\u8fc7bind()\u5b8c\u6210\u3002\u5728\u7ed1\u5b9a\u4e4b\u524d\uff0c\u9700\u8981\u5c06\u6e90\u5730\u5740\u8fdb\u884c\u521d\u59cb\u5316\uff0cnl_pid\u5b57\u6bb5\u6307\u660e\u53d1\u9001\u6d88\u606f\u4e00\u65b9\u7684pid\uff0cnl_groups\u8868\u793a\u591a\u64ad\u7ec4\u7684\u63a9\u7801\uff0c\u8fd9\u91cc\u6211\u4eec\u5e76\u6ca1\u6709\u6d89\u53ca\u591a\u64ad\uff0c\u56e0\u6b64\u9ed8\u8ba4\u4e3a0\u3002<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\n\tstruct sockaddr_nl src_addr;\r\n\tmemset(&amp;src_addr, 0, sizeof(src_addr));\r\n\tsrc_addr.nl_family = AF_NETLINK;\r\n\tsrc_addr.nl_pid = getpid();\r\n\tsrc_addr.nl_groups = 0;\r\n\r\n\tif (bind(sock_fd, (struct sockaddr *)&amp;src_addr, sizeof(src_addr)) &lt; 0) {\r\n\t\teprint(errno, &quot;bind&quot;, __LINE__);\r\n\t\treturn errno;\r\n\t}\r\n<\/pre>\n<p><strong>4.\u521d\u59cb\u5316msghdr\u7ed3\u6784<\/strong><\/p>\n<p>\u7528\u6237\u8fdb\u7a0b\u6700\u7ec8\u53d1\u9001\u7684\u662fmsghdr\u7ed3\u6784\u7684\u6d88\u606f\uff0c\u56e0\u6b64\u5fc5\u987b\u5bf9\u8fd9\u4e2a\u7ed3\u6784\u8fdb\u884c\u521d\u59cb\u5316\u3002\u800c\u6b64\u7ed3\u6784\u53c8\u4e0esockaddr_nl\uff0ciovec\u548cnlmsghdr\u4e09\u4e2a\u7ed3\u6784\u76f8\u5173\uff0c\u56e0\u6b64\u5fc5\u987b\u4f9d\u6b21\u5bf9\u8fd9\u4e9b\u6570\u636e\u7ed3\u6784\u8fdb\u884c\u521d\u59cb\u5316\u3002<\/p>\n<p>\u9996\u5148\u521d\u59cb\u5316\u76ee\u7684\u5957\u63a5\u5b57\u7684\u5730\u5740\u7ed3\u6784\uff0c\u8be5\u7ed3\u6784\u4e0e\u6e90\u5957\u63a5\u5b57\u5730\u5740\u7ed3\u6784\u521d\u59cb\u5316\u7684\u65b9\u6cd5\u7a0d\u6709\u4e0d\u540c\uff0c\u5373nl_pid\u5fc5\u987b\u4e3a0\uff0c\u8868\u793a\u63a5\u6536\u65b9\u4e3a\u5185\u6838\u3002<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\n\tstruct sockaddr_nl dest_addr;\r\n\tmemset(&amp;dest_addr, 0, sizeof(dest_addr));\r\n\tdest_addr.nl_family = AF_NETLINK;\r\n\tdest_addr.nl_pid = 0;\r\n\tdest_addr.nl_groups = 0;\r\n<\/pre>\n<p>\u63a5\u4e0b\u6765\u5bf9req\u7c7b\u578b\u7684\u6570\u636e\u62a5\u8fdb\u884c\u521d\u59cb\u5316\uff0c\u5373\u4f9d\u6b21\u5bf9\u5176\u5c01\u88c5\u7684\u4e24\u4e2a\u6570\u636e\u7ed3\u6784\u521d\u59cb\u5316\uff1a<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\n\tstruct req r;\r\n\tr.nlh.nlmsg_len = NLMSG_SPACE(MAX_PAYLOAD);\r\n\tr.nlh.nlmsg_pid = getpid();\r\n\tr.nlh.nlmsg_flags = 0;\r\n\tmemset(r.buf, 0, MAX_PAYLOAD);\r\n\tstrcpy(NLMSG_DATA(&amp;(r.nlh)), &quot;hello, I am edsionte!&quot;);\r\n<\/pre>\n<p>\u8fd9\u91cc\u7684nlmsg_len\u4e3a\u4e3asizeof(struct nlmsghdr)+MAX_PAYLOAD\u7684\u603b\u548c\u3002\u5b8fNLMSG_SPACE\u4f1a\u81ea\u52a8\u5c06\u4e24\u8005\u7684\u957f\u5ea6\u76f8\u52a0\u3002\u63a5\u4e0b\u6765\u5bf9\u7f13\u51b2\u533a\u5411\u91cfiov\u8fdb\u884c\u521d\u59cb\u5316\uff0c\u8ba9iov_base\u5b57\u6bb5\u6307\u5411\u6570\u636e\u62a5\u7ed3\u6784\uff0c\u800ciov_len\u4e3a\u6570\u636e\u62a5\u957f\u5ea6\u3002<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\n\tstruct iovec iov;\r\n\tiov.iov_base = (void *)&amp;r;\r\n\tiov.iov_len = sizeof(r);\r\n<\/pre>\n<p>\u4e00\u5207\u5c31\u7eea\u540e\uff0c\u5c06\u76ee\u7684\u5957\u63a5\u5b57\u5730\u5740\u4e0e\u5f53\u524d\u8981\u53d1\u9001\u7684\u6d88\u606fmsg\u7ed1\u5b9a\uff0c\u5373\u5c06\u76ee\u7684\u5957\u63a5\u5b57\u5730\u5740\u590d\u5236\u7ed9msg_name\u3002\u518d\u5c06\u8981\u53d1\u9001\u7684\u6570\u636eiov\u4e0emsg_iov\u7ed1\u5b9a\uff0c\u5982\u679c\u4e00\u6b21\u6027\u8981\u53d1\u9001\u591a\u4e2a\u6570\u636e\u5305\uff0c\u5219\u521b\u5efa\u4e00\u4e2aiovec\u7c7b\u578b\u7684\u6570\u7ec4\u3002<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\n\tstruct msghdr msg;\r\n\tmsg.msg_name = (void *)&amp;dest_addr;\r\n\tmsg.msg_namelen = sizeof(dest_addr);\r\n\tmsg.msg_iov = &amp;iov;\r\n\tmsg.msg_iovlen = 1;\r\n<\/pre>\n<p><strong>5.\u5411\u5185\u6838\u53d1\u9001\u6d88\u606f<\/strong><\/p>\n<p>\u53d1\u9001\u6d88\u606f\u5219\u5f88\u7b80\u5355\uff0c\u901a\u8fc7sendmsg\u51fd\u6570\u5373\u53ef\u5b8c\u6210\uff0c\u524d\u63d0\u662f\u6b63\u786e\u7684\u521b\u5efanetlink\u5957\u63a5\u5b57\u548c\u8981\u53d1\u9001\u7684\u6d88\u606f\u3002<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\n\tif (sendmsg(sock_fd, &amp;msg, 0) &lt; 0) {\r\n\t\teprint(errno, &quot;sendmsg&quot;, __LINE__);\r\n\t\treturn errno;\r\n\t}\r\n<\/pre>\n<p><strong>6.\u63a5\u53d7\u5185\u6838\u53d1\u6765\u7684\u6d88\u606f<\/strong><\/p>\n<p>\u5982\u679c\u7528\u6237\u8fdb\u7a0b\u9700\u8981\u63a5\u6536\u5185\u6838\u53d1\u9001\u7684\u6d88\u606f\uff0c\u5219\u9700\u8981\u901a\u8fc7recvmsg\u5b8c\u6210\uff0c\u53ea\u4e0d\u8fc7\u5728\u63a5\u6536\u4e4b\u524d\u9700\u8981\u5c06\u6570\u636e\u62a5r\u91cd\u65b0\u521d\u59cb\u5316\uff0c\u56e0\u4e3a\u53d1\u9001\u548c\u63a5\u6536\u65f6\u4f20\u9012\u7684\u6570\u636e\u7ed3\u6784\u53ef\u80fd\u662f\u4e0d\u540c\u7684\u3002<\/p>\n<p>\u4e3a\u4e86\u7b80\u5355\u6f14\u793anetlink\u7684\u7528\u6cd5\uff0c\u672c\u6587\u6240\u8ff0\u7684\u7528\u6237\u8fdb\u7a0b\u53d1\u9001\u7684\u662f\u4e00\u6bb5\u5b57\u7b26\u4e32\uff0c\u8fd9\u4e00\u70b9\u4ece\u6570\u636e\u62a5\u7ed3\u6784req\u7684\u5b9a\u4e49\u53ef\u4ee5\u770b\u51fa\u3002\u800c\u5185\u6838\u5411\u7528\u6237\u8fdb\u7a0b\u53d1\u9001\u7684\u4e5f\u662f\u4e00\u6bb5\u5b57\u7b26\u4e32\uff0c\u5177\u4f53\u60c5\u51b5\u4e0b\u9762\u5c06\u4f1a\u5177\u4f53\u8bf4\u660e\u3002<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\n        memset(&amp;r.nlh, 0, NLMSG_SPACE(MAX_PAYLOAD));\r\n        if (recvmsg(sock_fd, &amp;msg, 0) &lt; 0) {\r\n                eprint(errno, &quot;recvmsg&quot;, __LINE__);\r\n                return errno;\r\n        }\r\n\r\n        printf(&quot;Received message payload:%s\\n&quot;, (char *)NLMSG_DATA(&amp;r.nlh));\r\n        close(sock_fd);\r\n\r\n<\/pre>\n<p>\u63a5\u6536\u5b8c\u6bd5\u540e\uff0c\u901a\u8fc7\u4e13\u95e8\u7684\u5b8fNLMSG_DATA\u5bf9\u6570\u636e\u62a5\u8fdb\u884c\u64cd\u4f5c\u3002 netlink\u5bf9\u6570\u636e\u62a5\u7684\u7684\u8bbf\u95ee\u548c\u64cd\u4f5c\u90fd\u662f\u901a\u8fc7\u4e00\u7cfb\u5217\u6807\u51c6\u7684\u5b8fNLMSG_XXX\u6765\u5b8c\u6210\u7684\uff0c\u5177\u4f53\u7684\u8bf4\u660e\u53ef\u4ee5\u901a\u8fc7man netlink\u67e5\u770b\u3002\u8fd9\u91cc\u7684NLMSG_DATA\u4f20\u9012\u8fdb\u53bb\u7684\u662fnlh\uff0c\u4f46\u5b83\u83b7\u53d6\u7684\u662f\u7d27\u90bbnlh\u7684\u771f\u6b63\u6570\u636e\u3002\u672c\u7a0b\u5e8f\u4e2d\u4f20\u9012\u7684\u662f\u5b57\u7b26\u4e32\uff0c\u6240\u4ee5\u53d6\u6570\u636e\u65f6\u5019\u7528char *\u5f3a\u5236\u7c7b\u578b\u8f6c\u6362\uff0c\u5982\u679c\u4f20\u9012\u7684\u662f\u5176\u4ed6\u6570\u636e\u7ed3\u6784\uff0c\u5219\u76f8\u5e94\u8f6c\u6362\u6570\u636e\u7c7b\u578b\u5373\u53ef\u3002<\/p>\n<h3>\u5185\u6838\u6a21\u5757<\/h3>\n<p>netlink\u65e2\u7136\u662f\u4e00\u79cd\u7528\u6237\u6001\u548c\u5185\u6838\u6001\u4e4b\u95f4\u7684\u53cc\u5411\u901a\u4fe1\u673a\u5236\uff0c\u90a3\u4e48\u9664\u4e86\u7f16\u5199\u7528\u6237\u7a0b\u5e8f\u8fd8\u8981\u7f16\u5199\u5185\u6838\u6a21\u5757\uff0c\u4e5f\u5c31\u662f\u8bf4\u7528\u6237\u8fdb\u7a0b\u548c\u5185\u6838\u6a21\u5757\u4e4b\u95f4\u5bf9\u6570\u636e\u7684\u5904\u7406\u8981\u5f7c\u6b64\u5bf9\u5e94\u8d77\u6765\u3002<\/p>\n<p><strong>1.\u5185\u6838\u6a21\u5757\u52a0\u8f7d\u548c\u5378\u8f7d\u51fd\u6570 <\/strong><\/p>\n<p>\u5185\u6838\u6a21\u5757\u52a0\u8f7d\u51fd\u6570\u4e3b\u8981\u901a\u8fc7netlink_kernel_create\u51fd\u6570\u7533\u8bf7\u670d\u52a1\u5668\u7aef\u7684\u5957\u63a5\u5b57nl_sk\uff0c\u5185\u6838\u4e2d\u5bf9\u5957\u63a5\u5b57\u8868\u793a\u4e3asock\u7ed3\u6784\u3002\u53e6\u5916\uff0c\u5728\u521b\u5efa\u5957\u63a5\u5b57\u65f6\u8fd8\u9700\u8981\u4f20\u9012\u548c\u7528\u6237\u8fdb\u7a0b\u76f8\u540c\u7684netlink\u534f\u8bae\u7c7b\u578bNETLINK_TEST\u3002\u521b\u5efa\u5957\u63a5\u5b57\u51fd\u6570\u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\u9ed8\u8ba4\u4e3ainit_net\uff0c\u7b2c\u4e09\u4e2a\u53c2\u6570\u4e3a\u591a\u64ad\u65f6\u4f7f\u7528\uff0c\u6211\u4eec\u8fd9\u91cc\u4e0d\u4f7f\u7528\u591a\u64ad\u56e0\u6b64\u9ed8\u8ba4\u503c\u4e3a0\u3002nl_data_handler\u662f\u4e00\u4e2a\u94a9\u5b50\u51fd\u6570\uff0c\u6bcf\u5f53\u5185\u6838\u63a5\u6536\u5230\u4e00\u4e2a\u6d88\u606f\u65f6\uff0c\u8fd9\u4e2a\u94a9\u5b50\u51fd\u6570\u5c31\u88ab\u56de\u8c03\u5bf9\u7528\u6237\u6570\u636e\u8fdb\u884c\u5904\u7406\u3002<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\n#define NETLINK_TEST 17\r\nstruct sock *nl_sk = NULL;\r\nstatic int __init hello_init(void)\r\n{\r\n\tprintk(&quot;hello_init is starting..\\n&quot;);\r\n\tnl_sk = netlink_kernel_create(&amp;init_net, NETLINK_TEST, 0, nl_data_ready, NULL, THIS_MODULE);\r\n\tif (nl_sk == 0)\r\n\t{\r\n\t\tprintk(&quot;can not create netlink socket.\\n&quot;);\r\n\t\treturn -1;\r\n\t}\r\n\treturn 0;\r\n}\r\n<\/pre>\n<p>\u5185\u6838\u6a21\u5757\u5378\u8f7d\u51fd\u6570\u6240\u505a\u7684\u5de5\u4f5c\u4e0e\u52a0\u8f7d\u51fd\u6570\u76f8\u53cd\uff0c\u901a\u8fc7sock_release\u51fd\u6570\u91ca\u653e\u4e00\u5f00\u59cb\u7533\u8bf7\u7684\u5957\u63a5\u5b57\u3002<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\nstatic void __exit hello_exit(void)\r\n{\r\n\tsock_release(nl_sk-&gt;sk_socket);\r\n\tprintk(&quot;hello_exit is leaving..\\n&quot;);\r\n}\r\n<\/pre>\n<p><strong>2.\u94a9\u5b50\u51fd\u6570\u7684\u5b9e\u73b0<\/strong><\/p>\n<p>\u5728\u5185\u6838\u521b\u5efanetlink\u5957\u63a5\u5b57\u65f6\uff0c\u5fc5\u987b\u7ed1\u5b9a\u4e00\u4e2a\u94a9\u5b50\u51fd\u6570\uff0c\u8be5\u94a9\u5b50\u51fd\u6570\u539f\u578b\u4e3a\uff1a<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\n void (*input)(struct sk_buff *skb);\r\n<\/pre>\n<p>\u94a9\u5b50\u51fd\u6570\u7684\u5b9e\u73b0\u4e3b\u8981\u662f\u5148\u63a5\u6536\u7528\u6237\u8fdb\u7a0b\u53d1\u9001\u7684\u6d88\u606f\uff0c\u63a5\u6536\u4ee5\u540e\u5185\u6838\u518d\u53d1\u9001\u4e00\u6761\u6d88\u606f\u5230\u7528\u6237\u8fdb\u7a0b\u3002<\/p>\n<p>\u5728\u94a9\u5b50\u51fd\u6570\u4e2d\uff0c\u5148\u901a\u8fc7skb_get\u51fd\u6570\u5bf9\u5957\u63a5\u5b57\u7f13\u51b2\u533a\u589e\u52a0\u4e00\u6b21\u5f15\u7528\u503c\uff0c\u518d\u901a\u8fc7nlmsg_hdr\u51fd\u6570\u83b7\u53d6netlink\u6d88\u606f\u5934\u6307\u9488nlh\u3002\u63a5\u7740\u4f7f\u7528NLMSG_DATA\u5b8f\u83b7\u53d6\u7528\u6237\u8fdb\u7a0b\u53d1\u9001\u8fc7\u6765\u7684\u6570\u636estr\u3002\u9664\u6b64\u4e4b\u5916\uff0c\u518d\u6253\u5370\u53d1\u9001\u8005\u7684pid\u3002<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\nvoid nl_data_handler(struct sk_buff *__skb)\r\n{\r\n\tstruct sk_buff *skb;\r\n\tstruct nlmsghdr *nlh;\r\n\tu32 pid;\r\n\tint rc;\r\n\tchar str&#x5B;100];\r\n\tint len = NLMSG_SPACE(MAX_PAYLOAD);\r\n\r\n\tprintk(&quot;read data..\\n&quot;);\r\n\tskb = skb_get(__skb);\r\n\r\n\tif (skb-&gt;len &gt;= NLMSG_SPACE(0)) {\r\n\t\tnlh = nlmsg_hdr(skb);\r\n\t\tprintk(&quot;Recv: %s\\n&quot;, (char *)NLMSG_DATA(nlh));\r\n\t\tmemcpy(str, NLMSG_DATA(nlh), sizeof(str));\r\n\t\tpid = nlh-&gt;nlmsg_pid;\r\n\t\tprintk(&quot;pid is %d\\n&quot;, pid);\r\n\t\tkfree_skb(skb);\r\n<\/pre>\n<p>\u63a5\u4e0b\u6765\u91cd\u65b0\u7533\u8bf7\u4e00\u4e2a\u5957\u63a5\u5b57\u7f13\u51b2\u533a\uff0c\u4e3a\u5185\u6838\u53d1\u9001\u6d88\u606f\u5230\u7528\u6237\u8fdb\u7a0b\u505a\u51c6\u5907\uff0cnlmsg_put\u51fd\u6570\u5c06\u586b\u5145netlink\u6570\u636e\u62a5\u5934\u3002\u63a5\u4e0b\u6765\u5c06\u7528\u6237\u8fdb\u7a0b\u53d1\u9001\u7684\u5b57\u7b26\u4e32\u590d\u5236\u5230nlh\u7d27\u90bb\u7684\u6570\u636e\u7f13\u51b2\u533a\u4e2d\uff0c\u7b49\u5f85\u5185\u6838\u53d1\u9001\u3002netlink_unicast\u51fd\u6570\u5c06\u4ee5\u975e\u963b\u585e\u7684\u65b9\u5f0f\u53d1\u9001\u6570\u636e\u5305\u5230\u7528\u6237\u8fdb\u7a0b\uff0cpid\u5177\u4f53\u6307\u660e\u4e86\u63a5\u6536\u6d88\u606f\u7684\u8fdb\u7a0b\u3002<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\n\t\tskb = alloc_skb(len, GFP_ATOMIC);\r\n\t\tif (!skb){\r\n\t\t\tprintk(KERN_ERR &quot;net_link: allocate failed.\\n&quot;);\r\n\t\t\treturn;\r\n\t\t}\r\n\t\tnlh = nlmsg_put(skb, 0, 0, 0, MAX_PAYLOAD, 0);\r\n\t\tNETLINK_CB(skb).pid = 0;\r\n\r\n\t\tmemcpy(NLMSG_DATA(nlh), str, sizeof(str));\r\n\t\tprintk(&quot;net_link: going to send.\\n&quot;);\r\n\t\trc = netlink_unicast(nl_sk, skb, pid, MSG_DONTWAIT);\r\n\t\tif (rc &lt; 0) {\r\n\t\t\tprintk(KERN_ERR &quot;net_link: can not unicast skb (%d)\\n&quot;, rc);\r\n\t\t}\r\n\t\tprintk(&quot;net_link: send is ok.\\n&quot;);\r\n\t}\r\n}\r\n<\/pre>\n<p>\u8fd9\u6837\u5c31\u5b8c\u6210\u4e86\u5185\u6838\u6a21\u5757\u7684\u7f16\u5199\uff0c\u5b83\u4e0e\u7528\u6237\u8fdb\u7a0b\u901a\u4fe1\u5171\u540c\u5b8c\u6210\u6570\u636e\u4ea4\u4e92\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Netlink\u662f\u4e00\u79cd\u5728\u5185\u6838\u6001\u548c\u7528\u6237\u6001\u53ef\u4ee5\u8fdb\u884c\u53cc\u5411\u901a\u4fe1\u7684\u673a\u5236\uff0c\u4e5f\u5c31\u662f\u8bf4\uff0c\u7528\u6237\u8fdb\u7a0b\u65e2\u53ef\u4ee5\u4f5c\u4e3a\u670d\u52a1\u5668\u7aef\u53c8\u53ef\u4ee5\u4f5c\u4e3a\u5ba2\u6237 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[353],"tags":[354,209,355,405],"class_list":["post-4134","post","type-post","status-publish","format-standard","hentry","category-netlink","tag-netlink","tag-socket","tag-355","tag-405"],"views":10833,"_links":{"self":[{"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/posts\/4134","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/comments?post=4134"}],"version-history":[{"count":0,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/posts\/4134\/revisions"}],"wp:attachment":[{"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/media?parent=4134"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/categories?post=4134"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/tags?post=4134"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}