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 DHCPv6 Server Identifier Generation #46

Merged
merged 20 commits into from
Nov 27, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
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
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 13 additions & 7 deletions example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -192,12 +192,17 @@ networks:
# responding to INFOREQ.
v6:
# optional, interfaces to bind
interfaces:
- enp6s0
# TODO: SERVER ID
# server_id:
# type: DUID-LLT (default) | DUID-LL | DUID-EN | ...
# value: ""
# interfaces:
# - enp6s0
# Optional, if server_id is not specified, we will generate an server identifer or use previous generated server identifier(if exists). Addtionally, if all settings are the same as previous settings, we will also use previous generated server identifier.
server_id:
duid_type: LLT # LLT (default) | LL | EN | UUID
identifier: fe80::c981:b769:461a:bfb4 # Optional, set blank or remove to auto-generate link-layer address. For LLT and LL, it must be a valid link-layer address.
time: 1111112 # Optional, set blank or remove to auto-generate time. For LLT, it must be a valid u32 timestamp
hardware_type: 1 # Optional, set blank or remove to auto-generate hardware type. For LL, it must be a valid u16 hardware type
#enterprise_id: 1 # Optional, set blank or remove to auto-generate enterprise id. For EN, it must be a valid u32 enterprise id
persist: true # Optional, default value is true. set false to generate a new DUID every time the server is restarted. Persist feature is now unimplemented.
chenwanqq marked this conversation as resolved.
Show resolved Hide resolved
server_id_path: ./server_id.json # optional. default is /var/lib/dora/server_id
chenwanqq marked this conversation as resolved.
Show resolved Hide resolved
# global options (optional)
options:
values:
Expand All @@ -222,7 +227,8 @@ v6:
2001:db8:1::/64: # https://en.wikipedia.org/wiki/IPv6_address#Documentation
# optional - what interfaces we will apply to this network
interfaces:
- enp6s0
# - enp6s0
- ens37
# no explicit ranges (yet)
config:
lease_time:
Expand Down
1 change: 1 addition & 0 deletions libs/config/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ trust-dns-proto = { workspace = true }
base64 = "0.21.0"
hex = "0.4"
phf = { version = "0.11", features = ["macros"] }
rand = "0.8"

