Please help !! [long]
I write my own module to security data transmition. I don't now what is wrong.
When I type:
iptables -A INPUT -m sec --szyfr klucz -j ACCEPT
iptables write error:
when in parse function
default: return 0; } return 0; " Unknown arg klucz" when in parse function default: return 0; }
return 1;
"Invalid argument"
This is source code:
------------------------------lib_sec.h
#ifndef _IPT_SEC_H
#define _IPT_SEC_H
#define SEC_SZYFR 0x01
#define SEC_DESZYFR 0x02
struct ipt_sec_info
{
char klucz[2048];
int wybor;
u_int8_t flags;
u_int8_t invert;
};#endif /* _IPT_SEC_H */
-------------------------------libipt_sec.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <getopt.h>
#include <netdb.h>
#include <ctype.h>
#include <iptables.h>
#include <linux/netfilter_ipv4/ip_tables.h>
#include "ipt_sec.h"
static void init(struct ipt_entry_match *m, unsigned int *nfcache);
static void help(void);
static int parse(int c, char **argv, int invert, unsigned int *flags, const struct ipt_entry *entry, unsigned int *nfcache,struct ipt_entry_match **match); static void final_check(unsigned int flags);
static void print(const struct ipt_ip *ip, const struct ipt_entry_match *m, int numeric);
static void save(const struct ipt_ip *ip, const struct ipt_entry_match *m);
static struct option opts[] = {
{.name = "szyfr", .has_arg = 1,.flag = 0,.val = '1'},
{.name = "deszyfr",.has_arg = 1,.flag = 0,.val = '2'},
{.name = 0}
};static struct iptables_match sec = {
.name = "sec",
.version = IPTABLES_VERSION,
.size = IPT_ALIGN(sizeof(struct ipt_sec_info)), .userspacesize = IPT_ALIGN(sizeof(struct ipt_sec_info)),
.init = &init,
.help = &help,
.parse = &parse,
.final_check = &final_check,
.print = &print,
.save = &save,
.extra_opts = opts
};
static void help(void)
{
printf ("Security match v%s options:\n"
"--szyfr key Match is cripto\n"
"--deszyfr key Match is decripto\n",
IPTABLES_VERSION);
}
// function read key from file
static void parse_klucz(char *arg, struct ipt_sec_info *sinfo, int wybor)
{
char linia[100];
FILE *fp;
fp = fopen(arg,"r");
if (fp !=NULL)
{
while (fgets(linia,100,fp)!=NULL)
{
strcat(sinfo->klucz,linia);
sinfo->wybor=wybor;
}
fclose(fp);
}
else
{
exit_error(PARAMETER_PROBLEM, "File is not avable");
}
}static int
parse(int c, char **argv, int invert, unsigned int *flags,
const struct ipt_entry *entry, unsigned int *nfcache, struct ipt_entry_match **match)
{
struct ipt_sec_info *sinfo = (struct ipt_sec_info *) (*match)->data;
if (*flags)
exit_error(PARAMETER_PROBLEM, "I don't now what this ?");
switch (c) {
case '1':
if (*flags & SEC_SZYFR)
exit_error(PARAMETER_PROBLEM, "flags %i",*flags);
sinfo->flags = SEC_SZYFR;
check_inverse(optarg,&invert,&optind,0);
parse_klucz(argv[optind-1],sinfo,1);
if (invert) sinfo->invert = 1; // exit_error(OTHER_PROBLEM, " %i",invert);
// *flags |= SEC_SZYFR;
*flags = 1;
break;
case '2':
if (*flags & SEC_DESZYFR)
exit_error(PARAMETER_PROBLEM,"plum");
sinfo->flags = SEC_SZYFR;
check_inverse(argv[optind-1],&invert,&optind,0);
parse_klucz(argv[optind-1],sinfo,2);
if (invert) sinfo->invert = 1; // exit_error(OTHER_PROBLEM, "B %s", klucz.klu);
// *flags |= SEC_DESZYFR;
*flags = 1;
break;
default:
return 0;
}
// exit_error(OTHER_PROBLEM, "test %s",sinfo->klucz);
// *flags |= SEC_SZYFR;
return 0;
}static void final_check(unsigned int flags)
{
if (!flags)
exit_error(PARAMETER_PROBLEM, "Security match must specify --szyfr --deszyfr");
}
static void print(const struct ipt_ip *ip, const struct ipt_entry_match *m,
int numeric)
{
}
static void save(const struct ipt_ip *ip, const struct ipt_entry_match *m) {
}
static void init(struct ipt_entry_match *m, unsigned int *nfcache)
{
*nfcache |= NFC_UNKNOWN;
}void _init(void)
{
register_match(&sec);
}-------------------------------------------------------------ipt_sec.c // kernel module
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/version.h>
#include <linux/skbuff.h>
#include <linux/netfilter_ipv4/ip_tables.h>
#include "ipt_sec.h"
#define DRIVER_AUTHOR "Michal dracek Rupka <[email protected]>"; #define DRIVER_DESC "Security";
MODULE_LICENSE("GPL");MODULE_AUTHOR(DRIVER_AUTHOR);MODULE_DESCRIPTION(DRIVER_DESC);
static int
match(const struct sk_buff *skb,
const struct net_device *in,
const struct net_device *out,
const void *matchinfo,
int offset,
const void *hdr,
u_int16_t datalen,
int *hotdrop)
{
printk(KERN_ALERT "klucz ");
if ((void *) skb != NULL)
{
printk(KERN_ALERT "cos jest\n");
}return 0;
}
static int
checkentry(const char *tablename,
const struct ipt_ip *ip,
void *matchinfo,
unsigned int matchsize,
unsigned int hook_mask)
{
return 0;
}
static struct ipt_match sec_match = {
.name = "sec", .match = &match, .checkentry = &checkentry, .me = THIS_MODULE,
};
static int __init sec_init(void)
{
printk(KERN_ALERT "Zainicjowano modul security ... \n");
return ipt_register_match(&sec_match);
}static void __exit sec_exit(void)
{
ipt_unregister_match(&sec_match);
printk(KERN_ALERT "Usuniecie modulu ...\n");
}module_init(sec_init);module_exit(sec_exit);
PS. My english isn't perfect sorry
Signature · 4 lines
# Michał "dRacek" Rupka # dracek(at)ze94(dot)internetdsl(dot)tpnet(dot)pl # GG 1124108, JID dracek(at)chrome.pl, RLU 314366 # GSM 608-118-007 http://80.53.136.94/dracek ####