1 /* Copyright 2000, 2001, 2002 Joakim Axelsson (gozem@linux.nu)
2 * Patrick Schaaf (bof@bof.de)
3 * Martin Josefsson (gandalf@wlug.westbo.se)
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include <sys/socket.h>
25 #include <netinet/in.h>
26 #include <arpa/inet.h>
27 #include <asm/bitops.h>
28 #include <linux/if_ether.h>
30 #include <linux/netfilter_ipv4/ip_set_macipmap.h>
35 #define OPT_CREATE_FROM 0x01U
36 #define OPT_CREATE_TO 0x02U
37 #define OPT_CREATE_NETWORK 0x04U
38 #define OPT_CREATE_MATCHUNSET 0x08U
40 #define OPT_ADDDEL_IP 0x01U
41 #define OPT_ADDDEL_MAC 0x02U
43 /* Initialize the create. */
44 void create_init(void *data)
50 /* Function which parses command options; returns true if it ate an option */
51 int create_parse(int c, char *argv[], void *data, unsigned *flags)
53 struct ip_set_req_macipmap_create *mydata =
54 (struct ip_set_req_macipmap_create *) data;
60 parse_ip(optarg, &mydata->from);
62 *flags |= OPT_CREATE_FROM;
64 DP("--from %x (%s)", mydata->from,
65 ip_tostring_numeric(mydata->from));
70 parse_ip(optarg, &mydata->to);
72 *flags |= OPT_CREATE_TO;
74 DP("--to %x (%s)", mydata->to,
75 ip_tostring_numeric(mydata->to));
80 parse_ipandmask(optarg, &mydata->from, &mydata->to);
82 /* Make to the last of from + mask */
83 mydata->to = mydata->from | (~mydata->to);
85 *flags |= OPT_CREATE_NETWORK;
87 DP("--network from %x (%s)",
88 mydata->from, ip_tostring_numeric(mydata->from));
89 DP("--network to %x (%s)",
90 mydata->to, ip_tostring_numeric(mydata->to));
95 mydata->flags |= IPSET_MACIP_MATCHUNSET;
97 *flags |= OPT_CREATE_MATCHUNSET;
110 /* Final check; exit if not ok. */
111 void create_final(void *data, unsigned int flags)
113 struct ip_set_req_macipmap_create *mydata =
114 (struct ip_set_req_macipmap_create *) data;
117 exit_error(PARAMETER_PROBLEM,
118 "Need to specify --from and --to, or --network\n");
120 if (flags & OPT_CREATE_NETWORK) {
122 if ((flags & OPT_CREATE_FROM) || (flags & OPT_CREATE_TO))
123 exit_error(PARAMETER_PROBLEM,
124 "Can't specify --from or --to with --network\n");
127 if ((flags & OPT_CREATE_FROM) == 0
128 || (flags & OPT_CREATE_TO) == 0)
129 exit_error(PARAMETER_PROBLEM,
130 "Need to specify both --from and --to\n");
134 DP("from : %x to: %x diff: %d match unset: %d", mydata->from,
135 mydata->to, mydata->to - mydata->from,
136 flags & OPT_CREATE_MATCHUNSET);
138 if (mydata->from > mydata->to)
139 exit_error(PARAMETER_PROBLEM,
140 "From can't be lower than to.\n");
142 if (mydata->to - mydata->from > MAX_RANGE)
143 exit_error(PARAMETER_PROBLEM,
144 "Range too large. Max is %d IPs in range\n",
148 /* Create commandline options */
149 static struct option create_opts[] = {
152 {"network", 1, 0, '3'},
153 {"matchunset", 0, 0, '4'},
157 static void parse_mac(const char *mac, unsigned char *ethernet)
161 if (strlen(mac) != ETH_ALEN * 3 - 1)
162 exit_error(PARAMETER_PROBLEM, "Bad mac address `%s'", mac);
164 for (i = 0; i < ETH_ALEN; i++) {
168 number = strtol(mac + i * 3, &end, 16);
170 if (end == mac + i * 3 + 2 && number >= 0 && number <= 255)
171 ethernet[i] = number;
173 exit_error(PARAMETER_PROBLEM,
174 "Bad mac address `%s'", mac);
178 /* Add, del, test parser */
179 ip_set_ip_t adt_parser(unsigned cmd, const char *optarg, void *data)
181 struct ip_set_req_macipmap *mydata =
182 (struct ip_set_req_macipmap *) data;
183 char *saved = ipset_strdup(optarg);
184 char *ptr, *tmp = saved;
186 DP("macipmap: %p %p", optarg, data);
188 ptr = strsep(&tmp, "%");
189 parse_ip(ptr, &mydata->ip);
192 parse_mac(tmp, mydata->ethernet);
194 memset(mydata->ethernet, 0, ETH_ALEN);
204 void initheader(struct set *set, const void *data)
206 struct ip_set_req_macipmap_create *header =
207 (struct ip_set_req_macipmap_create *) data;
208 struct ip_set_macipmap *map =
209 (struct ip_set_macipmap *) set->settype->header;
211 memset(map, 0, sizeof(struct ip_set_macipmap));
212 map->first_ip = header->from;
213 map->last_ip = header->to;
214 map->flags = header->flags;
217 void printheader(struct set *set, unsigned options)
219 struct ip_set_macipmap *mysetdata =
220 (struct ip_set_macipmap *) set->settype->header;
222 printf(" from: %s", ip_tostring(mysetdata->first_ip, options));
223 printf(" to: %s", ip_tostring(mysetdata->last_ip, options));
225 if (mysetdata->flags & IPSET_MACIP_MATCHUNSET)
226 printf(" matchunset");
230 static void print_mac(unsigned char macaddress[ETH_ALEN])
234 printf("%02X", macaddress[0]);
235 for (i = 1; i < ETH_ALEN; i++)
236 printf(":%02X", macaddress[i]);
239 void printips_sorted(struct set *set, void *data, size_t len, unsigned options)
241 struct ip_set_macipmap *mysetdata =
242 (struct ip_set_macipmap *) set->settype->header;
243 struct ip_set_macip *table =
244 (struct ip_set_macip *) data;
245 u_int32_t addr = mysetdata->first_ip;
247 while (addr <= mysetdata->last_ip) {
248 if (test_bit(IPSET_MACIP_ISSET,
249 (void *)&table[addr - mysetdata->first_ip].flags)) {
250 printf("%s%%", ip_tostring(addr, options));
251 print_mac(table[addr - mysetdata->first_ip].
259 void saveheader(struct set *set, unsigned options)
261 struct ip_set_macipmap *mysetdata =
262 (struct ip_set_macipmap *) set->settype->header;
264 printf("-N %s %s --from %s",
265 set->name, set->settype->typename,
266 ip_tostring(mysetdata->first_ip, options));
267 printf(" --to %s", ip_tostring(mysetdata->last_ip, options));
269 if (mysetdata->flags & IPSET_MACIP_MATCHUNSET)
270 printf(" --matchunset");
274 void saveips(struct set *set, void *data, size_t len, unsigned options)
276 struct ip_set_macipmap *mysetdata =
277 (struct ip_set_macipmap *) set->settype->header;
278 struct ip_set_macip *table =
279 (struct ip_set_macip *) data;
280 u_int32_t addr = mysetdata->first_ip;
282 while (addr <= mysetdata->last_ip) {
283 if (test_bit(IPSET_MACIP_ISSET,
284 (void *)&table[addr - mysetdata->first_ip].flags)) {
286 set->name, ip_tostring(addr, options));
287 print_mac(table[addr - mysetdata->first_ip].
298 ("-N set macipmap --from IP --to IP [--matchunset]\n"
299 "-N set macipmap --network IP/mask [--matchunset]\n"
302 "-T set IP[%%MAC]\n");
305 static struct settype settype_macipmap = {
306 .typename = SETTYPE_NAME,
307 .protocol_version = IP_SET_PROTOCOL_VERSION,
310 .create_size = sizeof(struct ip_set_req_macipmap_create),
311 .create_init = &create_init,
312 .create_parse = &create_parse,
313 .create_final = &create_final,
314 .create_opts = create_opts,
317 .adt_size = sizeof(struct ip_set_req_macipmap),
318 .adt_parser = &adt_parser,
321 .header_size = sizeof(struct ip_set_macipmap),
322 .initheader = &initheader,
323 .printheader = &printheader,
324 .printips = &printips_sorted, /* We only have sorted version */
325 .printips_sorted = &printips_sorted,
326 .saveheader = &saveheader,
330 .bindip_tostring = &binding_ip_tostring,
331 .bindip_parse = &parse_ip,
338 settype_register(&settype_macipmap);