Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

netfilter: xt_socket: add XT_SOCKET_RESTORESKMARK flag

xt_socket is useful for matching sockets with IP_TRANSPARENT and
taking some action on the matching packets. However, it lacks the
ability to match only a small subset of transparent sockets.

Suppose there are 2 applications, each with its own set of transparent
sockets. The first application wants all matching packets dropped,
while the second application wants them forwarded somewhere else.

Add the ability to retore the skb->mark from the sk_mark. The mark
is only restored if a matching socket is found and the transparent /
nowildcard conditions are satisfied.

Now the 2 hypothetical applications can differentiate their sockets
based on a mark value set with SO_MARK.

iptables -t mangle -I PREROUTING -m socket --transparent \
--restore-skmark -j action
iptables -t mangle -A action -m mark --mark 10 -j action2
iptables -t mangle -A action -m mark --mark 11 -j action3

Signed-off-by: Harout Hedeshian <harouth@codeaurora.org>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>

authored by

Harout Hedeshian and committed by
Pablo Neira Ayuso
01555e74 ef493bd9

+61 -6
+8
include/uapi/linux/netfilter/xt_socket.h
··· 6 6 enum { 7 7 XT_SOCKET_TRANSPARENT = 1 << 0, 8 8 XT_SOCKET_NOWILDCARD = 1 << 1, 9 + XT_SOCKET_RESTORESKMARK = 1 << 2, 9 10 }; 10 11 11 12 struct xt_socket_mtinfo1 { ··· 18 17 __u8 flags; 19 18 }; 20 19 #define XT_SOCKET_FLAGS_V2 (XT_SOCKET_TRANSPARENT | XT_SOCKET_NOWILDCARD) 20 + 21 + struct xt_socket_mtinfo3 { 22 + __u8 flags; 23 + }; 24 + #define XT_SOCKET_FLAGS_V3 (XT_SOCKET_TRANSPARENT \ 25 + | XT_SOCKET_NOWILDCARD \ 26 + | XT_SOCKET_RESTORESKMARK) 21 27 22 28 #endif /* _XT_SOCKET_H */
+53 -6
net/netfilter/xt_socket.c
··· 205 205 socket_match(const struct sk_buff *skb, struct xt_action_param *par, 206 206 const struct xt_socket_mtinfo1 *info) 207 207 { 208 + struct sk_buff *pskb = (struct sk_buff *)skb; 208 209 struct sock *sk = skb->sk; 209 210 210 211 if (!sk) ··· 226 225 */ 227 226 if (info->flags & XT_SOCKET_TRANSPARENT) 228 227 transparent = xt_socket_sk_is_transparent(sk); 228 + 229 + if (info->flags & XT_SOCKET_RESTORESKMARK && !wildcard && 230 + transparent) 231 + pskb->mark = sk->sk_mark; 229 232 230 233 if (sk != skb->sk) 231 234 sock_gen_put(sk); ··· 252 247 } 253 248 254 249 static bool 255 - socket_mt4_v1_v2(const struct sk_buff *skb, struct xt_action_param *par) 250 + socket_mt4_v1_v2_v3(const struct sk_buff *skb, struct xt_action_param *par) 256 251 { 257 252 return socket_match(skb, par, par->matchinfo); 258 253 } ··· 376 371 } 377 372 378 373 static bool 379 - socket_mt6_v1_v2(const struct sk_buff *skb, struct xt_action_param *par) 374 + socket_mt6_v1_v2_v3(const struct sk_buff *skb, struct xt_action_param *par) 380 375 { 381 376 const struct xt_socket_mtinfo1 *info = (struct xt_socket_mtinfo1 *) par->matchinfo; 377 + struct sk_buff *pskb = (struct sk_buff *)skb; 382 378 struct sock *sk = skb->sk; 383 379 384 380 if (!sk) ··· 400 394 */ 401 395 if (info->flags & XT_SOCKET_TRANSPARENT) 402 396 transparent = xt_socket_sk_is_transparent(sk); 397 + 398 + if (info->flags & XT_SOCKET_RESTORESKMARK && !wildcard && 399 + transparent) 400 + pskb->mark = sk->sk_mark; 403 401 404 402 if (sk != skb->sk) 405 403 sock_gen_put(sk); ··· 438 428 return 0; 439 429 } 440 430 431 + static int socket_mt_v3_check(const struct xt_mtchk_param *par) 432 + { 433 + const struct xt_socket_mtinfo3 *info = 434 + (struct xt_socket_mtinfo3 *)par->matchinfo; 435 + 436 + if (info->flags & ~XT_SOCKET_FLAGS_V3) { 437 + pr_info("unknown flags 0x%x\n", 438 + info->flags & ~XT_SOCKET_FLAGS_V3); 439 + return -EINVAL; 440 + } 441 + return 0; 442 + } 443 + 441 444 static struct xt_match socket_mt_reg[] __read_mostly = { 442 445 { 443 446 .name = "socket", ··· 465 442 .name = "socket", 466 443 .revision = 1, 467 444 .family = NFPROTO_IPV4, 468 - .match = socket_mt4_v1_v2, 445 + .match = socket_mt4_v1_v2_v3, 469 446 .checkentry = socket_mt_v1_check, 470 447 .matchsize = sizeof(struct xt_socket_mtinfo1), 471 448 .hooks = (1 << NF_INET_PRE_ROUTING) | ··· 477 454 .name = "socket", 478 455 .revision = 1, 479 456 .family = NFPROTO_IPV6, 480 - .match = socket_mt6_v1_v2, 457 + .match = socket_mt6_v1_v2_v3, 481 458 .checkentry = socket_mt_v1_check, 482 459 .matchsize = sizeof(struct xt_socket_mtinfo1), 483 460 .hooks = (1 << NF_INET_PRE_ROUTING) | ··· 489 466 .name = "socket", 490 467 .revision = 2, 491 468 .family = NFPROTO_IPV4, 492 - .match = socket_mt4_v1_v2, 469 + .match = socket_mt4_v1_v2_v3, 493 470 .checkentry = socket_mt_v2_check, 494 471 .matchsize = sizeof(struct xt_socket_mtinfo1), 495 472 .hooks = (1 << NF_INET_PRE_ROUTING) | ··· 501 478 .name = "socket", 502 479 .revision = 2, 503 480 .family = NFPROTO_IPV6, 504 - .match = socket_mt6_v1_v2, 481 + .match = socket_mt6_v1_v2_v3, 505 482 .checkentry = socket_mt_v2_check, 483 + .matchsize = sizeof(struct xt_socket_mtinfo1), 484 + .hooks = (1 << NF_INET_PRE_ROUTING) | 485 + (1 << NF_INET_LOCAL_IN), 486 + .me = THIS_MODULE, 487 + }, 488 + #endif 489 + { 490 + .name = "socket", 491 + .revision = 3, 492 + .family = NFPROTO_IPV4, 493 + .match = socket_mt4_v1_v2_v3, 494 + .checkentry = socket_mt_v3_check, 495 + .matchsize = sizeof(struct xt_socket_mtinfo1), 496 + .hooks = (1 << NF_INET_PRE_ROUTING) | 497 + (1 << NF_INET_LOCAL_IN), 498 + .me = THIS_MODULE, 499 + }, 500 + #ifdef XT_SOCKET_HAVE_IPV6 501 + { 502 + .name = "socket", 503 + .revision = 3, 504 + .family = NFPROTO_IPV6, 505 + .match = socket_mt6_v1_v2_v3, 506 + .checkentry = socket_mt_v3_check, 506 507 .matchsize = sizeof(struct xt_socket_mtinfo1), 507 508 .hooks = (1 << NF_INET_PRE_ROUTING) | 508 509 (1 << NF_INET_LOCAL_IN),