Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement nftables rules expressions #592

Merged
merged 31 commits into from
Mar 5, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
6b933e4
nftables: Adding meta keys in NLA nft_meta
inemajo Feb 8, 2019
1da292c
nftables: Adding verdict_code from verdict NLA
inemajo Feb 8, 2019
d0e60f9
nftables: Adding nat_flag NLA
inemajo Feb 8, 2019
8810f08
nftables: fix nft_set_msg and add set_flags NLA
inemajo Feb 8, 2019
add929e
nftables: adding flags for nft_lookup NLA
inemajo Feb 8, 2019
a3e99ba
nftables: fix implementation of nft_exthdr NLA
inemajo Feb 8, 2019
7ecabaf
nftables: adding dynset expression. NOTE: NFTA_DYNSET_EXPR is a rule_…
inemajo Feb 11, 2019
1296883
nftables: Adding nft_match NLA
inemajo Feb 22, 2019
8eaa7f9
nftables: Adding nft_target NLA
inemajo Feb 22, 2019
46df388
nftables: Create a parser module.
inemajo Feb 25, 2019
3e169c5
nftables: Create Rule object and parser for expressions
inemajo Feb 25, 2019
c2ea524
nftables: expr parser adding a class for parsing expressions
inemajo Feb 25, 2019
06a3e79
nftables: adding nftreg for expr parser
inemajo Feb 25, 2019
0cd161b
nftables: parser: expr: Adding the replaxce_str class
inemajo Feb 25, 2019
e1b2059
nftables: parser: adding meta expression
inemajo Feb 25, 2019
7ebdfef
nftables: parser: expr: adding NFTVerdict and NFTData classes
inemajo Feb 26, 2019
9df8203
nftables: parser: adding cmp expression
inemajo Feb 25, 2019
bbc1743
nftables: parser: parser: adding __repr__
inemajo Feb 26, 2019
c7cc031
nftables: parser/expr: Adding ExprImmediate
inemajo Feb 26, 2019
4e13ddb
nftables: parser/expr: Adding ExprPayload
inemajo Feb 26, 2019
773a0b4
nftables: parser/expr: Adding ExprLookup
inemajo Feb 26, 2019
22571a3
nftables: parser/expr: Adding ExprNat
inemajo Feb 26, 2019
d66d8fa
nftables: parser/expr: Adding ExprBitwise
inemajo Feb 26, 2019
dcc8260
nftables: parser/expr: Adding ExprCounter
inemajo Feb 26, 2019
3360d1c
nftables: parser/expr: Adding ExprMatch
inemajo Feb 26, 2019
187a176
nftables: parser/expr: Adding ExprTarget
inemajo Feb 26, 2019
7683d3e
nftables: parser: passing from ascci to raw
inemajo Feb 25, 2019
a88bee2
nftables: flake8
inemajo Mar 4, 2019
41d88d4
nftables: rule: Adding nfproto family
inemajo Mar 4, 2019
947a179
nftables: Adding tests
inemajo Mar 5, 2019
fa30212
test_nftables: Remove debug comment, raise SkipTest than Exception
inemajo Mar 5, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions tests/general/test_nftables.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import json
import errno
from subprocess import check_output, CalledProcessError

from pyroute2 import netns
from pyroute2.nftables.main import NFTables
from pyroute2.nftables.rule import NFTRule

from utils import require_user

#NFT_BIN_PATH = "/root/nft/nftables/src/nft"
NFT_BIN_PATH = "nft"
NS_NAME = 'pyroute2_test_nftable'


class NFTables_test(object):
Copy link
Owner

@svinota svinota Mar 5, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm going to disable this test for a while since the nft command does not support export json anymore, and -j list ruleset provides a completely different output.

We have to rework the test somehow.


def setup(self):
require_user('root')
try:
netns.create(NS_NAME)
except OSError as e:
if e.errno == errno.EEXIST:
netns.remove(NS_NAME)
netns.create(NS_NAME)
else:
raise
try:
check_output([NFT_BIN_PATH, "-f", "nftables.ruleset"])
except OSError as e:
if e.errno == errno.ENOENT:
raise Exception("You must install nftables for the test")
else:
raise

def teardown(self):
netns.remove(NS_NAME)

def test_export_json(self):
try:
nft_res = json.loads(
check_output([NFT_BIN_PATH, "export", "json"]))
except CalledProcessError:
raise Exception(
"Please install nft compiled with --with-json option")
nft_res = [e['rule'] for e in nft_res['nftables'] if 'rule' in e]
my_res = []
for r in NFTables(nfgen_family=0).get_rules():
my_res.append(NFTRule.from_netlink(r).to_dict())
assert my_res == nft_res
15 changes: 15 additions & 0 deletions tests/nftables.ruleset
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@

flush ruleset

table inet filter {
chain coucou {
accept
}
chain input {
type filter hook input priority 0; policy accept;
meta l4proto tcp tcp dport 1234 accept
iifname lo0 oifname wan0 ip saddr 1.2.3.4 ip daddr 1.2.3.4 ip version 3 jump coucou
iifname lo0 oifname wan0 (tcp dport | 1234) & 34 == 56 jump coucou
iifname lo0 oifname wan0 ether saddr 00:11:22:33:44:55 jump coucou
}
}