KOVACS Krisztian | 430bbc7 | 2008-10-15 11:50:34 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Shared library add-on to iptables to add early socket matching support. |
| 3 | * |
| 4 | * Copyright (C) 2007 BalaBit IT Ltd. |
| 5 | */ |
Jan Engelhardt | 4d2a77f | 2010-12-03 22:55:34 +0100 | [diff] [blame] | 6 | #include <stdio.h> |
KOVACS Krisztian | 430bbc7 | 2008-10-15 11:50:34 +0200 | [diff] [blame] | 7 | #include <xtables.h> |
Jan Engelhardt | 4d2a77f | 2010-12-03 22:55:34 +0100 | [diff] [blame] | 8 | #include <linux/netfilter/xt_socket.h> |
KOVACS Krisztian | 430bbc7 | 2008-10-15 11:50:34 +0200 | [diff] [blame] | 9 | |
Jan Engelhardt | 9c5c105 | 2011-02-18 03:22:52 +0100 | [diff] [blame] | 10 | enum { |
| 11 | O_TRANSPARENT = 0, |
Eric Dumazet | 64d4579 | 2013-06-20 05:52:35 -0700 | [diff] [blame] | 12 | O_NOWILDCARD = 1, |
Jan Engelhardt | 9c5c105 | 2011-02-18 03:22:52 +0100 | [diff] [blame] | 13 | }; |
| 14 | |
| 15 | static const struct xt_option_entry socket_mt_opts[] = { |
| 16 | {.name = "transparent", .id = O_TRANSPARENT, .type = XTTYPE_NONE}, |
| 17 | XTOPT_TABLEEND, |
Jan Engelhardt | 4d2a77f | 2010-12-03 22:55:34 +0100 | [diff] [blame] | 18 | }; |
| 19 | |
Eric Dumazet | 64d4579 | 2013-06-20 05:52:35 -0700 | [diff] [blame] | 20 | static const struct xt_option_entry socket_mt_opts_v2[] = { |
| 21 | {.name = "transparent", .id = O_TRANSPARENT, .type = XTTYPE_NONE}, |
| 22 | {.name = "nowildcard", .id = O_NOWILDCARD, .type = XTTYPE_NONE}, |
| 23 | XTOPT_TABLEEND, |
| 24 | }; |
| 25 | |
Jan Engelhardt | 4d2a77f | 2010-12-03 22:55:34 +0100 | [diff] [blame] | 26 | static void socket_mt_help(void) |
| 27 | { |
| 28 | printf( |
| 29 | "socket match options:\n" |
| 30 | " --transparent Ignore non-transparent sockets\n\n"); |
| 31 | } |
| 32 | |
Eric Dumazet | 64d4579 | 2013-06-20 05:52:35 -0700 | [diff] [blame] | 33 | static void socket_mt_help_v2(void) |
| 34 | { |
| 35 | printf( |
| 36 | "socket match options:\n" |
| 37 | " --nowildcard Do not ignore LISTEN sockets bound on INADDR_ANY\n" |
| 38 | " --transparent Ignore non-transparent sockets\n\n"); |
| 39 | } |
| 40 | |
Jan Engelhardt | 9c5c105 | 2011-02-18 03:22:52 +0100 | [diff] [blame] | 41 | static void socket_mt_parse(struct xt_option_call *cb) |
Jan Engelhardt | 4d2a77f | 2010-12-03 22:55:34 +0100 | [diff] [blame] | 42 | { |
Jan Engelhardt | 9c5c105 | 2011-02-18 03:22:52 +0100 | [diff] [blame] | 43 | struct xt_socket_mtinfo1 *info = cb->data; |
Jan Engelhardt | 4d2a77f | 2010-12-03 22:55:34 +0100 | [diff] [blame] | 44 | |
Jan Engelhardt | 9c5c105 | 2011-02-18 03:22:52 +0100 | [diff] [blame] | 45 | xtables_option_parse(cb); |
| 46 | switch (cb->entry->id) { |
| 47 | case O_TRANSPARENT: |
Jan Engelhardt | 4d2a77f | 2010-12-03 22:55:34 +0100 | [diff] [blame] | 48 | info->flags |= XT_SOCKET_TRANSPARENT; |
Jan Engelhardt | 9c5c105 | 2011-02-18 03:22:52 +0100 | [diff] [blame] | 49 | break; |
Jan Engelhardt | 4d2a77f | 2010-12-03 22:55:34 +0100 | [diff] [blame] | 50 | } |
Jan Engelhardt | 4d2a77f | 2010-12-03 22:55:34 +0100 | [diff] [blame] | 51 | } |
| 52 | |
Eric Dumazet | 64d4579 | 2013-06-20 05:52:35 -0700 | [diff] [blame] | 53 | static void socket_mt_parse_v2(struct xt_option_call *cb) |
| 54 | { |
| 55 | struct xt_socket_mtinfo2 *info = cb->data; |
| 56 | |
| 57 | xtables_option_parse(cb); |
| 58 | switch (cb->entry->id) { |
| 59 | case O_TRANSPARENT: |
| 60 | info->flags |= XT_SOCKET_TRANSPARENT; |
| 61 | break; |
| 62 | case O_NOWILDCARD: |
| 63 | info->flags |= XT_SOCKET_NOWILDCARD; |
| 64 | break; |
| 65 | } |
| 66 | } |
| 67 | |
Jan Engelhardt | 4d2a77f | 2010-12-03 22:55:34 +0100 | [diff] [blame] | 68 | static void |
| 69 | socket_mt_save(const void *ip, const struct xt_entry_match *match) |
| 70 | { |
| 71 | const struct xt_socket_mtinfo1 *info = (const void *)match->data; |
| 72 | |
| 73 | if (info->flags & XT_SOCKET_TRANSPARENT) |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 74 | printf(" --transparent"); |
Jan Engelhardt | 4d2a77f | 2010-12-03 22:55:34 +0100 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | static void |
| 78 | socket_mt_print(const void *ip, const struct xt_entry_match *match, |
| 79 | int numeric) |
| 80 | { |
Jan Engelhardt | 7386635 | 2010-12-18 02:04:59 +0100 | [diff] [blame] | 81 | printf(" socket"); |
Jan Engelhardt | 4d2a77f | 2010-12-03 22:55:34 +0100 | [diff] [blame] | 82 | socket_mt_save(ip, match); |
| 83 | } |
| 84 | |
Eric Dumazet | 64d4579 | 2013-06-20 05:52:35 -0700 | [diff] [blame] | 85 | static void |
| 86 | socket_mt_save_v2(const void *ip, const struct xt_entry_match *match) |
| 87 | { |
| 88 | const struct xt_socket_mtinfo2 *info = (const void *)match->data; |
| 89 | |
| 90 | if (info->flags & XT_SOCKET_TRANSPARENT) |
| 91 | printf(" --transparent"); |
| 92 | if (info->flags & XT_SOCKET_NOWILDCARD) |
| 93 | printf(" --nowildcard"); |
| 94 | } |
| 95 | |
| 96 | static void |
| 97 | socket_mt_print_v2(const void *ip, const struct xt_entry_match *match, |
| 98 | int numeric) |
| 99 | { |
| 100 | printf(" socket"); |
| 101 | socket_mt_save_v2(ip, match); |
| 102 | } |
| 103 | |
Jan Engelhardt | 4d2a77f | 2010-12-03 22:55:34 +0100 | [diff] [blame] | 104 | static struct xtables_match socket_mt_reg[] = { |
| 105 | { |
| 106 | .name = "socket", |
| 107 | .revision = 0, |
| 108 | .family = NFPROTO_IPV4, |
| 109 | .version = XTABLES_VERSION, |
| 110 | .size = XT_ALIGN(0), |
| 111 | .userspacesize = XT_ALIGN(0), |
| 112 | }, |
| 113 | { |
| 114 | .name = "socket", |
| 115 | .revision = 1, |
| 116 | .family = NFPROTO_UNSPEC, |
| 117 | .version = XTABLES_VERSION, |
| 118 | .size = XT_ALIGN(sizeof(struct xt_socket_mtinfo1)), |
| 119 | .userspacesize = XT_ALIGN(sizeof(struct xt_socket_mtinfo1)), |
| 120 | .help = socket_mt_help, |
Jan Engelhardt | 4d2a77f | 2010-12-03 22:55:34 +0100 | [diff] [blame] | 121 | .print = socket_mt_print, |
| 122 | .save = socket_mt_save, |
Jan Engelhardt | 9c5c105 | 2011-02-18 03:22:52 +0100 | [diff] [blame] | 123 | .x6_parse = socket_mt_parse, |
| 124 | .x6_options = socket_mt_opts, |
Jan Engelhardt | 4d2a77f | 2010-12-03 22:55:34 +0100 | [diff] [blame] | 125 | }, |
Eric Dumazet | 64d4579 | 2013-06-20 05:52:35 -0700 | [diff] [blame] | 126 | { |
| 127 | .name = "socket", |
| 128 | .revision = 2, |
| 129 | .family = NFPROTO_UNSPEC, |
| 130 | .version = XTABLES_VERSION, |
| 131 | .size = XT_ALIGN(sizeof(struct xt_socket_mtinfo2)), |
| 132 | .userspacesize = XT_ALIGN(sizeof(struct xt_socket_mtinfo2)), |
| 133 | .help = socket_mt_help_v2, |
| 134 | .print = socket_mt_print_v2, |
| 135 | .save = socket_mt_save_v2, |
| 136 | .x6_parse = socket_mt_parse_v2, |
| 137 | .x6_options = socket_mt_opts_v2, |
| 138 | }, |
KOVACS Krisztian | 430bbc7 | 2008-10-15 11:50:34 +0200 | [diff] [blame] | 139 | }; |
| 140 | |
| 141 | void _init(void) |
| 142 | { |
Jan Engelhardt | 4d2a77f | 2010-12-03 22:55:34 +0100 | [diff] [blame] | 143 | xtables_register_matches(socket_mt_reg, ARRAY_SIZE(socket_mt_reg)); |
KOVACS Krisztian | 430bbc7 | 2008-10-15 11:50:34 +0200 | [diff] [blame] | 144 | } |