dora-core = { path = "../../dora-core" }
client-classification = { path = "../client-classification" }
Expand Down
53 changes: 53 additions & 0 deletions libs/config/sample/config_v6.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
v6:
# optional, interfaces to bind
# interfaces:
# - enp6s0
# Optional, if server_id is not specified, we will generate an server identifer or use previous generated server identifier(if exists). Addtionally, if all settings are the same as previous settings, we will also use previous generated server identifier.
server_id:
duid_type: LLT # LLT (default) | LL | EN | UUID
identifier: fe80::c981:b769:461a:bfb4 # Optional, set blank or remove to auto-generate link-layer address. For LLT and LL, it must be a valid link-layer address.
time: 1111112 # Optional, set blank or remove to auto-generate time. For LLT, it must be a valid u32 timestamp
hardware_type: 1 # Optional, set blank or remove to auto-generate hardware type. For LL, it must be a valid u16 hardware type
persist: true # Optional, default value is true. set false to generate a new DUID every time the server is restarted. Persist feature is now unimplemented.
server_id_path: /var/lib/dora/server_id # optional. default is /var/lib/dora/server_id
# global options (optional)
options:
values:
# dns
23:
type: ip_list
value:
- 2001:db8::1
- 2001:db8::2
# optional
networks:
# subnet selection:
# we will attempt to match first the IP of the `interfaces` field below to determine
# which subnet to apply, if none is specified we will use the link-local, then the global IP
# of the interface we received the message on.
# (this method should be double checked, I'm not sure it's correct)
#
# https://kea.readthedocs.io/en/kea-1.6.0/arm/dhcp6-srv.html#dhcp6-config-subnets
# https://datatracker.ietf.org/doc/html/rfc8415#section-13.1
# https://techhub.hpe.com/eginfolib/networking/docs/switches/5130ei/5200-3942_l3-ip-svcs_cg/content/483572577.htm
#
2001:db8:1::/64: # https://en.wikipedia.org/wiki/IPv6_address#Documentation
# optional - what interfaces we will apply to this network
#interfaces:
# - enp6s0
# no explicit ranges (yet)
config:
lease_time:
default: 3600
preferred_time:
default: 3600
# same with options
# inspiration: https://kea.readthedocs.io/en/kea-2.2.0/arm/dhcp6-srv.html?highlight=router%20advertisement#dhcp6-std-options-list
options:
values:
# dns
23:
type: ip_list
value:
- 2001:db8::1
- 2001:db8::2
52 changes: 52 additions & 0 deletions libs/config/sample/config_v6_EN.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
v6:
# optional, interfaces to bind
# interfaces:
# - enp6s0
# Optional, if server_id is not specified, we will generate an server identifer or use previous generated server identifier(if exists). Addtionally, if all settings are the same as previous settings, we will also use previous generated server identifier.
server_id:
duid_type: EN # LLT (default) | LL | EN | UUID
identifier: 1122FFFE3842 # Optional, set blank or remove to auto-generate link-layer address. For LLT and LL, it must be a valid link-layer address.
enterprise_id: 23
persist: true # Optional, default value is true. set false to generate a new DUID every time the server is restarted. Persist feature is now unimplemented.
server_id_path: /var/lib/dora/server_id # optional. default is /var/lib/dora/server_id
# global options (optional)
options:
values:
# dns
23:
type: ip_list
value:
- 2001:db8::1
- 2001:db8::2
# optional
networks:
# subnet selection:
# we will attempt to match first the IP of the `interfaces` field below to determine
# which subnet to apply, if none is specified we will use the link-local, then the global IP
# of the interface we received the message on.
# (this method should be double checked, I'm not sure it's correct)
#
# https://kea.readthedocs.io/en/kea-1.6.0/arm/dhcp6-srv.html#dhcp6-config-subnets
# https://datatracker.ietf.org/doc/html/rfc8415#section-13.1
# https://techhub.hpe.com/eginfolib/networking/docs/switches/5130ei/5200-3942_l3-ip-svcs_cg/content/483572577.htm
#
2001:db8:1::/64: # https://en.wikipedia.org/wiki/IPv6_address#Documentation
# optional - what interfaces we will apply to this network
#interfaces:
# - enp6s0
# no explicit ranges (yet)
config:
lease_time:
default: 3600
preferred_time:
default: 3600
# same with options
# inspiration: https://kea.readthedocs.io/en/kea-2.2.0/arm/dhcp6-srv.html?highlight=router%20advertisement#dhcp6-std-options-list
options:
values:
# dns
23:
type: ip_list
value:
- 2001:db8::1
- 2001:db8::2
52 changes: 52 additions & 0 deletions libs/config/sample/config_v6_LL.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
v6:
# optional, interfaces to bind
# interfaces:
# - enp6s0
# Optional, if server_id is not specified, we will generate an server identifer or use previous generated server identifier(if exists). Addtionally, if all settings are the same as previous settings, we will also use previous generated server identifier.
server_id:
duid_type: LL # LLT (default) | LL | EN | UUID
identifier: fe80::c981:b769:461a:bfb4 # Optional, set blank or remove to auto-generate link-layer address. For LLT and LL, it must be a valid link-layer address.
hardware_type: 2 # Optional, set blank or remove to auto-generate hardware type. For LL, it must be a valid u16 hardware type
persist: true # Optional, default value is true. set false to generate a new DUID every time the server is restarted. Persist feature is now unimplemented.
server_id_path: /var/lib/dora/server_id # optional. default is /var/lib/dora/server_id
# global options (optional)
options:
values:
# dns
23:
type: ip_list
value:
- 2001:db8::1
- 2001:db8::2
# optional
networks:
# subnet selection:
# we will attempt to match first the IP of the `interfaces` field below to determine
# which subnet to apply, if none is specified we will use the link-local, then the global IP
# of the interface we received the message on.
# (this method should be double checked, I'm not sure it's correct)
#
# https://kea.readthedocs.io/en/kea-1.6.0/arm/dhcp6-srv.html#dhcp6-config-subnets
# https://datatracker.ietf.org/doc/html/rfc8415#section-13.1
# https://techhub.hpe.com/eginfolib/networking/docs/switches/5130ei/5200-3942_l3-ip-svcs_cg/content/483572577.htm
#
2001:db8:1::/64: # https://en.wikipedia.org/wiki/IPv6_address#Documentation
# optional - what interfaces we will apply to this network
#interfaces:
# - enp6s0
# no explicit ranges (yet)
config:
lease_time:
default: 3600
preferred_time:
default: 3600
# same with options
# inspiration: https://kea.readthedocs.io/en/kea-2.2.0/arm/dhcp6-srv.html?highlight=router%20advertisement#dhcp6-std-options-list
chenwanqq marked this conversation as resolved.
Show resolved Hide resolved
options:
values:
# dns
23:
type: ip_list
value:
- 2001:db8::1
- 2001:db8::2
51 changes: 51 additions & 0 deletions libs/config/sample/config_v6_UUID.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
v6:
# optional, interfaces to bind
# interfaces:
# - enp6s0
# Optional, if server_id is not specified, we will generate an server identifer or use previous generated server identifier(if exists). Addtionally, if all settings are the same as previous settings, we will also use previous generated server identifier.
server_id:
duid_type: UUID # LLT (default) | LL | EN | UUID
identifier: 451c810bf191a92abf3768dd1ed61f3a # Optional, set blank or remove to auto-generate link-layer address. For LLT and LL, it must be a valid link-layer address.
persist: true # Optional, default value is true. set false to generate a new DUID every time the server is restarted. Persist feature is now unimplemented.
server_id_path: /var/lib/dora/server_id # optional. default is /var/lib/dora/server_id
# global options (optional)
options:
values:
# dns
23:
type: ip_list
value:
- 2001:db8::1
- 2001:db8::2
# optional
networks:
# subnet selection:
# we will attempt to match first the IP of the `interfaces` field below to determine
# which subnet to apply, if none is specified we will use the link-local, then the global IP
# of the interface we received the message on.
# (this method should be double checked, I'm not sure it's correct)
#
# https://kea.readthedocs.io/en/kea-1.6.0/arm/dhcp6-srv.html#dhcp6-config-subnets
# https://datatracker.ietf.org/doc/html/rfc8415#section-13.1
# https://techhub.hpe.com/eginfolib/networking/docs/switches/5130ei/5200-3942_l3-ip-svcs_cg/content/483572577.htm
#
2001:db8:1::/64: # https://en.wikipedia.org/wiki/IPv6_address#Documentation
# optional - what interfaces we will apply to this network
#interfaces:
# - enp6s0
# no explicit ranges (yet)
config:
lease_time:
default: 3600
preferred_time:
default: 3600
# same with options
# inspiration: https://kea.readthedocs.io/en/kea-2.2.0/arm/dhcp6-srv.html?highlight=router%20advertisement#dhcp6-std-options-list
options:
values:
# dns
23:
type: ip_list
value:
- 2001:db8::1
- 2001:db8::2
49 changes: 49 additions & 0 deletions libs/config/sample/config_v6_no_persist.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
v6:
# optional, interfaces to bind
# interfaces:
# - enp6s0
# Optional, if server_id is not specified, we will generate an server identifer or use previous generated server identifier(if exists). Addtionally, if all settings are the same as previous settings, we will also use previous generated server identifier.
server_id:
duid_type: LLT # LLT (default) | LL | EN | UUID
persist: false # Optional, default value is true. set false to generate a new DUID every time the server is restarted. Persist feature is now unimplemented.
# global options (optional)
options:
values:
# dns
23:
type: ip_list
value:
- 2001:db8::1
- 2001:db8::2
# optional
networks:
# subnet selection:
# we will attempt to match first the IP of the `interfaces` field below to determine
# which subnet to apply, if none is specified we will use the link-local, then the global IP
# of the interface we received the message on.
# (this method should be double checked, I'm not sure it's correct)
#
# https://kea.readthedocs.io/en/kea-1.6.0/arm/dhcp6-srv.html#dhcp6-config-subnets
# https://datatracker.ietf.org/doc/html/rfc8415#section-13.1
# https://techhub.hpe.com/eginfolib/networking/docs/switches/5130ei/5200-3942_l3-ip-svcs_cg/content/483572577.htm
#
2001:db8:1::/64: # https://en.wikipedia.org/wiki/IPv6_address#Documentation
# optional - what interfaces we will apply to this network
#interfaces:
# - enp6s0
# no explicit ranges (yet)
config:
lease_time:
default: 3600
preferred_time:
default: 3600
# same with options
# inspiration: https://kea.readthedocs.io/en/kea-2.2.0/arm/dhcp6-srv.html?highlight=router%20advertisement#dhcp6-std-options-list
options:
values:
# dns
23:
type: ip_list
value:
- 2001:db8::1
- 2001:db8::2
64 changes: 63 additions & 1 deletion libs/config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@ pub mod wire;
use std::{env, path::Path, time::Duration};

