{"id":1097,"date":"2022-05-03T03:50:04","date_gmt":"2022-05-02T19:50:04","guid":{"rendered":"https:\/\/comzyh.com\/blog\/?p=1097"},"modified":"2022-05-03T03:50:05","modified_gmt":"2022-05-02T19:50:05","slug":"firewalld-%e6%8a%a5%e5%91%8a-python-nftables-failed-%e7%9a%84%e8%b0%83%e8%af%95%e6%80%9d%e8%b7%af","status":"publish","type":"post","link":"https:\/\/comzyh.com\/blog\/archives\/1097\/","title":{"rendered":"Firewalld \u62a5\u544a &#8216;python-nftables&#8217; failed \u7684\u8c03\u8bd5\u601d\u8def"},"content":{"rendered":"\n<p>\u521a\u521a\u628a\u8f6f\u8def\u7531\u4ece Ubuntu 20.04 \u5347\u7ea7\u5230 21.10\uff0c\u5347\u7ea7\u7684\u65f6\u5019\u544a\u8bc9\u6211\u4f1a\u628a <code>FirewallBackend<\/code> \u4ece <code>iptables<\/code> \u6539\u6210 <code>nftables<\/code>\u3002\u6211\u6b63\u597d\u60f3\u8bd5\u4e00\u8bd5\uff0c\u5c31\u8ba9\u4ed6\u6539\u4e86\u3002<\/p>\n\n\n\n<p>\u7ed3\u679c\u91cd\u542f\u4e4b\u540e <code>firewall-cmd --reload<\/code> \u4f1a\u62a5\u9519<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Error: COMMAND_FAILED: 'python-nftables' failed:\nJSON blob:\n{\"nftables\": &#91;{\"metainfo\": {\"json_schema_version\": 1}}, {\"add\": {\"table\": {\"family\": \"inet\", \"name\": \"firewalld_policy_drop\"}}}, {\"add\": {\"chain\": {\"family\": \"inet\", \"table\": \"firewalld_policy_drop\", \"name\": \"filter_input\", \"type\": \"filter\", \"hook\": \"input\", \"prio\": 9, \"policy\": \"drop\"}}}, {\"add\": {\"chain\": {\"family\": \"inet\", \"table\": \"firewalld_policy_drop\", \"name\": \"filter_forward\", \"type\": \"filter\", \"hook\": \"forward\", \"prio\": 9, \"policy\": \"drop\"}}}, {\"add\": {\"chain\": {\"family\": \"inet\", \"table\": \"firewalld_policy_drop\", \"name\": \"filter_output\", \"type\": \"filter\", \"hook\": \"output\", \"prio\": 9, \"policy\": \"drop\"}}}, {\"add\": {\"rule\": {\"family\": \"inet\", \"table\": \"firewalld_policy_drop\", \"chain\": \"filter_input\", \"expr\": &#91;{\"match\": {\"left\": {\"ct\": {\"key\": \"state\"}}, \"op\": \"in\", \"right\": {\"set\": &#91;\"established\", \"related\"]}}}, {\"accept\": null}]}}}, {\"add\": {\"rule\": {\"family\": \"inet\", \"table\": \"firewalld_policy_drop\", \"chain\": \"filter_forward\", \"expr\": &#91;{\"match\": {\"left\": {\"ct\": {\"key\": \"state\"}}, \"op\": \"in\", \"right\": {\"set\": &#91;\"established\", \"related\"]}}}, {\"accept\": null}]}}}, {\"add\": {\"rule\": {\"family\": \"inet\", \"table\": \"firewalld_policy_drop\", \"chain\": \"filter_output\", \"expr\": &#91;{\"match\": {\"left\": {\"ct\": {\"key\": \"state\"}}, \"op\": \"in\", \"right\": {\"set\": &#91;\"established\", \"related\"]}}}, {\"accept\": null}]}}}]}\n<\/code><\/pre>\n\n\n\n<p>\u4e0a\u9762\u7684JSON \u5185\u5bb9\u5df2\u7ecf\u7ed9\u51fa\u4e86\u51fa\u9519\u7684 nftables \u64cd\u4f5c\u5217\u8868\uff0cfirewalld \u5728\u64cd\u7eb5 nftables \u7684\u65f6\u5019\uff0c\u4f1a\u5c06\u591a\u4e2a\u8bf7\u6c42\u5408\u5e76\u4e3a\u4e00\u4e2a\u53d1\u7ed9 nftables,\u5bfc\u81f4\u5e76\u4e0d\u80fd\u77e5\u9053\u4e0a\u9762\u54ea\u4e00\u6761\u51fa\u4e86\u95ee\u9898\u3002\u770b\u5230bugzilla\u76f8\u5173\u7684\u8ba8\u8bba\uff0c\u53ef\u4ee5\u8ba9firewalld \u62c6\u5206\u8bf7\u6c42\u3002<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p><a href=\"https:\/\/bugzilla.redhat.com\/show_bug.cgi?id=1869451#c6\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/bugzilla.redhat.com\/show_bug.cgi?id=1869451#c6<\/a><\/p><cite><code>set IndividualCalls=yes in \/etc\/firewalld\/firewalld.conf. This will give a better indication of what rule fails to apply to nftables.<\/code><\/cite><\/blockquote>\n\n\n\n<p>\u4fee\u590d\u4e86\u4e00\u4e9b\u6211\u81ea\u5df1\u8303\u7684\u7b80\u5355\u9519\u8bef\u540e\uff0c\u795e\u5947\u7684\u4e8b\u60c5\u53d1\u751f\u4e86\uff0c\u5982\u679c <code>IndividualCalls=no<\/code> \uff0c\u90a3\u4e48\u95ee\u9898\u76f4\u63a5\u6d88\u5931, reload \u7684\u65f6\u5019\u76f4\u63a5success \u4e86\u3002\u7ecf\u8fc7\u5206\u6790\u53d1\u73b0\uff0c\u95ee\u9898\u8fd8\u662f\u51fa\u5728 firewalld \u6279\u91cf\u64cd\u4f5c nftables \u4e0a\u3002<\/p>\n\n\n\n<p>\u6211\u7684\u63a8\u6d4b\u662f\uff1a\u5982\u679c\u4e00\u6279\u64cd\u4f5c\u4e2d\u4efb\u4f55\u4e00\u4e2a\u6307\u4ee4\u5931\u8d25\uff0c\u8fd9\u6279\u64cd\u4f5c\u5c31\u4f1a\u88ab\u56de\u6eda\u3002\u90a3\u4e48\u5982\u679c\u8fd9\u6279\u64cd\u4f5c\u4e2d\u6709\u65b0\u5efa Chain \u4e4b\u7c7b\u7684\u64cd\u4f5c\uff0c\u5c31\u4f1a\u5bfc\u81f4 Chain \u6ca1\u6709\u88ab\u521b\u5efa\uff0c\u7136\u540e\u540e\u9762\u5c31\u4f1a\u51fa\u5404\u79cd\u83ab\u540d\u5176\u5999\u7684\u95ee\u9898\uff0c\u6211\u4eec\u770b\u5230\u7684\u9519\u8bef\u5e76\u975e\u6839\u672c\u539f\u56e0\u3002<\/p>\n\n\n\n<p>\u6211\u8fd9\u4e2a\u95ee\u9898\u7684\u6700\u7ec8\u539f\u56e0\u662f ipset \u4e2d \u6761\u76ee\u6709overlap.\u5220\u9664 ipset \u5e76\u5bfc\u5165\u6b63\u786e\u7684ipset\u4e4b\u540e\u95ee\u9898\u89e3\u51b3\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u8c03\u8bd5\u601d\u8def<\/h2>\n\n\n\n<p>\u672c\u8d28\u4e0a\uff0cfirewalld \u542f\u52a8\u7684\u65f6\u5019\u662f\u4e0d\u5e94\u8be5\u6709\u4efb\u4f55\u9519\u8bef\u624d\u5bf9\u3002\u90a3\u4e48\u6211\u4eec\u53ef\u4ee5\u5728<code>\/etc\/firewalld\/firewalld.conf<\/code>\u8bbe\u7f6e<code>IndividualCalls=yes<\/code>\uff0c\u7136\u540e\u91cd\u542ffirewalld\u3002<\/p>\n\n\n\n<p>\u8bf7\u6ce8\u610f\uff1a\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0cipset \u4e5f\u4f1a\u4e00\u6761\u4e00\u6761\u7684\u63d2\u5165\uff0c\u5982\u679c\u4f60\u6709\u4e0a\u4e07\u4e2aIP\uff0c\u90a3\u4e48\u8fd9\u4e2a\u63d2\u5165\u901f\u5ea6\u4e5f\u4f1a\u975e\u5e38\u6162\u3002\u5bfc\u81f4\u4f60\u53ef\u80fd\u4f1a\u89c9\u5f97 firewalld \u6839\u672c\u542f\u52a8\u4e0d\u8d77\u6765\u3002\u90a3\u4f60\u4e5f\u53ef\u4ee5\u9009\u62e9\u4e0d\u8bbe\u7f6e<code>IndividualCalls<\/code>\uff0c\u6216\u8005\u9009\u62e9\u6e05\u7a7aipset\u4ee5\u63d0\u5347\u542f\u52a8\u901f\u5ea6\u3002<\/p>\n\n\n\n<p>\u91cd\u542f\u4e4b\u540e\u53ef\u4ee5\u770b\u770b\u6700\u65b0\u4e00\u6b21\u542f\u52a8\u65e5\u5fd7\uff0c<strong>\u7b2c\u4e00\u4e2a python-nftables \u76f8\u5173\u9519\u8bef<\/strong>\u662f\u4ec0\u4e48\u3002\u5982\u679c\u4f60\u8bbe\u7f6e\u4e86<code>IndividualCalls=yes<\/code>\uff0c\u90a3\u4e48\u6b64\u65f6\u8bf7\u6309\u4ece\u65e9\u5230\u665a\u7684\u987a\u5e8f\u4f9d\u6b21\u6392\u67e5\u9519\u8bef\u5e76\u89e3\u51b3\u3002\u5982\u679c\u4f60\u9009\u62e9\u4fdd\u6301<code>IndividualCalls=no<\/code>\uff0c\u4e5f\u9700\u8981\u67e5\u770b<strong>\u7b2c\u4e00\u4e2a\u9519\u8bef<\/strong>\u662f\u4ec0\u4e48\uff0c\u628a <code>JSON blob:<\/code>\u540e\u9762\u90a3\u4e00\u6bb5Json \u4fdd\u5b58\u4e0b\u6765\u3002<\/p>\n\n\n\n<p>\u6211\u8fd9\u91cc\u63d0\u4f9b\u4e00\u4e2a\u5c0f\u5de5\u5177\uff0c\u80fd\u628a\u4e00\u6279\u64cd\u4f5c\u62c6\u89e3\u6210\u4e00\u4e2a\u4e2a\u64cd\u4f5c\uff0c\u7136\u540e\u6267\u884cnftables\u3002<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><\/div>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \" title=\"nftjson.py\" >#!\/bin\/env python3\n\nimport nftables\nimport json\nimport argparse\n\n\nif __name__ == '__main__':\n    parser = argparse.ArgumentParser(\"nftables-python json debugger\")\n    parser.add_argument(\"json_file\", type=str, help='File path of json file')\n\n    args = parser.parse_args()\n\n    nft = nftables.Nftables()\n    # nft.set_json_output(True)\n    nft.set_echo_output(True)\n    nft.set_handle_output(True)\n    with open(args.json_file, 'r') as f:\n        cmd_all = json.load(f)\n        for i in range(1, len(cmd_all['nftables'])):\n            cmd_res = {\n                \"nftables\": [\n                    cmd_all['nftables'][0],\n                    cmd_all['nftables'][i]\n                ]\n            }\n\n            rc, output, error = nft.json_cmd(cmd_res)\n            if rc != 0:\n                print(cmd_res)\n                print('rc = ', rc)\n                print('output = ',output )\n                print('error = ', error)\n<\/pre><\/div>\n\n\n\n<p>\u5c06\u521a\u624d\u7684 JSON \u4fdd\u5b58\u5230 \/tmp\/failed-nft.json  ,\u7136\u540e\u6267\u884c<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo python nftjson.py \/tmp\/failed-nft.json<\/code><\/pre>\n\n\n\n<p>\u5c31\u4f1a\u628a\u5931\u8d25\u7684\u6307\u4ee4\u548c\u9519\u8bef\u5355\u72ec\u8f93\u51fa\u51fa\u6765\uff0c\u65b9\u4fbf\u8c03\u8bd5\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u521a\u521a\u628a\u8f6f\u8def\u7531\u4ece Ubuntu 20.04 \u5347\u7ea7\u5230 21.10\uff0c\u5347\u7ea7\u7684\u65f6\u5019\u544a\u8bc9\u6211\u4f1a\u628a FirewallBacke [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false}}},"categories":[1],"tags":[],"class_list":["post-1097","post","type-post","status-publish","format-standard","hentry","category-1"],"jetpack_publicize_connections":[],"aioseo_notices":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p6XQWE-hH","jetpack-related-posts":[],"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/comzyh.com\/blog\/wp-json\/wp\/v2\/posts\/1097","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/comzyh.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/comzyh.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/comzyh.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/comzyh.com\/blog\/wp-json\/wp\/v2\/comments?post=1097"}],"version-history":[{"count":3,"href":"https:\/\/comzyh.com\/blog\/wp-json\/wp\/v2\/posts\/1097\/revisions"}],"predecessor-version":[{"id":1100,"href":"https:\/\/comzyh.com\/blog\/wp-json\/wp\/v2\/posts\/1097\/revisions\/1100"}],"wp:attachment":[{"href":"https:\/\/comzyh.com\/blog\/wp-json\/wp\/v2\/media?parent=1097"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/comzyh.com\/blog\/wp-json\/wp\/v2\/categories?post=1097"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/comzyh.com\/blog\/wp-json\/wp\/v2\/tags?post=1097"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}