Skip to content

Commit bbb6189

Browse files
kristrevdavem330
authored andcommitted
inet_diag: Add equal-operator for ports
inet_diag currently provides less/greater than or equal operators for comparing ports when filtering sockets. An equal comparison can be performed by combining the two existing operators, or a user can for example request a port range and then do the final filtering in userspace. However, these approaches both have drawbacks. Implementing equal using LE/GE causes the size and complexity of a filter to grow quickly as the number of ports increase, while it on busy machines would be great if the kernel only returns information about relevant sockets. This patch introduces source and destination port equal operators. INET_DIAG_BC_S_EQ is used to match a source port, INET_DIAG_BC_D_EQ a destination port, and usage is the same as for the existing port operators. I.e., the port to match is stored in the no-member of the next inet_diag_bc_op-struct in the filter. Signed-off-by: Kristian Evensen <kristian.evensen@gmail.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent d323b52 commit bbb6189

2 files changed

Lines changed: 10 additions & 0 deletions

File tree

‎include/uapi/linux/inet_diag.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ enum {
9292
INET_DIAG_BC_D_COND,
9393
INET_DIAG_BC_DEV_COND, /* u32 ifindex */
9494
INET_DIAG_BC_MARK_COND,
95+
INET_DIAG_BC_S_EQ,
96+
INET_DIAG_BC_D_EQ,
9597
};
9698

9799
struct inet_diag_hostcond {

‎net/ipv4/inet_diag.c‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -564,12 +564,18 @@ static int inet_diag_bc_run(const struct nlattr *_bc,
564564
case INET_DIAG_BC_JMP:
565565
yes = 0;
566566
break;
567+
case INET_DIAG_BC_S_EQ:
568+
yes = entry->sport == op[1].no;
569+
break;
567570
case INET_DIAG_BC_S_GE:
568571
yes = entry->sport >= op[1].no;
569572
break;
570573
case INET_DIAG_BC_S_LE:
571574
yes = entry->sport <= op[1].no;
572575
break;
576+
case INET_DIAG_BC_D_EQ:
577+
yes = entry->dport == op[1].no;
578+
break;
573579
case INET_DIAG_BC_D_GE:
574580
yes = entry->dport >= op[1].no;
575581
break;
@@ -802,8 +808,10 @@ static int inet_diag_bc_audit(const struct nlattr *attr,
802808
if (!valid_devcond(bc, len, &min_len))
803809
return -EINVAL;
804810
break;
811+
case INET_DIAG_BC_S_EQ:
805812
case INET_DIAG_BC_S_GE:
806813
case INET_DIAG_BC_S_LE:
814+
case INET_DIAG_BC_D_EQ:
807815
case INET_DIAG_BC_D_GE:
808816
case INET_DIAG_BC_D_LE:
809817
if (!valid_port_comparison(bc, len, &min_len))

0 commit comments

Comments
 (0)