use anyhow::{bail, Context, Result};
use dora_core::dhcproto::v6::duid::Duid;
use dora_core::pnet::{
self,
datalink::NetworkInterface,
ipnetwork::{IpNetwork, Ipv4Network},
};
use rand::{self, RngCore};
use serde::{Deserialize, Serialize};
use tracing::debug;

use wire::v6::ServerDuid;
/// server config
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DhcpConfig {
Expand Down Expand Up @@ -179,3 +182,62 @@ pub fn renew(t: Duration) -> Duration {
pub fn rebind(t: Duration) -> Duration {
t * 7 / 8
}

pub fn generate_random_bytes(len: usize) -> Vec<u8> {
let mut ident = Vec::with_capacity(len);
rand::thread_rng().fill_bytes(&mut ident);
ident
}

pub fn generate_bytes_from_string(s: &String) -> Result<Vec<u8>> {
chenwanqq marked this conversation as resolved.
Show resolved Hide resolved
let mut ident = Vec::new();
let mut i = 0;
while i < s.len() {
let byte = u8::from_str_radix(&s[i..i + 2], 16).context("should be a valid hex string")?;
ident.push(byte);
i += 2;
}
Ok(ident)
}

pub fn generate_string_from_bytes(bytes: &Vec<u8>) -> Result<String> {
chenwanqq marked this conversation as resolved.
Show resolved Hide resolved
//Generate hex string from bytes, for example 0x00 0x01 0x02 0x03 -> "00010203"
let mut ident = String::new();
for byte in bytes {
ident.push_str(&format!("{:02x}", byte));
}
Ok(ident)
}

#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone, Default)]
pub struct IdentifierFileStruct {
chenwanqq marked this conversation as resolved.
Show resolved Hide resolved
pub identifier: String,
pub duid_config: Option<ServerDuid>,
}

impl IdentifierFileStruct {
pub fn new(identifier: &str, duid_config: &ServerDuid) -> Self {
Self {
identifier: identifier.to_owned(),
duid_config: Some(duid_config.clone()),
}
}

pub fn to_json(&self, path: &Path) -> Result<()> {
let file = std::fs::File::create(path)?;
serde_json::to_writer_pretty(file, self)?;
Ok(())
}

pub fn from_json(path: &Path) -> Result<Self> {
let file = std::fs::File::open(path)?;
let identifier_file_struct: IdentifierFileStruct = serde_json::from_reader(file)?;
Ok(identifier_file_struct)
}

pub fn duid(&self) -> Result<Duid> {
let duid_bytes = generate_bytes_from_string(&self.identifier)
.context("server identifier should be a valid hex string")?;
Ok(Duid::from(duid_bytes))
}
}
Loading
Loading