1 { pkgs, lib, config, ... }:
4 inherit (config.users) users groups;
5 cfg = config.services.upnpc;
7 while IFS=: read -r k v; do
8 k=$(printf %s "$k" | sed -e 's/^\s*//' -e 's/\s*$//')
9 v=$(printf %s "$v" | sed -e 's/^\s*//' -e 's/\s*$//')
12 ("Local LAN ip address") localIP=$v;;
20 options.services.upnpc = {
21 enable = mkEnableOption "UPnP redirections";
22 redirections = mkOption {
23 description = "UPnP redirections to request.";
25 type = types.listOf (types.submodule ({ config, ... }: {
26 options.externalPort = mkOption {
27 description = "External port to open on the redirecting device.";
30 options.internalPort = mkOption {
31 description = "Internal port, target of the redirection.";
33 default = config.externalPort;
35 options.protocol = mkOption {
36 description = "Protocol to redirect.";
37 type = with types; enum [ "TCP" "UDP" ];
40 options.description = mkOption {
41 description = "Description of the port mapping";
45 options.duration = mkOption {
46 description = "Duration of the redirection, in seconds. 0 means indefinitely.";
50 options.maintainPeriod = mkOption {
51 description = "Period (in seconds) between runs to maintain the redirection.";
52 type = with types; nullOr int;
53 default = if config.duration > 0 then config.duration / 2 else null;
54 defaultText = "if duration > 0 then duration / 2 else null";
56 options.override = mkOption {
57 description = "Try to override the redirection in case of conflict in mapping entry.";
61 options.service = mkOption {
62 description = "Configuration specific to the systemd service handling this UPnP redirecting.";
69 config = mkIf cfg.enable {
70 systemd.services = listToAttrs (map
72 nameValuePair "upnpc-${toString r.internalPort}" (mkMerge [
74 description = "UPnP ${toString r.internalPort}";
75 after = [ "network-pre.target" ];
76 #wantedBy = [ "multi-user.target" ];
77 path = [ pkgs.miniupnpc ];
79 Type = if r.maintainPeriod == null then "oneshot" else "simple";
80 RemainAfterExit = r.maintainPeriod == null;
81 ExecStart = pkgs.writeShellScript "upnpc-start-${toString r.internalPort}" ''
85 while IFS= read -r line; do
88 (*" is redirected to internal $localIP:${toString r.internalPort}"*) result=ok ;;
89 (*ConflictInMappingEntry*) result=conflict ;;
92 $(upnpc -u "$desc" ${optionalString (r.description != "") "-e \"${r.description}\""} \
93 -a "$localIP" ${toString r.internalPort} ${toString r.externalPort} ${r.protocol} ${toString r.duration} 2>&1)
99 ${optionalString r.override ''
100 test "$result" != conflict || {
101 upnpc -u "$desc" -d ${toString r.externalPort} ${r.protocol}
106 (ok) ${if r.maintainPeriod == null then "break" else "sleep " + toString r.maintainPeriod} ;;
111 ExecStop = "${pkgs.miniupnpc}/bin/upnpc -d ${toString r.externalPort} ${r.protocol}";
112 Restart = "on-failure";
114 User = users."upnpc".name;
115 } // lib.optionalAttrs (r.maintainPeriod != null) {
116 RestartSec = mkDefault r.maintainPeriod;
124 # This enables to match on the uid in the firewall.
125 users.users."upnpc" = {
127 group = groups."upnpc".name;
129 users.groups."upnpc" = { };
130 networking.nftables.ruleset =
131 lib.optionalString (cfg.redirections != [ ]) ''
133 # A set containing the udp port(s) to which SSDP replies are allowed.
139 # Create a rule for accepting any SSDP packets going to a remembered port.
140 udp dport @upnpc-ssdp counter accept comment "SSDP answer"
143 skuid ${users.upnpc.name} \
146 comment "SSDP automatic opening"
147 skuid ${users.upnpc.name} \
148 ip daddr 239.255.255.250 udp dport ssdp \
149 set add udp sport @upnpc-ssdp \
150 comment "SSDP automatic opening"
151 skuid ${users.upnpc.name} \
152 ip daddr 239.255.255.250 udp dport ssdp \
157 '' + lib.optionalString config.networking.enableIPv6 ''
160 skuid ${users.upnpc.name} \
161 ip6 daddr { FF02::C, FF05::C, FF08::C, FF0E::C } \
163 set add udp sport @upnpc-ssdp \
164 comment "SSDP automatic opening"
165 skuid ${users.upnpc.name} \
166 ip6 daddr { FF02::C, FF05::C, FF08::C, FF0E::C } \
168 counter accept comment "SSDP"
173 meta.maintainers = with maintainers; [ julm ];