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 all 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 .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ target/
data/
*.db*
.vscode/*.log
server_id
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.

19 changes: 12 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:
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.
path: ./server_id # optional. default is /var/lib/dora/server_id
# global options (optional)
options:
values:
Expand All @@ -222,7 +227,7 @@ 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
# 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
29 changes: 29 additions & 0 deletions libs/config/sample/config_v6.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
v6:
server_id:
type: LLT
identifier: fe80::c981:b769:461a:bfb4
time: 1111112
hardware_type: 1
persist: true
path: ./server_id
options:
values:
23:
type: ip_list
value:
- 2001:db8::1
- 2001:db8::2
networks:
2001:db8:1::/64:
config:
lease_time:
default: 3600
preferred_time:
default: 3600
options:
values:
23:
type: ip_list
value:
- 2001:db8::1
- 2001:db8::2
28 changes: 28 additions & 0 deletions libs/config/sample/config_v6_EN.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
v6:
server_id:
type: EN
identifier: 1122FFFE3842
enterprise_id: 23
persist: true
path: ./server_id
options:
values:
23:
type: ip_list
value:
- 2001:db8::1
- 2001:db8::2
networks:
2001:db8:1::/64:
config:
lease_time:
default: 3600
preferred_time:
default: 3600
options:
values:
23:
type: ip_list
value:
- 2001:db8::1
- 2001:db8::2
28 changes: 28 additions & 0 deletions libs/config/sample/config_v6_LL.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
v6:
server_id:
type: LL
identifier: fe80::c981:b769:461a:bfb4
hardware_type: 2
persist: true
path: ./server_id
options:
values:
23:
type: ip_list
value:
- 2001:db8::1
- 2001:db8::2
networks:
2001:db8:1::/64:
config:
lease_time:
default: 3600
preferred_time:
default: 3600
options:
values:
23:
type: ip_list
value:
- 2001:db8::1
- 2001:db8::2
27 changes: 27 additions & 0 deletions libs/config/sample/config_v6_UUID.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
v6:
server_id:
type: UUID
identifier: 451c810bf191a92abf3768dd1ed61f3a
persist: true
path: ./server_id
options:
values:
23:
type: ip_list
value:
- 2001:db8::1
- 2001:db8::2
networks:
2001:db8:1::/64:
config:
lease_time:
default: 3600
preferred_time:
default: 3600
options:
values:
23:
type: ip_list
value:
- 2001:db8::1
- 2001:db8::2
25 changes: 25 additions & 0 deletions libs/config/sample/config_v6_no_persist.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
v6:
server_id:
type: LLT
persist: false
options:
values:
23:
type: ip_list
value:
- 2001:db8::1
- 2001:db8::2
networks:
2001:db8:1::/64:
config:
lease_time:
default: 3600
preferred_time:
default: 3600
options:
values:
23:
type: ip_list
value:
- 2001:db8::1
- 2001:db8::2
36 changes: 35 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::ServerDuidInfo;
/// server config
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DhcpConfig {
Expand Down Expand Up @@ -179,3 +182,34 @@ 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
}

#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)]
pub struct PersistIdentifier {
pub identifier: String,
pub duid_config: ServerDuidInfo,
}

impl PersistIdentifier {
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)?;
Ok(serde_json::from_reader(file)?)
}

pub fn duid(&self) -> Result<Duid> {
let duid_bytes = hex::decode(&self.identifier)
.context("server identifier should be a valid hex string")?;
Ok(Duid::from(duid_bytes))
}
}
1 change: 1 addition & 0 deletions libs/config/src/v4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -666,6 +666,7 @@ impl FloodThreshold {

#[cfg(test)]
mod tests {

use dora_core::dhcproto::v4;

use super::*;
Expand Down
Loading
Loading