Add wwan networking
This commit is contained in:
parent
a9e5fa79a5
commit
bf015614b6
4 changed files with 88 additions and 111 deletions
|
@ -5,7 +5,7 @@
|
||||||
{ config, pkgs, ... }:
|
{ config, pkgs, ... }:
|
||||||
|
|
||||||
let routerConfig = {
|
let routerConfig = {
|
||||||
wanCardAddress = "00:0d:b9:51:a2:74";
|
wanInterface = "wwp0s19u1u3i12";
|
||||||
wireless = {
|
wireless = {
|
||||||
wleInterface = "wlp5s0";
|
wleInterface = "wlp5s0";
|
||||||
wleSSID = "hauser";
|
wleSSID = "hauser";
|
||||||
|
@ -25,12 +25,13 @@ let routerConfig = {
|
||||||
in
|
in
|
||||||
|
|
||||||
{
|
{
|
||||||
|
|
||||||
imports =
|
imports =
|
||||||
[
|
[
|
||||||
../hardware/apu.nix
|
../hardware/apu.nix
|
||||||
../hardware/wle900vx.nix
|
../hardware/wle900vx.nix
|
||||||
../role/base.nix
|
../role/base.nix
|
||||||
../role/wwan.nix
|
../role/networking/wwan.nix
|
||||||
(import ../role/router.nix routerConfig)
|
(import ../role/router.nix routerConfig)
|
||||||
];
|
];
|
||||||
|
|
||||||
|
@ -45,6 +46,7 @@ in
|
||||||
];
|
];
|
||||||
|
|
||||||
services.wwan = {
|
services.wwan = {
|
||||||
|
enable = true;
|
||||||
apn = "gprs.swisscom.ch";
|
apn = "gprs.swisscom.ch";
|
||||||
networkInterface = "wwp0s19u1u3i12";
|
networkInterface = "wwp0s19u1u3i12";
|
||||||
};
|
};
|
||||||
|
|
|
@ -7,23 +7,15 @@ with lib;
|
||||||
|
|
||||||
let
|
let
|
||||||
cfg = config.services.wwan;
|
cfg = config.services.wwan;
|
||||||
mbim-ip = pkgs.writeScriptBin "mbim-ip" readFile ./wwan/mbim-ip.bash;
|
mbim-ip-configured = pkgs.writeScriptBin "mbim-ip-configured" (''
|
||||||
mbim-ip-configured = pkgs.writeScriptBin "mbim-ip-configured" ''
|
|
||||||
#!${pkgs.stdenv.shell}
|
#!${pkgs.stdenv.shell}
|
||||||
MBIM_BINARY=${pkgs.libmbim}/bin/mbimcli
|
|
||||||
MBIM_INTERFACE=${cfg.mbimInterface}
|
MBIM_INTERFACE=${cfg.mbimInterface}
|
||||||
exec ${mbim-ip} $@
|
'' + (readFile ./wwan/mbim-ip.bash));
|
||||||
'';
|
|
||||||
in
|
in
|
||||||
{
|
{
|
||||||
options = {
|
options.services.wwan = {
|
||||||
services.wwan = {
|
|
||||||
enable = mkEnableOption "wwan client service";
|
enable = mkEnableOption "wwan client service";
|
||||||
|
|
||||||
config = mkOption {
|
|
||||||
type = types.attrsOf (types.submodule (
|
|
||||||
{
|
|
||||||
options = {
|
|
||||||
apn = mkOption {
|
apn = mkOption {
|
||||||
type = types.str;
|
type = types.str;
|
||||||
description = ''
|
description = ''
|
||||||
|
@ -48,8 +40,7 @@ in
|
||||||
};
|
};
|
||||||
|
|
||||||
apnAuth = mkOption {
|
apnAuth = mkOption {
|
||||||
type = types.enum;
|
type = types.enum [ "PAP" "CHAP" "MSCHAPV2" "" ];
|
||||||
values = [ "PAP" "CHAP" "MSCHAPV2" "" ];
|
|
||||||
default = "";
|
default = "";
|
||||||
description = ''
|
description = ''
|
||||||
APN authentication type, one of ${concatMapStringsSep ", " show values} (optional).
|
APN authentication type, one of ${concatMapStringsSep ", " show values} (optional).
|
||||||
|
@ -65,9 +56,11 @@ in
|
||||||
};
|
};
|
||||||
|
|
||||||
mbimInterface = mkOption {
|
mbimInterface = mkOption {
|
||||||
type = types.path;
|
type = types.str;
|
||||||
default = /dev/cdc-wdm0
|
default = "/dev/cdc-wdm0";
|
||||||
description = "MBIM Interface which the connection will use.";
|
description = ''
|
||||||
|
MBIM Interface which the connection will use.
|
||||||
|
'';
|
||||||
};
|
};
|
||||||
|
|
||||||
networkInterface = mkOption {
|
networkInterface = mkOption {
|
||||||
|
@ -75,52 +68,30 @@ in
|
||||||
description = "Name of the WWAN network interface";
|
description = "Name of the WWAN network interface";
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
}
|
|
||||||
));
|
|
||||||
|
|
||||||
default = {};
|
|
||||||
|
|
||||||
example = literalExample ''
|
|
||||||
{
|
|
||||||
wwan = {
|
|
||||||
apn = "gprs.swisscom.ch";
|
|
||||||
networkInterface = "wwp0s19u1u3i12";
|
|
||||||
};
|
|
||||||
}
|
|
||||||
'';
|
|
||||||
|
|
||||||
description = ''
|
|
||||||
Configuration for WWAN connectivity using a MBIM capable card.
|
|
||||||
'';
|
|
||||||
};
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
config = mkIf cfg.enable {
|
config = mkIf cfg.enable {
|
||||||
systemd.services."wwan" = {
|
systemd.services."wwan" = {
|
||||||
description = "WWAN connectivity";
|
description = "WWAN connectivity";
|
||||||
wantedBy = [ "network.target" ];
|
wantedBy = [ "network.target" ];
|
||||||
wants = [ "sys-subsystem-net-devices-${cfg.networkInterface}.device"];
|
bindsTo = [ "network-addresses-${cfg.networkInterface}.service" ];
|
||||||
|
path = [ pkgs.libmbim pkgs.iproute ];
|
||||||
|
|
||||||
serviceConfig = {
|
serviceConfig = {
|
||||||
ExecStart = "@${pkgs.libmbim}/bin/mbim-network ${toString cfg.mbimInterface} start";
|
ExecStart = "${mbim-ip-configured}/bin/mbim-ip-configured start ${cfg.networkInterface}";
|
||||||
ExecStop = "@${pkgs.libmbim}/bin/mbim-network ${toString cfg.mbimInterface} stop";
|
ExecStop = "${mbim-ip-configured}/bin/mbim-ip-configured stop ${cfg.networkInterface}";
|
||||||
|
|
||||||
# MBIM networking is a special fellow - it gets the IP address for us,
|
|
||||||
# but we need to manually set it to the interface
|
|
||||||
ExecStartPost = "@${mbim-ip-configured} start ${cfg.networkInterface}";
|
|
||||||
ExecStopPre = "@${mbim-ip-configured} stop ${cfg.networkInterface}";
|
|
||||||
|
|
||||||
RemainAfterExit = true;
|
RemainAfterExit = true;
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
environment.etc."/etc/mbim-network.conf".text = ''
|
environment.etc."mbim-network.conf".text = ''
|
||||||
APN=${cfg.apnUser}
|
APN=${cfg.apn}
|
||||||
APN_USER=${cfg.apnUser}
|
APN_USER=${cfg.apnUser}
|
||||||
APN_PASS=${cfg.apnPass}
|
APN_PASS=${cfg.apnPass}
|
||||||
APN_AUTH=${cfg.apnAuth}
|
APN_AUTH=${cfg.apnAuth}
|
||||||
PROXY=${optionalString cfg.proxy "yes"}
|
PROXY=${optionalString cfg.mbimProxy "yes"}
|
||||||
'';
|
'';
|
||||||
|
|
||||||
|
networking.interfaces.${cfg.networkInterface}.useDHCP = false;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,10 +13,6 @@ if [ "$MBIM_INTERFACE" == "" ]; then
|
||||||
MBIM_INTERFACE="/dev/cdc-wdm0"
|
MBIM_INTERFACE="/dev/cdc-wdm0"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [ "$MBIM_BINARY" == "" ]; then
|
|
||||||
MBIM_BINARY=mbimcli
|
|
||||||
fi
|
|
||||||
|
|
||||||
###############################################################################
|
###############################################################################
|
||||||
# Global Variables
|
# Global Variables
|
||||||
###############################################################################
|
###############################################################################
|
||||||
|
@ -32,6 +28,10 @@ ipv6_gateway=""
|
||||||
ipv6_dns=()
|
ipv6_dns=()
|
||||||
ipv6_mtu=""
|
ipv6_mtu=""
|
||||||
|
|
||||||
|
export previous_state state skip_line \
|
||||||
|
ipv4_addresses ipv4_gateway ipv4_dns ipv4_mtu \
|
||||||
|
ipv6_addresses ipv6_gateway ipv6_dns ipv6_mtu
|
||||||
|
|
||||||
###############################################################################
|
###############################################################################
|
||||||
# Function
|
# Function
|
||||||
###############################################################################
|
###############################################################################
|
||||||
|
@ -124,13 +124,13 @@ function parse_input_state_machine {
|
||||||
state="start"
|
state="start"
|
||||||
while true; do
|
while true; do
|
||||||
if [[ "$skip_line" == 0 ]]; then
|
if [[ "$skip_line" == 0 ]]; then
|
||||||
read line
|
read line || break # TODO: Clean up
|
||||||
else
|
else
|
||||||
skip_line=0
|
skip_line=0
|
||||||
fi
|
fi
|
||||||
case "$state" in
|
case "$state" in
|
||||||
"start")
|
"start")
|
||||||
read line # first line is empty, read a new one #TODO: This is not very clean...
|
read line || break # first line is empty, read a new one #TODO: This is not very clean...
|
||||||
case "$line" in
|
case "$line" in
|
||||||
*"configuration available: 'none'"*)
|
*"configuration available: 'none'"*)
|
||||||
# Skip none state
|
# Skip none state
|
||||||
|
@ -267,17 +267,17 @@ function parse_input_state_machine {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
interface_start(){
|
interface_stop(){
|
||||||
ip addr flush dev $DEV
|
ip addr flush dev $DEV
|
||||||
ip route flush dev $DEV
|
ip route flush dev $DEV
|
||||||
|
|
||||||
ip -6 addr flush dev $DEV
|
ip -6 addr flush dev $DEV
|
||||||
ip -6 p route flush dev $DEV
|
ip -6 route flush dev $DEV
|
||||||
|
|
||||||
#TODO: Nameserver?
|
#TODO: Nameserver?
|
||||||
}
|
}
|
||||||
|
|
||||||
interface_stop() {
|
interface_start() {
|
||||||
ip link set $DEV up
|
ip link set $DEV up
|
||||||
|
|
||||||
if [[ "${#ipv4_addresses[@]}" > 0 ]]; then
|
if [[ "${#ipv4_addresses[@]}" > 0 ]]; then
|
||||||
|
@ -285,6 +285,8 @@ interface_stop() {
|
||||||
ip link set $DEV mtu $ipv4_mtu
|
ip link set $DEV mtu $ipv4_mtu
|
||||||
ip route add default via $ipv4_gateway dev $DEV
|
ip route add default via $ipv4_gateway dev $DEV
|
||||||
#TODO: nameserver ${ipv4_dns[@]}
|
#TODO: nameserver ${ipv4_dns[@]}
|
||||||
|
else
|
||||||
|
echo "No IPv4 address, skipping v4 configuration..."
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [[ "${#ipv6_addresses[@]}" > 0 ]]; then
|
if [[ "${#ipv6_addresses[@]}" > 0 ]]; then
|
||||||
|
@ -292,6 +294,8 @@ interface_stop() {
|
||||||
ip -6 route add default via $ipv6_gateway dev $DEV
|
ip -6 route add default via $ipv6_gateway dev $DEV
|
||||||
ip -6 link set $DEV mtu $ipv6_mtu
|
ip -6 link set $DEV mtu $ipv6_mtu
|
||||||
#TODO: nameserver ${ipv6_dns[@]}"
|
#TODO: nameserver ${ipv6_dns[@]}"
|
||||||
|
else
|
||||||
|
echo "No IPv6 address, skipping v6 configuration..."
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -299,17 +303,23 @@ interface_stop() {
|
||||||
# Execution
|
# Execution
|
||||||
###############################################################################
|
###############################################################################
|
||||||
set -x
|
set -x
|
||||||
|
set -e
|
||||||
echo "NOTE: This script does not yet support nameserver configuration."
|
echo "NOTE: This script does not yet support nameserver configuration."
|
||||||
|
|
||||||
parse_input_state_machine <(${MBIM_BINARY} -d ${MBIM_INTERFACE} -p --query-ip-configuration=0)
|
|
||||||
print_full_configuration
|
|
||||||
|
|
||||||
case "$MODE" in
|
case "$MODE" in
|
||||||
"start")
|
"start")
|
||||||
|
mbim-network $MBIM_INTERFACE start
|
||||||
|
sleep 1
|
||||||
|
mbimcli -d $MBIM_INTERFACE -p --query-ip-configuration=0 | {
|
||||||
|
parse_input_state_machine
|
||||||
|
print_full_configuration
|
||||||
interface_stop
|
interface_stop
|
||||||
|
interface_start
|
||||||
|
}
|
||||||
;;
|
;;
|
||||||
"stop")
|
"stop")
|
||||||
interface_start
|
mbim-network $MBIM_INTERFACE stop
|
||||||
|
interface_stop
|
||||||
;;
|
;;
|
||||||
*)
|
*)
|
||||||
echo "USAGE: $0 start|stop INTERFACE" >&2
|
echo "USAGE: $0 start|stop INTERFACE" >&2
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
{
|
{
|
||||||
# To get the MAC address of each card, use this command: cat /sys/class/net/*device_name*/address
|
# To get the MAC address of each card, use this command: cat /sys/class/net/*device_name*/address
|
||||||
# Make sure to use the lower-case hex values in your udev rules. It does not like upper-case.
|
# Make sure to use the lower-case hex values in your udev rules. It does not like upper-case.
|
||||||
wanCardAddress ? "00:0d:b9:48:55:be",
|
wanInterface,
|
||||||
wireless ? {
|
wireless ? {
|
||||||
wleInterface = "wlp5s0";
|
wleInterface = "wlp5s0";
|
||||||
wleSSID = "hauser";
|
wleSSID = "hauser";
|
||||||
|
@ -27,16 +27,10 @@ in
|
||||||
(import ./networking/dns-recursive.nix lanNetwork)
|
(import ./networking/dns-recursive.nix lanNetwork)
|
||||||
];
|
];
|
||||||
|
|
||||||
# To get the MAC address of each card, use this command: cat /sys/class/net/*device_name*/address
|
|
||||||
# Make sure to use the lower-case hex values in your udev rules. It does not like upper-case.
|
|
||||||
services.udev.extraRules = ''
|
|
||||||
SUBSYSTEM=="net", ACTION=="add", ATTR{address}=="${wanCardAddress}", NAME="wan"
|
|
||||||
'';
|
|
||||||
|
|
||||||
networking = {
|
networking = {
|
||||||
nat = {
|
nat = {
|
||||||
enable = true;
|
enable = true;
|
||||||
externalInterface = "wan";
|
externalInterface = wanInterface;
|
||||||
internalInterfaces = [ "lan" ];
|
internalInterfaces = [ "lan" ];
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue