From 39534d167d8e1c88fc9a52b837a6f72857314842 Mon Sep 17 00:00:00 2001 From: laforge Date: Fri, 5 Sep 2003 12:54:06 +0000 Subject: IPv6 'port' of ROUTE target (Cedric de Launois) --- extensions/.ROUTE-test6 | 2 + extensions/libip6t_ROUTE.c | 209 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 211 insertions(+) create mode 100755 extensions/.ROUTE-test6 create mode 100644 extensions/libip6t_ROUTE.c (limited to 'extensions') diff --git a/extensions/.ROUTE-test6 b/extensions/.ROUTE-test6 new file mode 100755 index 0000000..7994970 --- /dev/null +++ b/extensions/.ROUTE-test6 @@ -0,0 +1,2 @@ +#! /bin/sh +[ -f $KERNEL_DIR/include/linux/netfilter_ipv6/ip6t_ROUTE.h ] && echo ROUTE diff --git a/extensions/libip6t_ROUTE.c b/extensions/libip6t_ROUTE.c new file mode 100644 index 0000000..71dada8 --- /dev/null +++ b/extensions/libip6t_ROUTE.c @@ -0,0 +1,209 @@ +/* Shared library add-on to iptables to add ROUTE v6 target support. + * Author : Cedric de Launois, + * v 1.0 2003/06/24 + */ + +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include + +/* Function which prints out usage message. */ +static void +help(void) +{ + printf( +"ROUTE target v%s options:\n" +" --oif \tifname \t\tRoute the packet through `ifname' network interface\n" +" --gw \tip \t\tRoute the packet via this gateway\n" +" --continue\t \t\tRoute the packet and continue traversing the rules.\n" +"\n", +"1.0"); +} + +static struct option opts[] = { + { "oif", 1, 0, '1' }, + { "iif", 1, 0, '2' }, + { "gw", 1, 0, '3' }, + { "continue", 0, 0, '4' }, + { 0 } +}; + +/* Initialize the target. */ +static void +init(struct ip6t_entry_target *t, unsigned int *nfcache) +{ + struct ip6t_route_target_info *route_info = + (struct ip6t_route_target_info*)t->data; + + route_info->oif[0] = '\0'; + route_info->iif[0] = '\0'; + route_info->gw[0] = 0; + route_info->gw[1] = 0; + route_info->gw[2] = 0; + route_info->gw[3] = 0; + route_info->flags = 0; +} + + +#define IP6T_ROUTE_OPT_OIF 0x01 +#define IP6T_ROUTE_OPT_IIF 0x02 +#define IP6T_ROUTE_OPT_GW 0x04 +#define IP6T_ROUTE_OPT_CONTINUE 0x08 + +/* Function which parses command options; returns true if it + ate an option */ +static int +parse(int c, char **argv, int invert, unsigned int *flags, + const struct ip6t_entry *entry, + struct ip6t_entry_target **target) +{ + struct ip6t_route_target_info *route_info = + (struct ip6t_route_target_info*)(*target)->data; + + switch (c) { + case '1': + if (*flags & IP6T_ROUTE_OPT_OIF) + exit_error(PARAMETER_PROBLEM, + "Can't specify --oif twice"); + + if (check_inverse(optarg, &invert, NULL, 0)) + exit_error(PARAMETER_PROBLEM, + "Unexpected `!' after --oif"); + + if (strlen(optarg) > sizeof(route_info->oif) - 1) + exit_error(PARAMETER_PROBLEM, + "Maximum interface name length %u", + sizeof(route_info->oif) - 1); + + strcpy(route_info->oif, optarg); + *flags |= IP6T_ROUTE_OPT_OIF; + break; + + case '2': + exit_error(PARAMETER_PROBLEM, + "--iif option not implemented"); + break; + + case '3': + if (*flags & IP6T_ROUTE_OPT_GW) + exit_error(PARAMETER_PROBLEM, + "Can't specify --gw twice"); + + if (check_inverse(optarg, &invert, NULL, 0)) + exit_error(PARAMETER_PROBLEM, + "Unexpected `!' after --gw"); + + if (!inet_pton(AF_INET6, optarg, (struct in6_addr*)&route_info->gw)) { + exit_error(PARAMETER_PROBLEM, + "Invalid IPv6 address %s", + optarg); + } + + *flags |= IP6T_ROUTE_OPT_GW; + break; + + case '4': + if (*flags & IP6T_ROUTE_OPT_CONTINUE) + exit_error(PARAMETER_PROBLEM, + "Can't specify --continue twice"); + + route_info->flags |= IP6T_ROUTE_CONTINUE; + *flags |= IP6T_ROUTE_OPT_CONTINUE; + + break; + + default: + return 0; + } + + return 1; +} + + +static void +final_check(unsigned int flags) +{ + if (!flags) + exit_error(PARAMETER_PROBLEM, + "ROUTE target: oif or gw option required"); +} + + +/* Prints out the targinfo. */ +static void +print(const struct ip6t_ip6 *ip, + const struct ip6t_entry_target *target, + int numeric) +{ + const struct ip6t_route_target_info *route_info + = (const struct ip6t_route_target_info *)target->data; + + printf("ROUTE "); + + if (route_info->oif[0]) + printf("oif:%s ", route_info->oif); + + if (route_info->gw[0] + || route_info->gw[1] + || route_info->gw[2] + || route_info->gw[3]) { + char address[INET6_ADDRSTRLEN]; + printf("gw:%s ", inet_ntop(AF_INET6, route_info->gw, address, INET6_ADDRSTRLEN)); + } + + if (route_info->flags & IP6T_ROUTE_CONTINUE) + printf("continue"); + +} + + +static void save(const struct ip6t_ip6 *ip, + const struct ip6t_entry_target *target) +{ + const struct ip6t_route_target_info *route_info + = (const struct ip6t_route_target_info *)target->data; + + if (route_info->oif[0]) + printf("--oif %s ", route_info->oif); + + if (route_info->gw[0] + || route_info->gw[1] + || route_info->gw[2] + || route_info->gw[3]) { + char address[INET6_ADDRSTRLEN]; + printf("--gw %s ", inet_ntop(AF_INET6, route_info->gw, address, INET6_ADDRSTRLEN)); + } + + if (route_info->flags & IP6T_ROUTE_CONTINUE) + printf("--continue "); +} + + +static +struct ip6tables_target route += { NULL, + "ROUTE", + IPTABLES_VERSION, + IP6T_ALIGN(sizeof(struct ip6t_route_target_info)), + IP6T_ALIGN(sizeof(struct ip6t_route_target_info)), + &help, + &init, + &parse, + &final_check, + &print, + &save, + opts +}; + +void _init(void) +{ + register_target6(&route); +} -- cgit v1.2.3