forked from containernetworking/plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Resolves: containernetworking#461
- Loading branch information
Showing
435 changed files
with
23,047 additions
and
9,347 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,203 @@ | ||
// Copyright 2018 CNI authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package main | ||
|
||
import ( | ||
"fmt" | ||
"github.com/containernetworking/cni/pkg/types/current" | ||
"github.com/davecgh/go-spew/spew" | ||
"github.com/google/nftables" | ||
"github.com/sirupsen/logrus" | ||
"net" | ||
) | ||
|
||
// Only used for testcases to override the D-Bus connection | ||
var testNftConn *nftables.Conn | ||
|
||
type nftBackend struct { | ||
conn *nftables.Conn | ||
tables []*nftables.Table | ||
chains []*nftables.Chain | ||
targetTable *nftables.Table | ||
targetChain *nftables.Chain | ||
targetHandle uint64 | ||
targetInterface string | ||
targetNetwork *net.IPNet | ||
} | ||
|
||
// nftBackend implements the FirewallBackend interface | ||
var _ FirewallBackend = &nftBackend{} | ||
|
||
func getNftConn() (*nftables.Conn, error) { | ||
if testNftConn != nil { | ||
return testNftConn, nil | ||
} | ||
return &nftables.Conn{}, nil | ||
} | ||
|
||
func newNftablesBackend(conf *FirewallNetConf) (FirewallBackend, error) { | ||
conn, err := getNftConn() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
backend := &nftBackend{ | ||
conn: conn, | ||
} | ||
return backend, nil | ||
} | ||
|
||
func (nb *nftBackend) isFilterTableExists() error { | ||
isTableFound := false | ||
tables, err := nb.conn.ListTables() | ||
if err != nil { | ||
return fmt.Errorf("isFilterTableExists(): %s", err) | ||
} | ||
for _, table := range tables { | ||
if table.Name == "filter" && table.Family == 2 { | ||
nb.targetTable = table | ||
isTableFound = true | ||
break | ||
} | ||
} | ||
if !isTableFound { | ||
return fmt.Errorf("isFilterTableExists(): the IPv4 filter table does not exist") | ||
} | ||
nb.tables = tables | ||
return nil | ||
} | ||
|
||
func (nb *nftBackend) isForwardChainExists() error { | ||
isChainFound := false | ||
chains, err := nb.conn.ListChains() | ||
if err != nil { | ||
return fmt.Errorf("isForwardChainExists(): %s", err) | ||
} | ||
for _, chain := range chains { | ||
if chain.Name != "FORWARD" { | ||
continue | ||
} | ||
if chain.Type != "filter" { | ||
continue | ||
} | ||
if chain.Table.Name != "filter" { | ||
continue | ||
} | ||
if chain.Table.Family != 2 { | ||
continue | ||
} | ||
nb.targetChain = chain | ||
isChainFound = true | ||
break | ||
} | ||
if !isChainFound { | ||
return fmt.Errorf("isForwardChainExists(): the FORWARD chain in IPv4 filter table does not exist") | ||
} | ||
nb.chains = chains | ||
return nil | ||
} | ||
|
||
func (nb *nftBackend) isValidInput(result *current.Result) error { | ||
if len(result.Interfaces) == 0 { | ||
return fmt.Errorf("the data passed to firewall plugin did not contain network interfaces") | ||
} | ||
|
||
if result.Interfaces[0].Name == "" { | ||
return fmt.Errorf("the data passed to firewall plugin has no bridge name, e.g. cnibr0") | ||
} | ||
|
||
nb.targetInterface = result.Interfaces[0].Name | ||
|
||
logrus.Errorf("nftBackend.Add() target interface: %s", spew.Sdump(nb.targetInterface)) | ||
|
||
if len(result.IPs) == 0 { | ||
return fmt.Errorf("the data passed to firewall plugin has no IP addresses") | ||
} | ||
|
||
if len(result.IPs) != 1 { | ||
return fmt.Errorf("the data passed to firewall plugin has more than one IP address") | ||
} | ||
|
||
addr := result.IPs[0].Address | ||
|
||
if addr.String() == "" { | ||
return fmt.Errorf("the data passed to firewall plugin has empty IP address") | ||
} | ||
|
||
if addr.IP.To4() == nil { | ||
return fmt.Errorf("the data passed to firewall plugin has non-IPv4 address") | ||
} | ||
|
||
_, netAddr, err := net.ParseCIDR(addr.String()) | ||
if err != nil { | ||
return fmt.Errorf("the data passed to firewall plugin has invalid IPv4 address") | ||
} | ||
|
||
nb.targetNetwork = netAddr | ||
|
||
return nil | ||
} | ||
|
||
func (nb *nftBackend) Add(conf *FirewallNetConf, result *current.Result) error { | ||
if err := nb.isValidInput(result); err != nil { | ||
return fmt.Errorf("nftBackend.Add() %s", err) | ||
} | ||
|
||
if err := nb.isFilterTableExists(); err != nil { | ||
return fmt.Errorf("nftBackend.Add() %s", err) | ||
} | ||
|
||
if err := nb.isForwardChainExists(); err != nil { | ||
return fmt.Errorf("nftBackend.Add() %s", err) | ||
} | ||
|
||
rules, err := nb.conn.GetRule(nb.targetTable, nb.targetChain) | ||
if err != nil { | ||
return fmt.Errorf("nftBackend.Add() GetRule failed: %s", err) | ||
} | ||
|
||
return fmt.Errorf("nftBackend.Add is not supported") | ||
|
||
if len(rules) > 0 { | ||
nb.targetHandle = rules[0].Handle | ||
for _, rule := range rules { | ||
logrus.Errorf("nftBackend.Add() rule: %s", spew.Sdump(rule.Handle)) | ||
//logrus.Errorf("nftBackend.Add() rule: %s", spew.Sdump(rule)) | ||
} | ||
} | ||
|
||
if nb.targetHandle == 0 { | ||
// TODO: add rules as opposed to insert | ||
} else { | ||
// TODO: insert the rules | ||
} | ||
|
||
return fmt.Errorf("nftBackend.Add is not supported") | ||
//return nil | ||
} | ||
|
||
func (nb *nftBackend) Del(conf *FirewallNetConf, result *current.Result) error { | ||
logrus.Errorf("nftBackend.Del() conf: %s", spew.Sdump(conf)) | ||
logrus.Errorf("nftBackend.Del() result: %s", spew.Sdump(result)) | ||
// TODO | ||
return nil | ||
} | ||
|
||
func (nb *nftBackend) Check(conf *FirewallNetConf, result *current.Result) error { | ||
// TODO | ||
logrus.Errorf("nftBackend.Check() conf: %s", spew.Sdump(conf)) | ||
logrus.Errorf("nftBackend.Check() result: %s", spew.Sdump(result)) | ||
return nil | ||
} |
Oops, something went wrong.