libip6t_REJECT.c revision 32b8e61e4e5bd405d9ad07bf9468498dfbb19f9e
1/* Shared library add-on to ip6tables to add customized REJECT support. 2 * 3 * (C) 2000 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu> 4 * 5 * ported to IPv6 by Harald Welte <laforge@gnumonks.org> 6 * 7 */ 8#include <stdbool.h> 9#include <stdio.h> 10#include <string.h> 11#include <stdlib.h> 12#include <getopt.h> 13#include <xtables.h> 14#include <linux/netfilter_ipv6/ip6t_REJECT.h> 15 16struct reject_names { 17 const char *name; 18 const char *alias; 19 enum ip6t_reject_with with; 20 const char *desc; 21}; 22 23static const struct reject_names reject_table[] = { 24 {"icmp6-no-route", "no-route", 25 IP6T_ICMP6_NO_ROUTE, "ICMPv6 no route"}, 26 {"icmp6-adm-prohibited", "adm-prohibited", 27 IP6T_ICMP6_ADM_PROHIBITED, "ICMPv6 administratively prohibited"}, 28#if 0 29 {"icmp6-not-neighbor", "not-neighbor"}, 30 IP6T_ICMP6_NOT_NEIGHBOR, "ICMPv6 not a neighbor"}, 31#endif 32 {"icmp6-addr-unreachable", "addr-unreach", 33 IP6T_ICMP6_ADDR_UNREACH, "ICMPv6 address unreachable"}, 34 {"icmp6-port-unreachable", "port-unreach", 35 IP6T_ICMP6_PORT_UNREACH, "ICMPv6 port unreachable"}, 36 {"tcp-reset", "tcp-reset", 37 IP6T_TCP_RESET, "TCP RST packet"} 38}; 39 40static void 41print_reject_types(void) 42{ 43 unsigned int i; 44 45 printf("Valid reject types:\n"); 46 47 for (i = 0; i < ARRAY_SIZE(reject_table); ++i) { 48 printf(" %-25s\t%s\n", reject_table[i].name, reject_table[i].desc); 49 printf(" %-25s\talias\n", reject_table[i].alias); 50 } 51 printf("\n"); 52} 53 54static void REJECT_help(void) 55{ 56 printf( 57"REJECT target options:\n" 58"--reject-with type drop input packet and send back\n" 59" a reply packet according to type:\n"); 60 61 print_reject_types(); 62} 63 64static const struct option REJECT_opts[] = { 65 {.name = "reject-with", .has_arg = true, .val = '1'}, 66 XT_GETOPT_TABLEEND, 67}; 68 69static void REJECT_init(struct xt_entry_target *t) 70{ 71 struct ip6t_reject_info *reject = (struct ip6t_reject_info *)t->data; 72 73 /* default */ 74 reject->with = IP6T_ICMP6_PORT_UNREACH; 75 76} 77 78static int REJECT_parse(int c, char **argv, int invert, unsigned int *flags, 79 const void *entry, struct xt_entry_target **target) 80{ 81 struct ip6t_reject_info *reject = 82 (struct ip6t_reject_info *)(*target)->data; 83 unsigned int i; 84 85 switch(c) { 86 case '1': 87 if (xtables_check_inverse(optarg, &invert, NULL, 0, argv)) 88 xtables_error(PARAMETER_PROBLEM, 89 "Unexpected `!' after --reject-with"); 90 for (i = 0; i < ARRAY_SIZE(reject_table); ++i) 91 if ((strncasecmp(reject_table[i].name, optarg, strlen(optarg)) == 0) 92 || (strncasecmp(reject_table[i].alias, optarg, strlen(optarg)) == 0)) { 93 reject->with = reject_table[i].with; 94 return 1; 95 } 96 xtables_error(PARAMETER_PROBLEM, "unknown reject type \"%s\"", optarg); 97 default: 98 /* Fall through */ 99 break; 100 } 101 return 0; 102} 103 104static void REJECT_print(const void *ip, const struct xt_entry_target *target, 105 int numeric) 106{ 107 const struct ip6t_reject_info *reject 108 = (const struct ip6t_reject_info *)target->data; 109 unsigned int i; 110 111 for (i = 0; i < ARRAY_SIZE(reject_table); ++i) 112 if (reject_table[i].with == reject->with) 113 break; 114 printf("reject-with %s ", reject_table[i].name); 115} 116 117static void REJECT_save(const void *ip, const struct xt_entry_target *target) 118{ 119 const struct ip6t_reject_info *reject 120 = (const struct ip6t_reject_info *)target->data; 121 unsigned int i; 122 123 for (i = 0; i < ARRAY_SIZE(reject_table); ++i) 124 if (reject_table[i].with == reject->with) 125 break; 126 127 printf("--reject-with %s ", reject_table[i].name); 128} 129 130static struct xtables_target reject_tg6_reg = { 131 .name = "REJECT", 132 .version = XTABLES_VERSION, 133 .family = NFPROTO_IPV6, 134 .size = XT_ALIGN(sizeof(struct ip6t_reject_info)), 135 .userspacesize = XT_ALIGN(sizeof(struct ip6t_reject_info)), 136 .help = REJECT_help, 137 .init = REJECT_init, 138 .parse = REJECT_parse, 139 .print = REJECT_print, 140 .save = REJECT_save, 141 .extra_opts = REJECT_opts, 142}; 143 144void _init(void) 145{ 146 xtables_register_target(&reject_tg6_reg); 147} 148