NFTables.Expr.Port (NFTables v0.8.1)
View SourcePort matching functions for Expr.
Provides protocol-agnostic port matching for TCP, UDP, SCTP, and DCCP. The protocol context is determined by earlier protocol calls (tcp(), udp(), sctp(), or dccp()) in the match chain.
Supports both single ports and port ranges using Elixir ranges.
Import
import NFTables.Expr.PortExamples
# TCP port matching
rule() |> tcp() |> dport(80)
rule() |> tcp() |> sport(1024)
# UDP port matching
rule() |> udp() |> dport(53)
rule() |> udp() |> sport(5353)
# SCTP port matching
rule() |> sctp() |> dport(9899)
rule() |> sctp() |> sport(5000)
# DCCP port matching
rule() |> dccp() |> dport(6000)
# Port ranges (all protocols)
rule() |> tcp() |> dport(8000..9000)
rule() |> sctp() |> sport(1024..65535)For more information, see the nftables payload expressions wiki.
Summary
Functions
Match destination port.
Alias for dport/2. Match destination port.
Convenience alias for dport/2. Match destination port.
Match source port.
Alias for sport/2. Match source port.
Functions
@spec dport(NFTables.Expr.t(), non_neg_integer() | Range.t()) :: NFTables.Expr.t()
Match destination port.
Works with TCP, UDP, SCTP, and DCCP based on the protocol context set by tcp(), udp(), sctp(), or dccp(). Supports single ports (integer) or port ranges (Range).
Examples
# Single port
rule() |> tcp() |> dport(80)
rule() |> udp() |> dport(53)
rule() |> sctp() |> dport(9899)
rule() |> dccp() |> dport(6000)
# Port range
rule() |> tcp() |> dport(8000..9000)
rule() |> sctp() |> dport(1024..65535)Errors
Raises ArgumentError if called without a protocol context (tcp/udp/sctp/dccp).
@spec dst_port(NFTables.Expr.t(), non_neg_integer() | Range.t()) :: NFTables.Expr.t()
Alias for dport/2. Match destination port.
Examples
rule() |> tcp() |> dst_port(443)
rule() |> udp() |> dst_port(53)
@spec port(NFTables.Expr.t(), non_neg_integer() | Range.t()) :: NFTables.Expr.t()
Convenience alias for dport/2. Match destination port.
Supports dual-arity: can start a new expression or continue an existing one.
Examples
# Single port
tcp() |> port(22)
# Port range
tcp() |> port(8000..9000)
@spec sport(NFTables.Expr.t(), non_neg_integer() | Range.t()) :: NFTables.Expr.t()
Match source port.
Works with TCP, UDP, SCTP, and DCCP based on the protocol context set by tcp(), udp(), sctp(), or dccp(). Supports single ports (integer) or port ranges (Range).
Examples
# Single port
rule() |> tcp() |> sport(1024)
rule() |> udp() |> sport(5353)
rule() |> sctp() |> sport(5000)
rule() |> dccp() |> sport(4000)
# Port range
rule() |> tcp() |> sport(1024..65535)
rule() |> udp() |> sport(10000..20000)Errors
Raises ArgumentError if called without tcp() or udp() first.
@spec src_port(NFTables.Expr.t(), non_neg_integer() | Range.t()) :: NFTables.Expr.t()
Alias for sport/2. Match source port.
Examples
rule() |> tcp() |> src_port(1024)
rule() |> tcp() |> src_port(1024..65535)