1 /* Shared library add-on to iptables to add IP range matching support. */
9 #include <linux/netfilter_ipv4/ipt_iprange.h>
11 /* Function which prints out usage message. */
16 "iprange match v%s options:\n"
17 "[!] --src-range ip-ip Match source IP in the specified range\n"
18 "[!] --dst-range ip-ip Match destination IP in the specified range\n"
23 static struct option opts[] = {
24 { "src-range", 1, 0, '1' },
25 { "dst-range", 1, 0, '2' },
29 /* Initialize the match. */
31 init(struct ipt_entry_match *m, unsigned int *nfcache)
33 /* Can't cache this. */
34 *nfcache |= NFC_UNKNOWN;
38 parse_iprange(char *arg, struct ipt_iprange *range)
43 dash = strchr(arg, '-');
47 ip = dotted_to_addr(arg);
49 exit_error(PARAMETER_PROBLEM, "iprange match: Bad IP address `%s'\n",
51 range->min_ip = ip->s_addr;
54 ip = dotted_to_addr(dash+1);
56 exit_error(PARAMETER_PROBLEM, "iprange match: Bad IP address `%s'\n",
58 range->max_ip = ip->s_addr;
60 range->max_ip = range->min_ip;
63 /* Function which parses command options; returns true if it
66 parse(int c, char **argv, int invert, unsigned int *flags,
67 const struct ipt_entry *entry,
68 unsigned int *nfcache,
69 struct ipt_entry_match **match)
71 struct ipt_iprange_info *info = (struct ipt_iprange_info *)(*match)->data;
75 if (*flags & IPRANGE_SRC)
76 exit_error(PARAMETER_PROBLEM,
77 "iprange match: Only use --src-range ONCE!");
78 *flags |= IPRANGE_SRC;
80 info->flags |= IPRANGE_SRC;
81 check_inverse(optarg, &invert, &optind, 0);
83 info->flags |= IPRANGE_SRC_INV;
86 parse_iprange(optarg, &info->src);
91 if (*flags & IPRANGE_DST)
92 exit_error(PARAMETER_PROBLEM,
93 "iprange match: Only use --dst-range ONCE!");
94 *flags |= IPRANGE_DST;
96 info->flags |= IPRANGE_DST;
97 check_inverse(optarg, &invert, &optind, 0);
99 info->flags |= IPRANGE_DST_INV;
101 parse_iprange(optarg, &info->dst);
111 /* Final check; must have specified --src-range or --dst-range. */
113 final_check(unsigned int flags)
116 exit_error(PARAMETER_PROBLEM,
117 "iprange match: You must specify `--src-range' or `--dst-range'");
121 print_iprange(const struct ipt_iprange *range)
123 const unsigned char *byte_min, *byte_max;
125 byte_min = (const unsigned char *) &(range->min_ip);
126 byte_max = (const unsigned char *) &(range->max_ip);
127 printf("%d.%d.%d.%d-%d.%d.%d.%d ",
128 byte_min[0], byte_min[1], byte_min[2], byte_min[3],
129 byte_max[0], byte_max[1], byte_max[2], byte_max[3]);
132 /* Prints out the info. */
134 print(const struct ipt_ip *ip,
135 const struct ipt_entry_match *match,
138 struct ipt_iprange_info *info = (struct ipt_iprange_info *)match->data;
140 if (info->flags & IPRANGE_SRC) {
141 printf("source IP range ");
142 if (info->flags & IPRANGE_SRC_INV)
144 print_iprange(&info->src);
146 if (info->flags & IPRANGE_DST) {
147 printf("destination IP range ");
148 if (info->flags & IPRANGE_DST_INV)
150 print_iprange(&info->dst);
154 /* Saves the union ipt_info in parsable form to stdout. */
156 save(const struct ipt_ip *ip, const struct ipt_entry_match *match)
158 struct ipt_iprange_info *info = (struct ipt_iprange_info *)match->data;
160 if (info->flags & IPRANGE_SRC) {
161 if (info->flags & IPRANGE_SRC_INV)
163 printf("--src-range ");
164 print_iprange(&info->src);
165 if (info->flags & IPRANGE_DST)
168 if (info->flags & IPRANGE_DST) {
169 if (info->flags & IPRANGE_DST_INV)
171 printf("--dst-range ");
172 print_iprange(&info->dst);
177 struct iptables_match iprange
181 IPT_ALIGN(sizeof(struct ipt_iprange_info)),
182 IPT_ALIGN(sizeof(struct ipt_iprange_info)),
194 register_match(&iprange);