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 redirections = mkOption {
22 description = "UPnP redirections to request.";
24 type = types.listOf (types.submodule ({ config, ... }: {
25 options.externalPort = mkOption {
26 description = "External port to open on the redirecting device.";
29 options.internalPort = mkOption {
30 description = "Internal port, target of the redirection.";
32 default = config.externalPort;
34 options.protocol = mkOption {
35 description = "Protocol to redirect.";
36 type = with types; enum [ "TCP" "UDP" ];
39 options.description = mkOption {
40 description = "Description of the port mapping";
44 options.duration = mkOption {
45 description = "Duration of the redirection, in seconds. 0 means indefinitely.";
49 options.maintainPeriod = mkOption {
50 description = "Period (in seconds) between runs to maintain the redirection.";
51 type = with types; nullOr int;
52 default = if config.duration > 0 then config.duration / 2 else null;
53 defaultText = "if duration > 0 then duration / 2 else null";
55 options.override = mkOption {
56 description = "Try to override the redirection in case of conflict in mapping entry.";
60 options.service = mkOption {
61 description = "Configuration specific to the systemd service handling this UPnP redirecting.";
69 systemd.services = listToAttrs (map
71 nameValuePair "upnpc-${toString r.internalPort}" (mkMerge [
73 description = "UPnP ${toString r.internalPort}";
74 after = [ "network-pre.target" ];
75 #wantedBy = [ "multi-user.target" ];
76 path = [ pkgs.miniupnpc ];
78 Type = if r.maintainPeriod == null then "oneshot" else "simple";
79 RemainAfterExit = r.maintainPeriod == null;
80 ExecStart = pkgs.writeShellScript "upnpc-start-${toString r.internalPort}" ''
84 while IFS= read -r line; do
87 (*" is redirected to internal $localIP:${toString r.internalPort}"*) result=ok ;;
88 (*ConflictInMappingEntry*) result=conflict ;;
91 $(upnpc -u "$desc" ${optionalString (r.description != "") "-e \"${r.description}\""} \
92 -a "$localIP" ${toString r.internalPort} ${toString r.externalPort} ${r.protocol} ${toString r.duration} 2>&1)
98 ${optionalString r.override ''
99 test "$result" != conflict || {
100 upnpc -u "$desc" -d ${toString r.externalPort} ${r.protocol}
105 (ok) ${if r.maintainPeriod == null then "break" else "sleep " + toString r.maintainPeriod} ;;
110 ExecStop = "${pkgs.miniupnpc}/bin/upnpc -d ${toString r.externalPort} ${r.protocol}";
111 Restart = "on-failure";
112 RestartSec = mkDefault r.maintainPeriod;
114 User = users."upnpc".name;
122 # This enables to match on the uid in the firewall.
123 users.users."upnpc" = {
125 group = groups."upnpc".name;
127 users.groups."upnpc" = { };
128 networking.nftables.ruleset =
129 lib.optionalString (cfg.redirections != [ ]) ''
131 # A set containing the udp port(s) to which SSDP replies are allowed.
137 # Create a rule for accepting any SSDP packets going to a remembered port.
138 udp dport @upnpc-ssdp counter accept comment "SSDP answer"
141 skuid ${users.upnpc.name} \
144 comment "SSDP automatic opening"
145 skuid ${users.upnpc.name} \
146 ip daddr 239.255.255.250 udp dport ssdp \
147 set add udp sport @upnpc-ssdp \
148 comment "SSDP automatic opening"
149 skuid ${users.upnpc.name} \
150 ip daddr 239.255.255.250 udp dport ssdp \
155 '' + lib.optionalString config.networking.enableIPv6 ''
158 skuid ${users.upnpc.name} \
159 ip6 daddr { FF02::C, FF05::C, FF08::C, FF0E::C } \
161 set add udp sport @upnpc-ssdp \
162 comment "SSDP automatic opening"
163 skuid ${users.upnpc.name} \
164 ip6 daddr { FF02::C, FF05::C, FF08::C, FF0E::C } \
166 counter accept comment "SSDP"
171 meta.maintainers = with maintainers; [ julm ];