From a47cd5c817dbb135a09310ec8fce6ddb62fbf5d4 Mon Sep 17 00:00:00 2001 From: Vadim Kurland Date: Wed, 21 Oct 2009 00:14:22 +0000 Subject: [PATCH] Support for branching NAT rules for PF. Refs #84 --- build_num | 2 +- doc/ChangeLog | 3 + src/gui/ActionsDialog.cpp | 5 + src/gui/DialogFactory.cpp | 2 +- src/pflib/NATCompiler_pf.cpp | 49 +- src/pflib/NATCompiler_pf_writers.cpp | 32 ++ src/pflib/PolicyCompiler_pf_writers.cpp | 8 +- test/pf/objects-for-regression-tests.fwb | 570 +++++++++++++++++------ 8 files changed, 511 insertions(+), 160 deletions(-) diff --git a/build_num b/build_num index a6f8b3076..ac182eafd 100644 --- a/build_num +++ b/build_num @@ -1 +1 @@ -#define BUILD_NUM 1622 +#define BUILD_NUM 1623 diff --git a/doc/ChangeLog b/doc/ChangeLog index 4f7815b72..a54a77fa1 100644 --- a/doc/ChangeLog +++ b/doc/ChangeLog @@ -1,5 +1,8 @@ 2009-10-20 vadim + * NATCompiler_pf_writers.cpp (PrintRule::processNext): Added + support for branching NAT rules for PF. + * platforms.cpp (getActionNameForPlatform): Human-readable names for Policy and NAT rule actions come from the platform .xml resource file (attribute "description"). diff --git a/src/gui/ActionsDialog.cpp b/src/gui/ActionsDialog.cpp index c14cc7270..8ce935465 100644 --- a/src/gui/ActionsDialog.cpp +++ b/src/gui/ActionsDialog.cpp @@ -275,6 +275,11 @@ void ActionsDialog::setRule(Rule *r) editor = DialogFactory::getActionDialogPageName(f, r); + if (fwbdebug) + qDebug() << "ActionsDialog::setRule" + << "Action: " << getRuleAction(rule) + << "editor: " << editor.c_str(); + branchNameInput = NULL; if (ropt->getInt("ipfw_classify_method") == DUMMYNETPIPE) diff --git a/src/gui/DialogFactory.cpp b/src/gui/DialogFactory.cpp index 10826c12f..1c7ed5082 100644 --- a/src/gui/DialogFactory.cpp +++ b/src/gui/DialogFactory.cpp @@ -359,7 +359,7 @@ string DialogFactory::getActionDialogPageName(Firewall *fw, Rule *rule) try { editor_page = Resources::getTargetCapabilityStr( - platform, "actions/" + act + "/editor_page"); + platform, "actions/" + act + "/dialog_page"); } catch (FWException &ex) { } return editor_page; } diff --git a/src/pflib/NATCompiler_pf.cpp b/src/pflib/NATCompiler_pf.cpp index 603486851..5734cf0ed 100644 --- a/src/pflib/NATCompiler_pf.cpp +++ b/src/pflib/NATCompiler_pf.cpp @@ -145,13 +145,39 @@ bool NATCompiler_pf::NATRuleType::processNext() if (rule->getRuleType()!=NATRule::Unknown) return true; + RuleElementTSrc *tsrcre = rule->getTSrc(); RuleElementTDst *tdstre = rule->getTDst(); + RuleElementTSrv *tsrvre = rule->getTSrv(); Service *osrv=compiler->getFirstOSrv(rule); Address *tsrc = compiler->getFirstTSrc(rule); Address *tdst = compiler->getFirstTDst(rule); - Service *tsrv=compiler->getFirstTSrv(rule); + Service *tsrv=compiler->getFirstTSrv(rule); + + if (rule->getAction() == NATRule::Branch) + { + rule->setRuleType(NATRule::NATBranch); + if (!tsrcre->isAny() || !tdstre->isAny() || !tsrvre->isAny()) + { + tsrcre->clearChildren(); + tsrcre->setAnyElement(); + + tdstre->clearChildren(); + tdstre->setAnyElement(); + + tsrvre->clearChildren(); + tsrvre->setAnyElement(); + + compiler->warning( + rule, + "Translated Src, Dst and Srv are ignored in the NAT " + "rule with action 'Branch'"); + } + return true; + } + + if (tsrc->isAny() && tdst->isAny() && tsrv->isAny()) { @@ -476,6 +502,20 @@ bool NATCompiler_pf::VerifyRules::processNext() "Original and translated destination should both be networks of the same size."); } + if (rule->getRuleType()==NATRule::NATBranch ) + { + RuleSet *branch = rule->getBranch(); + if (branch == NULL) + compiler->abort( + rule, + "Action 'Branch' needs NAT rule set to point to"); + if (!NAT::isA(branch)) + compiler->abort( + rule, + "Action 'Branch' must point to a NAT rule set " + "(points to " + branch->getTypeName() + ")"); + } + return true; } @@ -484,9 +524,10 @@ bool NATCompiler_pf::splitOnOSrv::processNext() NATRule *rule=getNext(); if (rule==NULL) return false; RuleElementOSrv *osrv=rule->getOSrv(); assert(osrv); - if (osrv->size()!=1) { - - for(list::iterator i=osrv->begin(); i!=osrv->end(); ++i) { + if (osrv->size()!=1) + { + for(list::iterator i=osrv->begin(); i!=osrv->end(); ++i) + { FWObject *o= *i; // if (FWReference::cast(o)!=NULL) o=FWReference::cast(o)->getPointer(); if (FWReference::cast(o)!=NULL) o=FWReference::cast(o)->getPointer(); diff --git a/src/pflib/NATCompiler_pf_writers.cpp b/src/pflib/NATCompiler_pf_writers.cpp index 38828578a..3183ef1a5 100644 --- a/src/pflib/NATCompiler_pf_writers.cpp +++ b/src/pflib/NATCompiler_pf_writers.cpp @@ -230,6 +230,38 @@ bool NATCompiler_pf::PrintRule::processNext() _printNATRuleOptions(rule); compiler->output << endl; break; + + case NATRule::NATBranch: + { + RuleSet *ruleset = rule->getBranch(); + string ruleset_name; + if (ruleset!=NULL) + { + ruleset_name = ruleset->getName(); + } else + { + compiler->abort( + rule, + "Branching rule refers ruleset that does not exist"); + // in test mode compiler->abort() does not really abort the program + ruleset_name = "UNKNOWN"; + } + compiler->output << "anchor \"" << ruleset_name << "\" "; + + if (iface_name!="") compiler->output << "on " << iface_name << " "; + if (!osrv->isAny() || !osrcrel->isAny() || !odstrel->isAny()) + { + _printProtocol(osrv); + compiler->output << "from "; + _printREAddr( osrcrel ); + compiler->output << "to "; + _printREAddr( odstrel ); + _printPort(osrv, true); + } + compiler->output << endl; + } + break; + default: break; } diff --git a/src/pflib/PolicyCompiler_pf_writers.cpp b/src/pflib/PolicyCompiler_pf_writers.cpp index b8c850b7c..8a74e026c 100644 --- a/src/pflib/PolicyCompiler_pf_writers.cpp +++ b/src/pflib/PolicyCompiler_pf_writers.cpp @@ -123,22 +123,16 @@ void PolicyCompiler_pf::PrintRule::_printAction(PolicyRule *rule) RuleSet *ruleset = rule->getBranch(); if (ruleset==NULL) compiler->abort( - rule, "Branching rule refers ruleset that does not exist"); string ruleset_name = ruleset->getName(); - if (ruleset_name.find("/*")!=string::npos) - compiler->output << "anchor \"" << ruleset_name << "\" "; - else - compiler->output << "anchor " << ruleset_name << " "; + compiler->output << "anchor \"" << ruleset_name << "\" "; break; } default: compiler->abort( - rule, string("Unknown action ") + rule->getActionAsString()); -// compiler->output << rule->getActionAsString() << " "; } } diff --git a/test/pf/objects-for-regression-tests.fwb b/test/pf/objects-for-regression-tests.fwb index 9f0fb837c..a5c24c588 100644 --- a/test/pf/objects-for-regression-tests.fwb +++ b/test/pf/objects-for-regression-tests.fwb @@ -1,6 +1,6 @@ - + @@ -417,7 +417,7 @@ - + @@ -438,7 +438,7 @@ - + @@ -459,7 +459,7 @@ - + @@ -480,7 +480,7 @@ - + @@ -501,7 +501,7 @@ - + @@ -522,7 +522,7 @@ - + @@ -543,7 +543,7 @@ - + @@ -564,7 +564,7 @@ - + @@ -585,7 +585,7 @@ - + @@ -818,7 +818,7 @@ - + @@ -1096,6 +1096,11 @@ + + + + + @@ -1704,7 +1709,7 @@ - + @@ -1725,7 +1730,7 @@ - + @@ -1746,7 +1751,7 @@ - + @@ -1767,7 +1772,7 @@ - + @@ -2490,7 +2495,7 @@ - + @@ -2511,7 +2516,7 @@ - + @@ -2532,7 +2537,7 @@ - + @@ -2553,7 +2558,7 @@ - + @@ -2574,7 +2579,7 @@ - + @@ -2596,7 +2601,7 @@ - + @@ -2618,7 +2623,7 @@ - + @@ -2639,7 +2644,7 @@ - + @@ -2660,7 +2665,7 @@ - + @@ -2681,7 +2686,7 @@ - + @@ -2702,7 +2707,7 @@ - + @@ -2723,7 +2728,7 @@ - + @@ -2744,7 +2749,7 @@ - + @@ -2765,7 +2770,7 @@ - + @@ -2786,7 +2791,7 @@ - + @@ -3382,7 +3387,7 @@ - + @@ -3403,7 +3408,7 @@ - + @@ -3425,7 +3430,7 @@ - + @@ -3589,7 +3594,7 @@ - + @@ -3613,7 +3618,7 @@ - + @@ -3638,7 +3643,7 @@ - + @@ -3662,7 +3667,7 @@ - + @@ -3686,7 +3691,7 @@ - + @@ -3711,7 +3716,7 @@ - + @@ -3735,7 +3740,7 @@ - + @@ -3759,7 +3764,7 @@ - + @@ -3784,7 +3789,7 @@ - + @@ -3808,7 +3813,7 @@ - + @@ -3829,7 +3834,7 @@ - + @@ -3851,7 +3856,7 @@ - + @@ -3872,7 +3877,7 @@ - + @@ -3895,7 +3900,7 @@ - + @@ -3916,7 +3921,7 @@ - + @@ -3937,7 +3942,7 @@ - + @@ -3958,7 +3963,7 @@ - + @@ -3979,7 +3984,7 @@ - + @@ -4000,7 +4005,7 @@ - + @@ -4021,7 +4026,7 @@ - + @@ -4042,7 +4047,7 @@ - + @@ -4063,7 +4068,7 @@ - + @@ -4084,7 +4089,7 @@ - + @@ -4105,7 +4110,7 @@ - + @@ -4126,7 +4131,7 @@ - + @@ -4147,7 +4152,7 @@ - + @@ -4168,7 +4173,7 @@ - + @@ -4189,7 +4194,7 @@ - + @@ -4210,7 +4215,7 @@ - + @@ -4231,7 +4236,7 @@ - + @@ -4252,7 +4257,7 @@ - + @@ -4738,7 +4743,7 @@ - + @@ -4761,7 +4766,7 @@ - + @@ -4784,7 +4789,7 @@ - + @@ -4805,7 +4810,7 @@ - + @@ -4826,7 +4831,7 @@ - + @@ -4847,7 +4852,7 @@ - + @@ -5271,7 +5276,7 @@ - + @@ -5720,7 +5725,7 @@ - + @@ -5741,7 +5746,7 @@ - + @@ -5762,7 +5767,7 @@ - + @@ -5783,7 +5788,7 @@ - + @@ -5804,7 +5809,7 @@ - + @@ -5825,7 +5830,7 @@ - + @@ -5846,7 +5851,7 @@ - + @@ -5867,7 +5872,7 @@ - + @@ -5888,7 +5893,7 @@ - + @@ -6087,7 +6092,7 @@ - + @@ -6108,7 +6113,7 @@ - + @@ -6318,7 +6323,7 @@ - + @@ -6339,7 +6344,7 @@ - + @@ -6360,7 +6365,7 @@ - + @@ -6382,7 +6387,7 @@ - + @@ -6736,7 +6741,7 @@ - + @@ -6757,7 +6762,7 @@ - + @@ -6778,7 +6783,7 @@ - + @@ -6799,7 +6804,7 @@ - + @@ -6820,7 +6825,7 @@ - + @@ -6841,7 +6846,7 @@ - + @@ -6862,7 +6867,7 @@ - + @@ -7242,7 +7247,7 @@ - + @@ -7263,7 +7268,7 @@ - + @@ -7662,7 +7667,7 @@ - + @@ -7690,7 +7695,7 @@ - + @@ -7723,7 +7728,7 @@ - + @@ -7751,7 +7756,7 @@ - + @@ -7779,7 +7784,7 @@ - + @@ -7985,7 +7990,7 @@ - + @@ -8006,7 +8011,7 @@ - + @@ -8510,7 +8515,7 @@ - + @@ -8531,7 +8536,7 @@ - + @@ -8974,7 +8979,7 @@ - + @@ -8995,7 +9000,7 @@ - + @@ -9270,7 +9275,7 @@ - + @@ -9291,7 +9296,7 @@ - + @@ -9570,7 +9575,7 @@ - + @@ -9591,7 +9596,7 @@ - + @@ -9866,7 +9871,7 @@ - + @@ -9887,7 +9892,7 @@ - + @@ -10162,7 +10167,7 @@ - + @@ -10183,7 +10188,7 @@ - + @@ -10493,7 +10498,7 @@ - + @@ -10514,7 +10519,7 @@ - + @@ -10789,7 +10794,7 @@ - + @@ -10810,7 +10815,7 @@ - + @@ -13835,7 +13840,7 @@ - + @@ -14114,7 +14119,7 @@ - + @@ -14135,7 +14140,7 @@ - + @@ -14156,7 +14161,7 @@ - + @@ -14177,7 +14182,7 @@ - + @@ -14930,7 +14935,7 @@ - + @@ -14951,7 +14956,7 @@ - + @@ -14972,7 +14977,7 @@ - + @@ -14993,7 +14998,7 @@ - + @@ -15014,7 +15019,7 @@ - + @@ -15035,7 +15040,7 @@ - + @@ -15056,7 +15061,7 @@ - + @@ -15077,7 +15082,7 @@ - + @@ -15098,7 +15103,7 @@ - + @@ -15119,7 +15124,7 @@ - + @@ -15140,7 +15145,7 @@ - + @@ -15161,7 +15166,7 @@ - + @@ -15182,7 +15187,7 @@ - + @@ -15203,7 +15208,7 @@ - + @@ -15224,7 +15229,7 @@ - + @@ -15245,7 +15250,7 @@ - + @@ -15803,7 +15808,7 @@ - + @@ -16123,6 +16128,277 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -16179,7 +16455,7 @@ - + @@ -16200,7 +16476,7 @@ - + @@ -16591,7 +16867,7 @@ - + @@ -17756,7 +18032,7 @@ - + @@ -17777,7 +18053,7 @@ - + @@ -17798,7 +18074,7 @@ - + @@ -17819,7 +18095,7 @@ - +