{"id":86472,"date":"2024-01-07T05:14:17","date_gmt":"2024-01-06T21:14:17","guid":{"rendered":"http:\/\/lrxjmw.cn\/?p=86472"},"modified":"2024-01-07T05:14:17","modified_gmt":"2024-01-06T21:14:17","slug":"userspace-kernel","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/userspace-kernel.html","title":{"rendered":"\u7528\u6237\u7a7a\u95f4\u548c\u5185\u6838\u7a7a\u95f4\u901a\u8baf-Netlink \u4e0a"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\nAlan Cox\u5728\u5185\u68381.3\u7248\u672c\u7684\u5f00\u53d1\u9636\u6bb5\u6700\u5148\u5f15\u5165\u4e86Netlink\uff0c\u521a\u5f00\u59cb\u65f6Netlink\u662f\u4ee5\u5b57\u7b26\u9a71\u52a8\u63a5\u53e3\u7684\u65b9\u5f0f\u63d0\u4f9b\u5185\u6838\u4e0e\u7528\u6237\u7a7a\u95f4\u7684\u53cc\u5411\u6570\u636e\u901a\u4fe1\uff1b\u968f\u540e\uff0c\u57282.1\u5185\u6838\u5f00\u53d1\u8fc7\u7a0b\u4e2d\uff0cAlexey Kuznetsov\u5c06Netlink\u6539\u5199\u6210\u4e00\u4e2a\u66f4\u52a0\u7075\u6d3b\u3001\u4e14\u6613\u4e8e\u6269\u5c55\u7684\u57fa\u4e8e\u6d88\u606f\u901a\u4fe1\u63a5\u53e3\uff0c\u5e76\u5c06\u5176\u5e94\u7528\u5230\u9ad8\u7ea7\u8def\u7531\u5b50\u7cfb\u7edf\u7684\u57fa\u7840\u6846\u67b6\u91cc\u3002\u81ea\u90a3\u65f6\u8d77\uff0cNetlink\u5c31\u6210\u4e86Linux\u5185\u6838\u5b50\u7cfb\u7edf\u548c\u7528\u6237\u6001\u7684\u5e94\u7528\u7a0b\u5e8f\u901a\u4fe1\u7684\u4e3b\u8981\u624b\u6bb5\u4e4b\u4e00\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\"\"<\/p>\n

2001\u5e74\uff0cForCES IETF\u59d4\u5458\u4f1a\u6b63\u5f0f\u5bf9Netlink\u8fdb\u884c\u4e86\u6807\u51c6\u5316\u7684\u5de5\u4f5c\u3002Jamal Hadi Salim\u63d0\u8bae\u5c06Netlink\u5b9a\u4e49\u6210\u4e00\u79cd\u7528\u4e8e\u7f51\u7edc\u8bbe\u5907\u7684\u8def\u7531\u5f15\u64ce\u7ec4\u4ef6\u548c\u5176\u63a7\u5236\u7ba1\u7406\u7ec4\u4ef6\u4e4b\u95f4\u901a\u4fe1\u7684\u534f\u8bae\u3002\u4e0d\u8fc7\u4ed6\u7684\u5efa\u8bae\u6700\u7ec8\u6ca1\u6709\u88ab\u91c7\u7eb3\uff0c\u53d6\u800c\u4ee3\u4e4b\u7684\u662f\u6211\u4eec\u4eca\u5929\u6240\u770b\u5230\u7684\u683c\u5c40\uff1aNetlink\u88ab\u8bbe\u8ba1\u6210\u4e00\u4e2a\u65b0\u7684\u534f\u8bae\u57df\uff0cdomain\u3002<\/p>\n

Linux\u4e4b\u7236\u6258\u74e6\u65af\u66fe\u8bf4\u8fc7\u201cLinux is evolution, not intelligent design\u201d\u3002\u4ec0\u4e48\u610f\u601d\uff1f\u5c31\u662f\u8bf4\uff0cNetlink\u4e5f\u540c\u6837\u9075\u5faa\u4e86Linux\u7684\u67d0\u4e9b\u8bbe\u8ba1\u7406\u5ff5\uff0c\u5373\u6ca1\u6709\u5b8c\u6574\u7684\u89c4\u8303\u6587\u6863\uff0c\u4ea6\u6ca1\u6709\u8bbe\u8ba1\u6587\u6863\u3002\u53ea\u6709\u4ec0\u4e48\uff1f\u4f60\u61c2\u5f97---\u201cRead the f**king source code\u201d\u3002<\/p>\n

\u5f53\u7136\uff0c\u672c\u6587\u4e0d\u662f\u5206\u6790Netlink\u5728Linux\u4e0a\u7684\u5b9e\u73b0\u673a\u5236\uff0c\u800c\u662f\u5c31\u201c\u4ec0\u4e48\u662fNetlink\u201d\u4ee5\u53ca\u201c\u5982\u4f55\u7528\u597dNetlink\u201d\u7684\u8bdd\u9898\u548c\u5927\u5bb6\u505a\u4e2a\u5206\u4eab\uff0c\u53ea\u6709\u5728\u9047\u5230\u95ee\u9898\u65f6\u624d\u9700\u8981\u53bb\u9605\u8bfb\u5185\u6838\u6e90\u7801\u5f04\u6e05\u4e2a\u6240\u4ee5\u7136\u3002<\/p>\n

\u4ec0\u4e48\u662fNetlink<\/strong><\/div>\n

\u5173\u4e8eNetlink\u7684\u7406\u89e3\uff0c\u9700\u8981\u628a\u63e1\u51e0\u4e2a\u5173\u952e\u70b9\uff1a<\/p>\n

1\u3001\u9762\u5411\u6570\u636e\u62a5\u7684\u65e0\u8fde\u63a5\u6d88\u606f\u5b50\u7cfb\u7edf<\/p>\n

2\u3001\u57fa\u4e8e\u901a\u7528\u7684BSD Socket\u67b6\u6784\u800c\u5b9e\u73b0<\/p>\n

\u5173\u4e8e\u7b2c\u4e00\u70b9\u4f7f\u6211\u4eec\u5f88\u5bb9\u6613\u8054\u60f3\u5230UDP\u534f\u8bae\uff0c\u80fd\u60f3\u5230\u8fd9\u4e00\u70b9\u5c31\u975e\u5e38\u68d2\u4e86\u3002\u6309\u7740UDP\u534f\u8bae\u6765\u7406\u89e3Netlink\u4e0d\u662f\u4e0d\u65e0\u9053\u7406\uff0c\u53ea\u8981\u4f60\u80fd\u89e6\u7c7b\u65c1\u901a\uff0c\u505a\u5230\u201c\u6d3b\u5b66\u201d\uff0c\u5584\u4e8e\u603b\u7ed3\u5f52\u7eb3\u3001\u8054\u60f3\uff0c\u6700\u540e\u5b9e\u73b0\u77e5\u8bc6\u8fc1\u79fb\u8fd9\u5c31\u662f\u5b66\u4e60\u7684\u672c\u8d28\u3002Netlink\u53ef\u4ee5\u5b9e\u73b0\u5185\u6838->\u7528\u6237\u4ee5\u53ca\u7528\u6237->\u5185\u6838\u7684\u53cc\u5411\u3001\u5f02\u6b65\u7684\u6570\u636e\u901a\u4fe1\uff0c\u540c\u65f6\u5b83\u8fd8\u652f\u6301\u4e24\u4e2a\u7528\u6237\u8fdb\u7a0b\u4e4b\u95f4\u3001\u751a\u81f3\u4e24\u4e2a\u5185\u6838\u5b50\u7cfb\u7edf\u4e4b\u95f4\u7684\u6570\u636e\u901a\u4fe1\u3002\u672c\u6587\u4e2d\uff0c\u5bf9\u540e\u4e24\u8005\u6211\u4eec\u4e0d\u4e88\u8003\u8651\uff0c\u7126\u70b9\u96c6\u4e2d\u5728\u5982\u4f55\u5b9e\u73b0\u7528\u6237<->\u5185\u6838\u4e4b\u95f4\u7684\u6570\u636e\u901a\u4fe1\u3002<\/p>\n

\u770b\u5230\u7b2c\u4e8c\u70b9\u8111\u6d77\u4e2d\u662f\u4e0d\u662f\u77ac\u95f4\u95ea\u73b0\u4e86\u4e0b\u9762\u8fd9\u5f20\u56fe\u7247\u5462\uff1f\u5982\u679c\u662f\uff0c\u5219\u8bf4\u660e\u4f60\u786e\u5b9e\u6709\u6167\u6839\uff1b\u5f53\u7136\uff0c\u4e0d\u662f\u4e5f\u6ca1\u5173\u7cfb\uff0c\u6167\u6839\u53ef\u4ee5\u6162\u6162\u957f\u561b\uff0c\u5475\u5475\u3002<\/p>\n

\u5728\u540e\u9762\u5b9e\u6218Netlink\u5957\u63a5\u5b57\u7f16\u7a0b\u65f6\u6211\u4eec\u4e3b\u8981\u4f1a\u7528\u5230socket()\uff0cbind()\uff0csendmsg()<\/p>\n

\u548crecvmsg()\u7b49\u7cfb\u7edf\u8c03\u7528\uff0c\u5f53\u7136\u8fd8\u6709socket\u63d0\u4f9b\u7684\u8f6e\u8bad(polling)\u673a\u5236\u3002<\/p>\n

Netlink\u901a\u4fe1\u7c7b\u578b<\/strong><\/div>\n

Netlink\u652f\u6301\u4e24\u79cd\u7c7b\u578b\u7684\u901a\u4fe1\u65b9\u5f0f\uff1a\u5355\u64ad\u548c\u591a\u64ad\u3002<\/p>\n

\u5355\u64ad\uff1a\u7ecf\u5e38\u7528\u4e8e\u4e00\u4e2a\u7528\u6237\u8fdb\u7a0b\u548c\u4e00\u4e2a\u5185\u6838\u5b50\u7cfb\u7edf\u4e4b\u95f41:1\u7684\u6570\u636e\u901a\u4fe1\u3002\u7528\u6237\u7a7a\u95f4\u53d1\u9001\u547d\u4ee4\u5230\u5185\u6838\uff0c\u7136\u540e\u4ece\u5185\u6838\u63a5\u53d7\u547d\u4ee4\u7684\u8fd4\u56de\u7ed3\u679c\u3002<\/p>\n

\u591a\u64ad\uff1a\u7ecf\u5e38\u7528\u4e8e\u4e00\u4e2a\u5185\u6838\u8fdb\u7a0b\u548c\u591a\u4e2a\u7528\u6237\u8fdb\u7a0b\u4e4b\u95f4\u76841:N\u7684\u6570\u636e\u901a\u4fe1\u3002\u5185\u6838\u4f5c\u4e3a\u4f1a\u8bdd\u7684\u53d1\u8d77\u8005\uff0c\u7528\u6237\u7a7a\u95f4\u7684\u5e94\u7528\u7a0b\u5e8f\u662f\u63a5\u6536\u8005\u3002\u4e3a\u4e86\u5b9e\u73b0\u8fd9\u4e2a\u529f\u80fd\uff0c\u5185\u6838\u7a7a\u95f4\u7684\u7a0b\u5e8f\u4f1a\u521b\u5efa\u4e00\u4e2a\u591a\u64ad\u7ec4\uff0c\u7136\u540e\u6240\u6709\u7528\u6237\u7a7a\u95f4\u7684\u5bf9\u8be5\u5185\u6838\u8fdb\u7a0b\u53d1\u9001\u7684\u6d88\u606f\u611f\u5174\u8da3\u7684\u8fdb\u7a0b\u90fd\u52a0\u5165\u5230\u8be5\u7ec4\u5373\u53ef\u63a5\u6536\u6765\u81ea\u5185\u6838\u53d1\u9001\u7684\u6d88\u606f\u4e86\u3002\u5982\u4e0b\uff1a<\/p>\n

\"\"<\/p>\n

\u5176\u4e2d\u8fdb\u7a0bA\u548c\u5b50\u7cfb\u7edf1\u4e4b\u95f4\u662f\u5355\u64ad\u901a\u4fe1\uff0c\u8fdb\u7a0bB\u3001C\u548c\u5b50\u7cfb\u7edf2\u662f\u591a\u64ad\u901a\u4fe1\u3002\u4e0a\u56fe\u8fd8\u5411\u6211\u4eec\u8bf4\u660e\u4e86\u4e00\u4e2a\u4fe1\u606f\u3002\u4ece\u7528\u6237\u7a7a\u95f4\u4f20\u9012\u5230\u5185\u6838\u7684\u6570\u636e\u662f\u4e0d\u9700\u8981\u6392\u961f\u7684\uff0c\u5373\u5176\u64cd\u4f5c\u662f\u540c\u6b65\u5b8c\u6210\uff1b\u800c\u4ece\u5185\u6838\u7a7a\u95f4\u5411\u7528\u6237\u7a7a\u95f4\u4f20\u9012\u6570\u636e\u65f6\u9700\u8981\u6392\u961f\uff0c\u662f\u5f02\u6b65\u7684\u3002\u4e86\u89e3\u4e86\u8fd9\u4e00\u70b9\u5728\u5f00\u53d1\u57fa\u4e8eNetlink\u7684\u5e94\u7528\u6a21\u5757\u65f6\u53ef\u4ee5\u4f7f\u6211\u4eec\u5c11\u8d70\u5f88\u591a\u5f2f\u8def\u3002\u5047\u5982\uff0c\u4f60\u5411\u5185\u6838\u53d1\u9001\u4e86\u4e00\u4e2a\u6d88\u606f\u9700\u8981\u83b7\u53d6\u5185\u6838\u4e2d\u67d0\u4e9b\u4fe1\u606f\uff0c\u6bd4\u5982\u8def\u7531\u8868\uff0c\u6216\u5176\u4ed6\u4fe1\u606f\uff0c\u5982\u679c\u8def\u7531\u8868\u8fc7\u4e8e\u5e9e\u5927\uff0c\u90a3\u4e48\u5185\u6838\u5728\u901a\u8fc7Netlink\u5411\u4f60\u8fd4\u56de\u6570\u636e\u65f6\uff0c\u4f60\u53ef\u4ee5\u597d\u751f\u7422\u78e8\u4e00\u4e0b\u5982\u4f55\u63a5\u6536\u8fd9\u4e9b\u6570\u636e\u7684\u95ee\u9898\uff0c\u6bd5\u7adf\u4f60\u5df2\u7ecf\u770b\u5230\u4e86\u90a3\u4e2a\u8f93\u51fa\u961f\u5217\u4e86\uff0c\u4e0d\u80fd\u89c6\u800c\u4e0d\u89c1\u554a\u3002<\/p>\n

Netlink\u7684\u6d88\u606f\u683c\u5f0f<\/strong><\/div>\n

Netlink\u6d88\u606f\u7531\u4e24\u90e8\u5206\u7ec4\u6210\uff1a\u6d88\u606f\u5934\u548c\u6709\u6548\u6570\u636e\u8f7d\u8377\uff0c\u4e14\u6574\u4e2aNetlink\u6d88\u606f\u662f4\u5b57\u8282\u5bf9\u9f50\uff0c\u4e00\u822c\u6309\u4e3b\u673a\u5b57\u8282\u5e8f\u8fdb\u884c\u4f20\u9012\u3002\u6d88\u606f\u5934\u4e3a\u56fa\u5b9a\u768416\u5b57\u8282\uff0c\u6d88\u606f\u4f53\u957f\u5ea6\u53ef\u53d8\uff1a<\/p>\n

\"\"<\/p>\n

Netlink\u7684\u6d88\u606f\u5934<\/strong><\/div>\n

\u6d88\u606f\u5934\u5b9a\u4e49\u5728<include\/linux\/netlink.h>\u6587\u4ef6\u91cc\uff0c\u7531\u7ed3\u6784\u4f53nlmsghdr\u8868\u793a\uff1a<\/p>\n

\u70b9\u51fb(\u6b64\u5904)\u6298\u53e0\u6216\u6253\u5f00<\/p>\n

    \n
  1. struct nlmsghdr<\/li>\n
  2. {<\/li>\n
  3. \u00a0\u00a0\u00a0\u00a0__u32\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0nlmsg_len;\u00a0\u00a0\u00a0\u00a0\/*\u00a0Length of message including header\u00a0*\/<\/li>\n
  4. \u00a0\u00a0\u00a0\u00a0__u16\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0nlmsg_type;\u00a0\u00a0\u00a0\u00a0\/*\u00a0Message content\u00a0*\/<\/li>\n
  5. \u00a0\u00a0\u00a0\u00a0__u16\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0nlmsg_flags;\u00a0\u00a0\u00a0\u00a0\/*\u00a0Additional flags\u00a0*\/<\/li>\n
  6. \u00a0\u00a0\u00a0\u00a0__u32\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0nlmsg_seq;\u00a0\u00a0\u00a0\u00a0\/*\u00a0Sequence number\u00a0*\/<\/li>\n
  7. \u00a0\u00a0\u00a0\u00a0__u32\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0nlmsg_pid;\u00a0\u00a0\u00a0\u00a0\/*\u00a0Sending process PID\u00a0*\/<\/li>\n
  8. };<\/li>\n<\/ol>\n

    \u6d88\u606f\u5934\u4e2d\u5404\u6210\u5458\u5c5e\u6027\u7684\u89e3\u91ca\u53ca\u8bf4\u660e\uff1a<\/p>\n

    nlmsg_len\uff1a\u6574\u4e2a\u6d88\u606f\u7684\u957f\u5ea6\uff0c\u6309\u5b57\u8282\u8ba1\u7b97\u3002\u5305\u62ec\u4e86Netlink\u6d88\u606f\u5934\u672c\u8eab\u3002<\/p>\n

    nlmsg_type\uff1a\u6d88\u606f\u7684\u7c7b\u578b\uff0c\u5373\u662f\u6570\u636e\u8fd8\u662f\u63a7\u5236\u6d88\u606f\u3002\u76ee\u524d(\u5185\u6838\u7248\u672c2.6.21)Netlink\u4ec5\u652f\u6301\u56db\u79cd\u7c7b\u578b\u7684\u63a7\u5236\u6d88\u606f\uff0c\u5982\u4e0b\uff1a<\/p>\n

    NLMSG_NOOP-\u7a7a\u6d88\u606f\uff0c\u4ec0\u4e48\u4e5f\u4e0d\u505a\uff1b<\/p>\n

    NLMSG_ERROR-\u6307\u660e\u8be5\u6d88\u606f\u4e2d\u5305\u542b\u4e00\u4e2a\u9519\u8bef\uff1b<\/p>\n

    NLMSG_DONE-\u5982\u679c\u5185\u6838\u901a\u8fc7Netlink\u961f\u5217\u8fd4\u56de\u4e86\u591a\u4e2a\u6d88\u606f\uff0c\u90a3\u4e48\u961f\u5217\u7684\u6700\u540e\u4e00\u6761\u6d88\u606f\u7684\u7c7b\u578b\u4e3aNLMSG_DONE\uff0c\u5176\u4f59\u6240\u6709\u6d88\u606f\u7684nlmsg_flags\u5c5e\u6027\u90fd\u88ab\u8bbe\u7f6eNLM_F_MULTI\u4f4d\u6709\u6548\u3002<\/p>\n

    NLMSG_OVERRUN-\u6682\u65f6\u6ca1\u7528\u5230\u3002<\/p>\n

    nlmsg_flags\uff1a\u9644\u52a0\u5728\u6d88\u606f\u4e0a\u7684\u989d\u5916\u8bf4\u660e\u4fe1\u606f\uff0c\u5982\u4e0a\u9762\u63d0\u5230\u7684NLM_F_MULTI\u3002\u6458\u5f55\u5982\u4e0b\uff1a<\/p>\n\n\n\n\n\n\n\n\n
    \u6807\u8bb0<\/td>\n\u4f5c\u7528\u53ca\u8bf4\u660e<\/td>\n<\/tr>\n
    NLM_F_REQUEST<\/td>\n\u5982\u679c\u6d88\u606f\u4e2d\u6709\u8be5\u6807\u8bb0\u4f4d\uff0c\u8bf4\u660e\u8fd9\u662f\u4e00\u4e2a\u8bf7\u6c42\u6d88\u606f\u3002\u6240\u6709\u4ece\u7528\u6237\u7a7a\u95f4\u5230\u5185\u6838\u7a7a\u95f4\u7684\u6d88\u606f\u90fd\u8981\u8bbe\u7f6e\u8be5\u4f4d\uff0c\u5426\u5219\u5185\u6838\u5c06\u5411\u7528\u6237\u8fd4\u56de\u4e00\u4e2aEINVAL\u65e0\u6548\u53c2\u6570\u7684\u9519\u8bef<\/td>\n<\/tr>\n
    NLM_F_MULTI<\/td>\n\u6d88\u606f\u4ece\u7528\u6237->\u5185\u6838\u662f\u540c\u6b65\u7684\u7acb\u523b\u5b8c\u6210\uff0c\u800c\u4ece\u5185\u6838->\u7528\u6237\u5219\u9700\u8981\u6392\u961f\u3002\u5982\u679c\u5185\u6838\u4e4b\u524d\u6536\u5230\u8fc7\u6765\u81ea\u7528\u6237\u7684\u6d88\u606f\u4e2d\u6709NLM_F_DUMP\u4f4d\u4e3a1\u7684\u6d88\u606f\uff0c\u90a3\u4e48\u5185\u6838\u5c31\u4f1a\u5411\u7528\u6237\u7a7a\u95f4\u53d1\u9001\u4e00\u4e2a\u7531\u591a\u4e2aNetlink\u6d88\u606f\u7ec4\u6210\u7684\u94fe\u8868\u3002\u9664\u4e86\u6700\u540e\u4e2a\u6d88\u606f\u5916\uff0c\u5176\u4f59\u6bcf\u6761\u6d88\u606f\u4e2d\u90fd\u8bbe\u7f6e\u4e86\u8be5\u4f4d\u6709\u6548\u3002<\/td>\n<\/tr>\n
    NLM_F_ACK<\/td>\n\u8be5\u6d88\u606f\u662f\u5185\u6838\u5bf9\u6765\u81ea\u7528\u6237\u7a7a\u95f4\u7684NLM_F_REQUEST\u6d88\u606f\u7684\u54cd\u5e94<\/td>\n<\/tr>\n
    NLM_F_ECHO<\/td>\n\u5982\u679c\u4ece\u7528\u6237\u7a7a\u95f4\u53d1\u7ed9\u5185\u6838\u7684\u6d88\u606f\u4e2d\u8be5\u6807\u8bb0\u4e3a1\uff0c\u5219\u8bf4\u660e\u7528\u6237\u7684\u5e94\u7528\u8fdb\u7a0b\u8981\u6c42\u5185\u6838\u5c06\u7528\u6237\u53d1\u7ed9\u5b83\u7684\u6bcf\u6761\u6d88\u606f\u901a\u8fc7\u5355\u64ad\u7684\u5f62\u5f0f\u518d\u53d1\u9001\u7ed9\u7528\u6237\u8fdb\u7a0b\u3002\u548c\u6211\u4eec\u901a\u5e38\u8bf4\u7684\u201c\u56de\u663e\u201d\u529f\u80fd\u7c7b\u4f3c\u3002<\/td>\n<\/tr>\n
    \u2026<\/td>\n\u2026<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

    \u5927\u5bb6\u53ea\u8981\u77e5\u9053nlmsg_flags\u6709\u591a\u79cd\u53d6\u503c\u5c31\u53ef\u4ee5\uff0c\u81f3\u4e8e\u6bcf\u79cd\u503c\u7684\u4f5c\u7528\u548c\u610f\u4e49\uff0c\u901a\u8fc7\u8c37\u6b4c\u548c\u6e90\u4ee3\u7801\u4e00\u5b9a\u53ef\u4ee5\u627e\u5230\u7b54\u6848\uff0c\u8fd9\u91cc\u5c31\u4e0d\u5c55\u5f00\u4e86\u3002\u4e0a\u4e00\u5f202.6.21\u5185\u6838\u4e2d\u6240\u6709\u7684\u53d6\u503c\u60c5\u51b5\uff1a<\/p>\n

    \"\"<\/p>\n

    nlmsg_seq\uff1a\u6d88\u606f\u5e8f\u5217\u53f7\u3002\u56e0\u4e3aNetlink\u662f\u9762\u5411\u6570\u636e\u62a5\u7684\uff0c\u6240\u4ee5\u5b58\u5728\u4e22\u5931\u6570\u636e\u7684\u98ce\u9669\uff0c\u4f46\u662fNetlink\u63d0\u4f9b\u4e86\u5982\u4f55\u786e\u4fdd\u6d88\u606f\u4e0d\u4e22\u5931\u7684\u673a\u5236\uff0c\u8ba9\u7a0b\u5e8f\u5f00\u53d1\u4eba\u5458\u6839\u636e\u5176\u5b9e\u9645\u9700\u6c42\u800c\u5b9e\u73b0\u3002\u6d88\u606f\u5e8f\u5217\u53f7\u4e00\u822c\u548cNLM_F_ACK\u7c7b\u578b\u7684\u6d88\u606f\u8054\u5408\u4f7f\u7528\uff0c\u5982\u679c\u7528\u6237\u7684\u5e94\u7528\u7a0b\u5e8f\u9700\u8981\u4fdd\u8bc1\u5176\u53d1\u9001\u7684\u6bcf\u6761\u6d88\u606f\u90fd\u6210\u529f\u88ab\u5185\u6838\u6536\u5230\u7684\u8bdd\uff0c\u90a3\u4e48\u5b83\u53d1\u9001\u6d88\u606f\u65f6\u9700\u8981\u7528\u6237\u7a0b\u5e8f\u81ea\u5df1\u8bbe\u7f6e\u5e8f\u53f7\uff0c\u5185\u6838\u6536\u5230\u8be5\u6d88\u606f\u540e\u5bf9\u63d0\u53d6\u5176\u4e2d\u7684\u5e8f\u5217\u53f7\uff0c\u7136\u540e\u5728\u53d1\u9001\u7ed9\u7528\u6237\u7a0b\u5e8f\u56de\u5e94\u6d88\u606f\u91cc\u8bbe\u7f6e\u540c\u6837\u7684\u5e8f\u5217\u53f7\u3002\u6709\u70b9\u7c7b\u4f3c\u4e8eTCP\u7684\u54cd\u5e94\u548c\u786e\u8ba4\u673a\u5236\u3002<\/p>\n

    \u6ce8\u610f\uff1a\u5f53\u5185\u6838\u4e3b\u52a8\u5411\u7528\u6237\u7a7a\u95f4\u53d1\u9001\u5e7f\u64ad\u6d88\u606f\u65f6\uff0c\u6d88\u606f\u4e2d\u7684\u8be5\u5b57\u6bb5\u603b\u662f\u4e3a0\u3002<\/strong><\/p>\n

    nlmsg_pid\uff1a\u5f53\u7528\u6237\u7a7a\u95f4\u7684\u8fdb\u7a0b\u548c\u5185\u6838\u7a7a\u95f4\u7684\u67d0\u4e2a\u5b50\u7cfb\u7edf\u4e4b\u95f4\u901a\u8fc7Netlink\u5efa\u7acb\u4e86\u6570\u636e\u4ea4\u6362\u7684\u901a\u9053\u540e\uff0cNetlink\u4f1a\u4e3a\u6bcf\u4e2a\u8fd9\u6837\u7684\u901a\u9053\u5206\u914d\u4e00\u4e2a\u552f\u4e00\u7684\u6570\u5b57\u6807\u8bc6\u3002\u5176\u4e3b\u8981\u4f5c\u7528\u5c31\u662f\u5c06\u6765\u81ea\u7528\u6237\u7a7a\u95f4\u7684\u8bf7\u6c42\u6d88\u606f\u548c\u54cd\u5e94\u6d88\u606f\u8fdb\u884c\u5173\u8054\u3002\u8bf4\u5f97\u76f4\u767d\u4e00\u70b9\uff0c\u5047\u5982\u7528\u6237\u7a7a\u95f4\u5b58\u5728\u591a\u4e2a\u7528\u6237\u8fdb\u7a0b\uff0c\u5185\u6838\u7a7a\u95f4\u540c\u6837\u5b58\u5728\u591a\u4e2a\u8fdb\u7a0b\uff0cNetlink\u5fc5\u987b\u63d0\u4f9b\u4e00\u79cd\u673a\u5236\u7528\u4e8e\u786e\u4fdd\u6bcf\u4e00\u5bf9\u201c\u7528\u6237-\u5185\u6838\u201d\u7a7a\u95f4\u901a\u4fe1\u7684\u8fdb\u7a0b\u4e4b\u95f4\u7684\u6570\u636e\u4ea4\u4e92\u4e0d\u4f1a\u53d1\u751f\u7d0a\u4e71\u3002<\/p>\n

    \"\"<\/p>\n

    \u5373\uff0c\u8fdb\u7a0bA\u3001B\u901a\u8fc7Netlink\u5411\u5b50\u7cfb\u7edf1\u83b7\u53d6\u4fe1\u606f\u65f6\uff0c\u5b50\u7cfb\u7edf1\u5fc5\u987b\u786e\u4fdd\u56de\u9001\u7ed9\u8fdb\u7a0bA\u7684\u54cd\u5e94\u6570\u636e\u4e0d\u4f1a\u53d1\u5230\u8fdb\u7a0bB\u90a3\u91cc\u3002\u4e3b\u8981\u9002\u7528\u4e8e\u7528\u6237\u7a7a\u95f4\u7684\u8fdb\u7a0b\u4ece\u5185\u6838\u7a7a\u95f4\u83b7\u53d6\u6570\u636e\u7684\u573a\u666f\u3002\u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\u7528\u6237\u7a7a\u95f4\u7684\u8fdb\u7a0b\u5728\u5411\u5185\u6838\u53d1\u9001\u6d88\u606f\u65f6\u4e00\u822c\u901a\u8fc7\u7cfb\u7edf\u8c03\u7528getpid()\u5c06\u5f53\u524d\u8fdb\u7a0b\u7684\u8fdb\u7a0b\u53f7\u8d4b\u7ed9\u8be5\u53d8\u91cf\uff0c\u5373\u7528\u6237\u7a7a\u95f4\u7684\u8fdb\u7a0b\u5e0c\u671b\u5f97\u5230\u5185\u6838\u7684\u54cd\u5e94\u65f6\u624d\u4f1a\u8fd9\u4e48\u505a\u3002\u4ece\u5185\u6838\u4e3b\u52a8\u53d1\u9001\u5230\u7528\u6237\u7a7a\u95f4\u7684\u6d88\u606f\u8be5\u5b57\u6bb5\u90fd\u88ab\u8bbe\u7f6e\u4e3a0\u3002<\/p>\n

    Netlink\u7684\u6d88\u606f\u4f53<\/strong><\/div>\n

    Netlink\u7684\u6d88\u606f\u4f53\u91c7\u7528TLV(Type-Length-Value)\u683c\u5f0f\uff1a<\/p>\n

    \"\"<\/p>\n

    Netlink\u6bcf\u4e2a\u5c5e\u6027\u90fd\u7531<include\/linux\/netlink.h>\u6587\u4ef6\u91cc\u7684struct nlattr{}<\/em>\u6765\u8868\u793a\uff1a<\/p>\n

    \"\"<\/p>\n

    Netlink\u63d0\u4f9b\u7684\u9519\u8bef\u6307\u793a\u6d88\u606f<\/strong><\/p>\n

    \u5185\u5bb9<\/strong><\/div>\n

    \u5f53\u7528\u6237\u7a7a\u95f4\u7684\u5e94\u7528\u7a0b\u5e8f\u548c\u5185\u6838\u7a7a\u95f4\u7684\u8fdb\u7a0b\u4e4b\u95f4\u901a\u8fc7Netlink\u901a\u4fe1\u65f6\u53d1\u751f\u4e86\u9519\u8bef\uff0cNetlink\u5fc5\u987b\u5411\u7528\u6237\u7a7a\u95f4\u901a\u62a5\u8fd9\u79cd\u9519\u8bef\u3002Netlink\u5bf9\u9519\u8bef\u6d88\u606f\u8fdb\u884c\u4e86\u5355\u72ec\u5c01\u88c5\uff0c<include\/linux\/netlink.h>\uff1a<\/p>\n

    \u70b9\u51fb(\u6b64\u5904)\u6298\u53e0\u6216\u6253\u5f00<\/p>\n

      \n
    1. struct nlmsgerr<\/li>\n
    2. {<\/li>\n
    3. \u00a0\u00a0\u00a0\u00a0int\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0error;\u00a0\/\/\u6807\u51c6\u7684\u9519\u8bef\u7801\uff0c\u5b9a\u4e49\u5728errno.h\u5934\u6587\u4ef6\u4e2d\u3002\u53ef\u4ee5\u7528perror()\u6765\u89e3\u91ca<\/li>\n
    4. \u00a0\u00a0\u00a0\u00a0struct nlmsghdr msg;\u00a0\/\/\u6307\u660e\u4e86\u54ea\u6761\u6d88\u606f\u89e6\u53d1\u4e86\u7ed3\u6784\u4f53\u4e2derror\u8fd9\u4e2a\u9519\u8bef\u503c<\/li>\n
    5. };<\/li>\n<\/ol>\n
      Netlink\u7f16\u7a0b\u9700\u8981\u6ce8\u610f\u7684\u95ee\u9898<\/strong><\/div>\n

      \u57fa\u4e8eNetlink\u7684\u7528\u6237-\u5185\u6838\u901a\u4fe1\uff0c\u6709\u4e24\u79cd\u60c5\u51b5\u53ef\u80fd\u4f1a\u5bfc\u81f4\u4e22\u5305\uff1a<\/p>\n

      1\u3001\u5185\u5b58\u8017\u5c3d\uff1b<\/p>\n

      2\u3001\u7528\u6237\u7a7a\u95f4\u63a5\u6536\u8fdb\u7a0b\u7684\u7f13\u51b2\u533a\u6ea2\u51fa\u3002\u5bfc\u81f4\u7f13\u51b2\u533a\u6ea2\u51fa\u7684\u4e3b\u8981\u539f\u56e0\u6709\u53ef\u80fd\u662f\uff1a\u7528\u6237\u7a7a\u95f4\u7684\u8fdb\u7a0b\u8fd0\u884c\u592a\u6162\uff1b\u6216\u8005\u63a5\u6536\u961f\u5217\u592a\u77ed\u3002<\/p>\n

      \u5982\u679cNetlink\u4e0d\u80fd\u5c06\u6d88\u606f\u6b63\u786e\u4f20\u9012\u5230\u7528\u6237\u7a7a\u95f4\u7684\u63a5\u6536\u8fdb\u7a0b\uff0c\u90a3\u4e48\u7528\u6237\u7a7a\u95f4\u7684\u63a5\u6536\u8fdb\u7a0b\u5728\u8c03\u7528recvmsg()\u7cfb\u7edf\u8c03\u7528\u65f6\u5c31\u4f1a\u8fd4\u56de\u4e00\u4e2a\u5185\u5b58\u4e0d\u8db3(ENOBUFS)\u7684\u9519\u8bef\uff0c\u8fd9\u4e00\u70b9\u9700\u8981\u6ce8\u610f\u3002\u6362\u53e5\u8bdd\u8bf4\uff0c\u7f13\u51b2\u533a\u6ea2\u51fa\u7684\u60c5\u51b5\u662f\u4e0d\u4f1a\u53d1\u9001\u5728\u4ece\u7528\u6237->\u5185\u6838\u7684sendmsg()\u7cfb\u7edf\u8c03\u7528\u91cc\uff0c\u539f\u56e0\u524d\u9762\u6211\u4eec\u4e5f\u8bf4\u8fc7\u4e86\uff0c\u8bf7\u5927\u5bb6\u81ea\u5df1\u601d\u8003\u4e00\u4e0b\u3002<\/p>\n

      \u5f53\u7136\uff0c\u5982\u679c\u4f7f\u7528\u7684\u662f\u963b\u585e\u578bsocket\u901a\u4fe1\uff0c\u4e5f\u5c31\u4e0d\u5b58\u5728\u5185\u5b58\u8017\u5c3d\u7684\u9690\u60a3\u4e86\uff0c\u8fd9\u53c8\u662f\u4e3a\u4ec0\u4e48\u5462\uff1f\u8d76\u7d27\u53bb\u8c37\u6b4c\u4e00\u4e0b\uff0c\u67e5\u67e5\u4ec0\u4e48\u662f\u963b\u585e\u578bsocket\u5427\u3002\u5b66\u800c\u4e0d\u601d\u5219\u7f54\uff0c\u601d\u800c\u4e0d\u5b66\u5219\u6b86\u561b\u3002<\/p>\n

      Netlink\u7684\u5730\u5740\u7ed3\u6784\u4f53<\/strong><\/div>\n

      \u5728TCP\u535a\u6587\u4e2d\u6211\u4eec\u63d0\u5230\u8fc7\u5728Internet\u7f16\u7a0b\u8fc7\u7a0b\u4e2d\u6240\u7528\u5230\u7684\u5730\u5740\u7ed3\u6784\u4f53\u548c\u6807\u51c6\u5730\u5740\u7ed3\u6784\u4f53\uff0c\u5b83\u4eec\u548cNetlink\u5730\u5740\u7ed3\u6784\u4f53\u7684\u5173\u7cfb\u5982\u4e0b\uff1a<\/p>\n

      struct sockaddr_nl{}\u7684\u8be6\u7ec6\u5b9a\u4e49\u548c\u63cf\u8ff0\u5982\u4e0b\uff1a<\/p>\n

      \"\"<\/p>\n

      \u70b9\u51fb(\u6b64\u5904)\u6298\u53e0\u6216\u6253\u5f00<\/p>\n

        \n
      1. struct sockaddr_nl<\/li>\n
      2. {<\/li>\n
      3. \u00a0\u00a0\u00a0\u00a0sa_family_t\u00a0\u00a0\u00a0\u00a0nl_family;\u00a0\u00a0\u00a0\u00a0\/*\u8be5\u5b57\u6bb5\u603b\u662f\u4e3aAF_NETLINK\u00a0\u00a0\u00a0\u00a0*\/<\/li>\n
      4. \u00a0\u00a0\u00a0\u00a0unsigned short\u00a0\u00a0\u00a0\u00a0nl_pad;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\/*\u00a0\u76ee\u524d\u672a\u7528\u5230\uff0c\u586b\u5145\u4e3a0*\/<\/li>\n
      5. \u00a0\u00a0\u00a0\u00a0__u32\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0nl_pid;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\/*\u00a0process pid\u00a0\u00a0\u00a0\u00a0*\/<\/li>\n
      6. \u00a0\u00a0\u00a0\u00a0__u32\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0nl_groups;\u00a0\u00a0\u00a0\u00a0\/*\u00a0multicast groups mask\u00a0*\/<\/li>\n
      7. };<\/li>\n<\/ol>\n

        nl_pid\uff1a\u8be5\u5c5e\u6027\u4e3a\u53d1\u9001\u6216\u63a5\u6536\u6d88\u606f\u7684\u8fdb\u7a0bID\uff0c\u524d\u9762\u6211\u4eec\u4e5f\u8bf4\u8fc7\uff0cNetlink\u4e0d\u4ec5\u53ef\u4ee5\u5b9e\u73b0\u7528\u6237-\u5185\u6838\u7a7a\u95f4\u7684\u901a\u4fe1\u8fd8\u53ef\u4f7f\u73b0\u5b9e\u7528\u6237\u7a7a\u95f4\u4e24\u4e2a\u8fdb\u7a0b\u4e4b\u95f4\uff0c\u6216\u5185\u6838\u7a7a\u95f4\u4e24\u4e2a\u8fdb\u7a0b\u4e4b\u95f4\u7684\u901a\u4fe1\u3002\u8be5\u5c5e\u6027\u4e3a0\u65f6\u4e00\u822c\u9002\u7528\u4e8e\u5982\u4e0b\u4e24\u79cd\u60c5\u51b5\uff1a<\/p>\n

        \u7b2c\u4e00\uff0c\u6211\u4eec\u8981\u53d1\u9001\u7684\u76ee\u7684\u5730\u662f\u5185\u6838\uff0c\u5373\u4ece\u7528\u6237\u7a7a\u95f4\u53d1\u5f80\u5185\u6838\u7a7a\u95f4\u65f6\uff0c\u6211\u4eec\u6784\u9020\u7684Netlink\u5730\u5740\u7ed3\u6784\u4f53\u4e2dnl_pid\u901a\u5e38\u60c5\u51b5\u4e0b\u90fd\u7f6e\u4e3a0\u3002\u8fd9\u91cc\u6709\u4e00\u70b9\u9700\u8981\u8ddf\u5927\u5bb6\u4ea4\u4ee3\u4e00\u4e0b\uff0c\u5728Netlink\u89c4\u8303\u91cc\uff0cPID\u5168\u79f0\u662fPort-ID(32bits)\uff0c\u5176\u4e3b\u8981\u4f5c\u7528\u662f\u7528\u4e8e\u552f\u4e00\u7684\u6807\u8bc6\u4e00\u4e2a\u57fa\u4e8enetlink\u7684socket\u901a\u9053\u3002\u901a\u5e38\u60c5\u51b5\u4e0bnl_pid\u90fd\u8bbe\u7f6e\u4e3a\u5f53\u524d\u8fdb\u7a0b\u7684\u8fdb\u7a0b\u53f7\u3002\u7136\u800c\uff0c\u5bf9\u4e8e\u4e00\u4e2a\u8fdb\u7a0b\u7684\u591a\u4e2a\u7ebf\u7a0b\u540c\u65f6\u4f7f\u7528netlink socket\u7684\u60c5\u51b5\uff0cnl_pid\u7684\u8bbe\u7f6e\u4e00\u822c\u91c7\u7528\u5982\u4e0b\u8fd9\u4e2a\u6837\u5b50\u6765\u5b9e\u73b0\uff1a<\/p>\n

        \u70b9\u51fb(\u6b64\u5904)\u6298\u53e0\u6216\u6253\u5f00<\/p>\n

          \n
        1. pthread_self()\u00a0<<\u00a016\u00a0|\u00a0getpid();<\/li>\n<\/ol>\n

          \u7b2c\u4e8c\uff0c\u4ece\u5185\u6838\u53d1\u51fa\u7684\u591a\u64ad\u62a5\u6587\u5230\u7528\u6237\u7a7a\u95f4\u65f6\uff0c\u5982\u679c\u7528\u6237\u7a7a\u95f4\u7684\u8fdb\u7a0b\u5904\u5728\u8be5\u591a\u64ad\u7ec4\u4e2d\uff0c\u90a3\u4e48\u5176\u5730\u5740\u7ed3\u6784\u4f53\u4e2dnl_pid\u4e5f\u8bbe\u7f6e\u4e3a0\uff0c\u540c\u65f6\u8fd8\u8981\u7ed3\u5408\u4e0b\u9762\u4ecb\u7ecd\u5230\u7684\u53e6\u4e00\u4e2a\u5c5e\u6027\u3002<\/p>\n

          nl_groups\uff1a\u5982\u679c\u7528\u6237\u7a7a\u95f4\u7684\u8fdb\u7a0b\u5e0c\u671b\u52a0\u5165\u67d0\u4e2a\u591a\u64ad\u7ec4\uff0c\u5219\u5fc5\u987b\u6267\u884cbind()\u7cfb\u7edf\u8c03\u7528\u3002\u8be5\u5b57\u6bb5\u6307\u660e\u4e86\u8c03\u7528\u8005\u5e0c\u671b\u52a0\u5165\u7684\u591a\u64ad\u7ec4\u53f7\u7684\u63a9\u7801<\/u><\/strong>(\u6ce8\u610f\u4e0d\u662f\u7ec4\u53f7\uff0c\u540e\u9762\u6211\u4eec\u4f1a\u8be6\u7ec6\u8bb2\u89e3\u8fd9\u4e2a\u5b57\u6bb5)\u3002\u5982\u679c\u8be5\u5b57\u6bb5\u4e3a0\u5219\u8868\u793a\u8c03\u7528\u8005\u4e0d\u5e0c\u671b\u52a0\u5165\u4efb\u4f55\u591a\u64ad\u7ec4\u3002\u5bf9\u4e8e\u6bcf\u4e2a\u96b6\u5c5e\u4e8eNetlink\u534f\u8bae\u57df\u7684\u534f\u8bae\uff0c\u6700\u591a\u53ef\u652f\u630132\u4e2a\u591a\u64ad\u7ec4(\u56e0\u4e3anl_groups\u7684\u957f\u5ea6\u4e3a32\u6bd4\u7279)\uff0c\u6bcf\u4e2a\u591a\u64ad\u7ec4\u7528\u4e00\u4e2a\u6bd4\u7279\u6765\u8868\u793a\u3002<\/p>\n

          \u5173\u4e8eNetlink\u5269\u4e0b\u7684\u77e5\u8bc6\u70b9\uff0c\u6211\u4eec\u5728\u540e\u9762\u7684\u5b9e\u6218\u73af\u8282\u6709\u7528\u5230\u65f6\u518d\u8ba8\u8bba\u3002<\/p>\n

          \u672a\u5b8c\uff0c\u5f85\u7eed\u2026<\/p>\n

           <\/p>\n

          \n

          \u539f\u6587\u6765\u81ea\uff1ahttp:\/\/blog.chinaunix.net\/uid-31475633-id-5767572.html<\/a><\/p>\n

          \u672c\u6587\u5730\u5740\uff1a http:\/\/lrxjmw.cn\/userspace-kernel.html<\/a>\u7f16\u8f91\uff1a\u674e\u5e05\uff0c\u5ba1\u6838\u5458\uff1a\u6682\u65e0<\/span><\/p>\n<\/blockquote>\n","protected":false},"excerpt":{"rendered":"

          \u5bfc\u8bfb Alan Cox\u5728\u5185\u68381.3\u7248\u672c\u7684\u5f00\u53d1\u9636\u6bb5\u6700\u5148\u5f15\u5165\u4e86Netlink\uff0c\u521a\u5f00\u59cb\u65f6Netlink\u662f\u4ee5\u5b57\u7b26\u9a71\u52a8\u63a5 […]<\/p>\n","protected":false},"author":63,"featured_media":86497,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-86472","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/86472","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/63"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=86472"}],"version-history":[{"count":5,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/86472\/revisions"}],"predecessor-version":[{"id":289364,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/86472\/revisions\/289364"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/86497"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=86472"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=86472"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=86472"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}