-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adding bluefield plugin, to always provide the same IPv6 address
- Loading branch information
Showing
3 changed files
with
110 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
package bluefield | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
"net" | ||
|
||
"github.com/coredhcp/coredhcp/plugins" | ||
"github.com/coredhcp/coredhcp/handler" | ||
"github.com/coredhcp/coredhcp/logger" | ||
"github.com/insomniacslk/dhcp/dhcpv6" | ||
"github.com/insomniacslk/dhcp/iana" | ||
|
||
) | ||
|
||
var log = logger.GetLogger("plugins/bluefield") | ||
|
||
var Plugin = plugins.Plugin { | ||
Name: "bluefield", | ||
Setup6: setupPlugin, | ||
} | ||
var ipaddr net.IP | ||
|
||
|
||
// setupPlugin initializes the plugin with given arguments. | ||
func setupPlugin(args ...string) (handler.Handler6, error) { | ||
if len(args) < 1 { | ||
return nil, fmt.Errorf("plugin bluefield requires at least one argument (static IPv6 address)") | ||
} | ||
ipaddr = net.ParseIP(args[0]) | ||
if ipaddr == nil { | ||
return nil, fmt.Errorf("invalid IPv6 address: %s", args[0]) | ||
} | ||
log.Infof("Parsed IP %s", ipaddr) | ||
return handleDHCPv6, nil | ||
} | ||
|
||
func handleDHCPv6(req, resp dhcpv6.DHCPv6) (dhcpv6.DHCPv6, bool) { | ||
m, err := req.GetInnerMessage() | ||
if err != nil { | ||
return nil, true | ||
} | ||
|
||
hwaddr, err := net.ParseMAC("00:11:22:33:44:55") | ||
if err != nil { | ||
return nil, true | ||
} | ||
|
||
v6ServerID := &dhcpv6.DUIDLL { | ||
HWType: iana.HWTypeEthernet, | ||
LinkLayerAddr: hwaddr, | ||
} | ||
|
||
|
||
switch m.Type() { | ||
case dhcpv6.MessageTypeSolicit: | ||
resp, err := dhcpv6.NewAdvertiseFromSolicit(m) | ||
if err != nil { | ||
log.Errorf("Failed to create DHCPv6 advertise: %v", err) | ||
return nil, true | ||
} | ||
|
||
log.Infof("IP: %s", ipaddr) | ||
|
||
resp.AddOption(&dhcpv6.OptIANA { | ||
IaId: m.Options.OneIANA().IaId, | ||
T1: 1 * time.Hour, | ||
T2: 2 * time.Hour, | ||
Options: dhcpv6.IdentityOptions{Options: []dhcpv6.Option{ | ||
&dhcpv6.OptIAAddress{ | ||
IPv6Addr: ipaddr, | ||
PreferredLifetime: 24 * time.Hour, | ||
ValidLifetime: 48 * time.Hour, | ||
}, | ||
}}, | ||
}) | ||
|
||
dhcpv6.WithServerID(v6ServerID)(resp) | ||
return resp, false | ||
|
||
case dhcpv6.MessageTypeRequest: | ||
resp, err = dhcpv6.NewReplyFromMessage(m) | ||
if err != nil { | ||
log.Errorf("Failed to create DHCPv6 reply: %v", err) | ||
return nil, false | ||
} | ||
|
||
resp.AddOption(&dhcpv6.OptIANA { | ||
IaId: m.Options.OneIANA().IaId, | ||
T1: 1 * time.Hour, | ||
T2: 2 * time.Hour, | ||
Options: dhcpv6.IdentityOptions{Options: []dhcpv6.Option{ | ||
&dhcpv6.OptIAAddress{ | ||
IPv6Addr: ipaddr, | ||
PreferredLifetime: 24 * time.Hour, | ||
ValidLifetime: 48 * time.Hour, | ||
}, | ||
}}, | ||
}) | ||
|
||
dhcpv6.WithServerID(v6ServerID)(resp) | ||
return resp, true | ||
} | ||
return nil, false | ||
} |