netfilter: nf_nat_snmp_basic: use ASN.1 decoder
Subsystems (2)
v3 cover netfilter: nf_nat_snmp_basic: use ASN.1 decoder
The goal of this patch set are to use the ASN.1 decoder library to parse SNMP ASN.1 payload.
This patch set are same as previous patch
"[PATCH V2]netfilter: nf_nat_snmp_basic: use asn1 decoder library".
My test environment are below.
#Network
Client <-------------> Netfilter FW <-------------> SNMP Server
192.168.3.2 192.168.3.1 192.168.4.1 192.168.4.2
#FW commands
iptables -t raw -I PREROUTING -p udp -m multiport --dports 161,162 \ -j CT --helper snmp
echo 'file nf_nat_snmp_basic.c +p' > \
/sys/kernel/debug/dynamic_debug/control
#SNMP Server commands
sudo ip r a 192.168.3.2 via 192.168.4.1 dev enp2s0
#Client commands
sudo ip r a 192.168.4.2 via 192.168.3.1 dev enp3s0
To test basic snmp test, I used snmpwalk command because it is easy to use
snmpwalk -v <1 or 2c> -c public <ip address> OID
example)
snmpwalk -v 2c -c public 192.168.4.2 .1.3.6.1.2.1.4.21
so that we can see this message from dmesg.
"snmp_helper: 192.168.3.2 to 192.168.4.1"
And, to test snmp trap test, I used snmptrap command.
snmptrap -v 1 -c public 192.168.3.2 .1 192.168.4.2 0 0 0 .1 a \ 192.168.4.2
snmptrap -v 2c -c public 192.168.3.2 .1 .1 .1 a 192.168.4.2
SNMPv1 trap includes two ip address in payload. so we can see below message twice.
"snmp_helper: 192.168.4.2 to 192.168.3.1"
If you want to see asn1 decoder debug message, please use below command
echo 'file asn1_decoder.c +p' > /sys/kernel/debug/dynamic_debug/control
V3 :
- be separated by previous patch.
V2 :
- Add missing nf_nat_snmp_basic.asn1 file
V1 :
- Initial patch
Taehee Yoo (5):
netfilter: nf_nat_snmp_basic: remove useless comment
netfilter: nf_nat_snmp_basic: remove debug parameter
netfilter: nf_nat_snmp_basic: replace ctinfo with dir.
netfilter: nf_nat_snmp_basic: use nf_ct_helper_log
netfilter: nf_nat_snmp_basic: use asn1 decoder library
net/ipv4/netfilter/Kconfig | 1 + net/ipv4/netfilter/Makefile | 5 +- net/ipv4/netfilter/nf_nat_snmp_basic.asn1 | 177 +++++ net/ipv4/netfilter/nf_nat_snmp_basic.c | 1175 ++--------------------------- 4 files changed, 245 insertions(+), 1113 deletions(-) create mode 100644 net/ipv4/netfilter/nf_nat_snmp_basic.asn1
Signature · 1 line
2.9.3
PATCH v3 1/5 netfilter: nf_nat_snmp_basic: remove useless comment
Remove comments that do not let us know important information.
---
V3 :
- be separated by previous patch.
V2 :
- Add missing nf_nat_snmp_basic.asn1 file
V1 :
- Initial patch
net/ipv4/netfilter/nf_nat_snmp_basic.c | 30 ------------------------------ 1 file changed, 30 deletions(-)
net/ipv4/netfilter/nf_nat_snmp_basic.c +0 -30
@@ -81,12 +81,6 @@ struct oct1_map }; -/***************************************************************************** -* -* Basic ASN.1 decoding routines (gxsnmp author Dirk Wisse) -* -*****************************************************************************/ -/* Class */ #define ASN1_UNI 0/* Universal */ #define ASN1_APL 1/* Application */
@@ -491,12 +485,6 @@ static unsigned char asn1_oid_decode(struct asn1_ctx *ctx,return 1 ; } -/***************************************************************************** -* -* SNMP decoding routines (gxsnmp author Dirk Wisse) -* -*****************************************************************************/ -/* SNMP Versions */ #define SNMP_V1 0#define SNMP_V2C 1
@@ -992,12 +980,6 @@ snmp_trap_decode(struct asn1_ctx *ctx, struct snmp_v1_trap *trap,return 0 ; } -/***************************************************************************** -* -* Misc. routines -* -*****************************************************************************/ -/* * Parse and mangle SNMP message according to mapping. * (And this is the fucking 'basic' method).
@@ -1157,12 +1139,6 @@ static int snmp_parse_mangle(unsigned char *msg,return 1 ; } -/***************************************************************************** -* -* NAT routines. -* -*****************************************************************************/ -/* * SNMP translation routine. */
@@ -1259,12 +1235,6 @@ static struct nf_conntrack_helper snmp_trap_helper __read_mostly = { .tuple.dst.protonum = IPPROTO_UDP, }; -/***************************************************************************** -* -* Module stuff. -* -*****************************************************************************/ -static int __init nf_nat_snmp_basic_init (void ) {BUG_ON (nf_nat_snmp_hook !=NULL );
Signature · 1 line
2.9.3
PATCH v3 2/5 netfilter: nf_nat_snmp_basic: remove debug parameter
In reply to Taehee YooTo see debug message of nf_nat_snmp_basic, we should set debug value when we insert this module. but it is inconvenient and only using of the dynamic debugging is enough to debug.
This patch just removes debug code. then in the next patch, debugging code will be added.
---
V3 :
- be separated by previous patch.
V2 :
- Add missing nf_nat_snmp_basic.asn1 file
V1 :
- Initial patch
net/ipv4/netfilter/nf_nat_snmp_basic.c | 62 ---------------------------------- 1 file changed, 62 deletions(-)
net/ipv4/netfilter/nf_nat_snmp_basic.c +0 -62
@@ -66,7 +66,6 @@ MODULE_ALIAS("ip_nat_snmp_basic");#define SNMP_TRAP_PORT 162#define NOCT1 (n) (*(u8 *)(n)) -static int debug;static DEFINE_SPINLOCK (snmp_lock);/*
@@ -888,23 +887,13 @@ static inline void mangle_address(unsigned char *begin, __sum16 *check) {if (map->from ==NOCT1 (addr)) { -u_int32_t old; - -if (debug) -memcpy (&old, addr,sizeof (old)); - *addr = map->to;/* Update UDP checksum if being used */ if (*check) {fast_csum (check, &map->from, &map->to, addr - begin); - } - -if (debug) -printk (KERN_DEBUG "bsalg: mapped %pI4 to %pI4\n" , - &old, addr); } }
@@ -995,10 +984,6 @@ static int snmp_parse_mangle(unsigned char *msg,struct asn1_octstr comm;struct snmp_object *obj; -if (debug >1 ) -print_hex_dump (KERN_DEBUG ,"" ,DUMP_PREFIX_NONE ,16 ,1 , - msg, len,0 ); -asn1_open (&ctx, msg, len);/*
@@ -1018,8 +1003,6 @@ static int snmp_parse_mangle(unsigned char *msg,return 0 ;if (!asn1_uint_decode (&ctx, end, &vers))return 0 ; -if (debug >1 ) -pr_debug ("bsalg: snmp version: %u\n" , vers +1 );if (vers >1 )return 1 ;
@@ -1032,14 +1015,6 @@ static int snmp_parse_mangle(unsigned char *msg,return 0 ;if (!asn1_octets_decode (&ctx, end, &comm.data, &comm.len))return 0 ; -if (debug >1 ) { -unsigned int i; - -pr_debug ("bsalg: community: " ); -for (i =0 ; i < comm.len; i++) -pr_cont ("%c" , comm.data[i]); -pr_cont ("\n" ); - }kfree (comm.data);/*
@@ -1049,23 +1024,6 @@ static int snmp_parse_mangle(unsigned char *msg,return 0 ;if (cls !=ASN1_CTX || con !=ASN1_CON )return 0 ; -if (debug >1 ) { -static const unsigned char *const pdus[] = { - [SNMP_PDU_GET ] ="get" , - [SNMP_PDU_NEXT ] ="get-next" , - [SNMP_PDU_RESPONSE ] ="response" , - [SNMP_PDU_SET ] ="set" , - [SNMP_PDU_TRAP1 ] ="trapv1" , - [SNMP_PDU_BULK ] ="bulk" , - [SNMP_PDU_INFORM ] ="inform" , - [SNMP_PDU_TRAP2 ] ="trapv2" - }; - -if (pdutype >SNMP_PDU_TRAP2 ) -pr_debug ("bsalg: bad pdu type %u\n" , pdutype); -else -pr_debug ("bsalg: pdu: %s\n" , pdus[pdutype]); - }if (pdutype !=SNMP_PDU_RESPONSE && pdutype !=SNMP_PDU_TRAP1 && pdutype !=SNMP_PDU_TRAP2 )return 1 ;
@@ -1088,11 +1046,6 @@ static int snmp_parse_mangle(unsigned char *msg,if (!snmp_request_decode (&ctx, &req))return 0 ; - -if (debug >1 ) -pr_debug ("bsalg: request: id=0x%lx error_status=%u " -"error_index=%u\n" , req.id, req.error_status, - req.error_index); }/*
@@ -1105,8 +1058,6 @@ static int snmp_parse_mangle(unsigned char *msg,return 0 ;while (!asn1_eoc_decode (&ctx, eoc)) { -unsigned int i; -if (!snmp_object_decode (&ctx, &obj)) {if (obj) {kfree (obj->id);
@@ -1115,17 +1066,6 @@ static int snmp_parse_mangle(unsigned char *msg,return 0 ; } -if (debug >1 ) { -pr_debug ("bsalg: object: " ); -for (i =0 ; i < obj->id_len; i++) { -if (i >0 ) -pr_cont ("." ); -pr_cont ("%lu" , obj->id[i]); - } -pr_cont (": type=%u\n" , obj->type); - - } -if (obj->type ==SNMP_IPADDR )mangle_address (ctx.begin, ctx.pointer -4 , map, check);
@@ -1252,5 +1192,3 @@ static void __exit nf_nat_snmp_basic_fini(void)module_init (nf_nat_snmp_basic_init);module_exit (nf_nat_snmp_basic_fini); - -module_param (debug, int,0600 );
Signature · 1 line
2.9.3
PATCH v3 3/5 netfilter: nf_nat_snmp_basic: replace ctinfo with dir.
In reply to Taehee YooThe snmp_translate() receives ctinfo data to get dir value only. because of caller already has dir value, we just replace ctinfo with dir.
---
V3 :
- be separated by previous patch.
V2 :
- Add missing nf_nat_snmp_basic.asn1 file
V1 :
- Initial patch
net/ipv4/netfilter/nf_nat_snmp_basic.c | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-)
net/ipv4/netfilter/nf_nat_snmp_basic.c +2 -5
@@ -1082,15 +1082,12 @@ static int snmp_parse_mangle(unsigned char *msg,/* * SNMP translation routine. */ -static int snmp_translate (struct nf_conn *ct, -enum ip_conntrack_info ctinfo, -struct sk_buff *skb) +static int snmp_translate (struct nf_conn *ct,int dir,struct sk_buff *skb) {struct iphdr *iph =ip_hdr (skb);struct udphdr *udph = (struct udphdr *)((__be32 *)iph + iph->ihl);u_int16_t udplen =ntohs (udph->len);u_int16_t paylen = udplen -sizeof (struct udphdr ); -int dir =CTINFO2DIR (ctinfo);struct oct1_map map;/*
@@ -1155,7 +1152,7 @@ static int help(struct sk_buff *skb, unsigned int protoff,return NF_DROP ;spin_lock_bh (&snmp_lock); - ret =snmp_translate (ct, ctinfo, skb); + ret =snmp_translate (ct, dir, skb);spin_unlock_bh (&snmp_lock);return ret; }
Signature · 1 line
2.9.3
PATCH v3 4/5 netfilter: nf_nat_snmp_basic: use nf_ct_helper_log
In reply to Taehee YooUse nf_ct_helper_log to write log message.
---
V3 :
- be separated by previous patch.
V2 :
- Add missing nf_nat_snmp_basic.asn1 file
V1 :
- Initial patch
net/ipv4/netfilter/nf_nat_snmp_basic.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-)
net/ipv4/netfilter/nf_nat_snmp_basic.c +6 -5
@@ -1109,7 +1109,7 @@ static int snmp_translate(struct nf_conn *ct, int dir, struct sk_buff *skb)if (!snmp_parse_mangle ((unsigned char *)udph +sizeof (struct udphdr ), paylen, &map, &udph->check)) { -net_warn_ratelimited ("bsalg: parser failed\n" ); +nf_ct_helper_log (skb, ct,"parser failed\n" );return NF_DROP ; }return NF_ACCEPT ;
@@ -1143,13 +1143,14 @@ static int help(struct sk_buff *skb, unsigned int protoff,* can mess around with the payload. */ if (ntohs (udph->len) != skb->len - (iph->ihl <<2 )) { -net_warn_ratelimited ("SNMP: dropping malformed packet src=%pI4 dst=%pI4\n" , - &iph->saddr, &iph->daddr); -return NF_DROP ; +nf_ct_helper_log (skb, ct,"dropping malformed packet\n" ); +return NF_DROP ; } -if (!skb_make_writable (skb, skb->len)) +if (!skb_make_writable (skb, skb->len)) { +nf_ct_helper_log (skb, ct,"cannot mangle packet" );return NF_DROP ; + }spin_lock_bh (&snmp_lock); ret =snmp_translate (ct, dir, skb);
Signature · 1 line
2.9.3
PATCH v3 5/5 netfilter: nf_nat_snmp_basic: use asn1 decoder library
In reply to Taehee YooThe basic SNMP ALG parse snmp ASN.1 payload
however, since 2012 linux kernel provide ASN.1 decoder library. If we use ASN.1 decoder in the /lib/asn1_decoder.c, we can remove about 1000 line of ASN.1 parsing routine.
To use asn1_decoder.c, we should write mib file(nf_nat_snmp_basic.asn1) then /script/asn1_compiler.c makes *-asn1.c and *-asn1.h file at the compiletime.(nf_nat_snmp_basic-asn1.c, nf_nat_snmp_basic-asn1.h) The nf_nat_snmp_basic.asn1 is made by RFC1155, RFC1157, RFC1902, RFC1905, RFC2578, RFC3416. of course that mib file supports only the basic SNMP ALG.
Previous SNMP ALG mangles only first octet of IPv4 address. but after this patch, the SNMP ALG mangles whole IPv4 Address. And SNMPv3 is not supported.
I tested with snmp commands such ans snmpd, snmpwalk, snmptrap.
---
V3 :
- be separated by previous patch.
V2 :
- Add missing nf_nat_snmp_basic.asn1 file
V1 :
- Initial patch
net/ipv4/netfilter/Kconfig | 1 + net/ipv4/netfilter/Makefile | 5 +- net/ipv4/netfilter/nf_nat_snmp_basic.asn1 | 177 +++++ net/ipv4/netfilter/nf_nat_snmp_basic.c | 1065 ++--------------------------- 4 files changed, 237 insertions(+), 1011 deletions(-) create mode 100644 net/ipv4/netfilter/nf_nat_snmp_basic.asn1
net/ipv4/netfilter/Kconfig +1 -0
@@ -148,6 +148,7 @@ config NF_NAT_SNMP_BASIC depends on NF_CONNTRACK_SNMP depends on NETFILTER_ADVANCED default NF_NAT && NF_CONNTRACK_SNMP + select ASN1 ---help--- This module implements an Application Layer Gateway (ALG) for
net/ipv4/netfilter/Makefile +4 -1
@@ -27,8 +27,11 @@ obj-$(CONFIG_NF_REJECT_IPV4) += nf_reject_ipv4.o # NAT helpers (nf_conntrack) obj-$(CONFIG_NF_NAT_H323) += nf_nat_h323.o obj-$(CONFIG_NF_NAT_PPTP) += nf_nat_pptp.o -obj-$(CONFIG_NF_NAT_SNMP_BASIC) += nf_nat_snmp_basic.o +nf_nat_snmp_basic-asn1.o := nf_nat_snmp_basic-asn1.c nf_nat_snmp_basic-asn1.h +obj-$(CONFIG_NF_NAT_SNMP_BASIC) += nf_nat_snmp_basic.o nf_nat_snmp_basic-asn1.o obj-$(CONFIG_NF_NAT_MASQUERADE_IPV4) += nf_nat_masquerade_ipv4.o +clean-files := nf_nat_snmp_basic-asn1.c nf_nat_snmp_basic-asn1.h + # NAT protocols (nf_nat) obj-$(CONFIG_NF_NAT_PROTO_GRE) += nf_nat_proto_gre.o
net/ipv4/netfilter/nf_nat_snmp_basic.asn1 new file +177 -0
177 lines not on this page · raw message
net/ipv4/netfilter/nf_nat_snmp_basic.c +55 -1010
1,124 lines not on this page · raw message
Signature · 1 line
2.9.3
Pablo Neira Ayuso
maintainerQuoted 2 lines · Taehee Yoo
The goal of this patch set are to use the ASN.1 decoder library to parse SNMP ASN.1 payload.
Series applied, thanks.
Pablo Neira Ayuso
maintainerQuoted 5 lines · Pablo Neira Ayuso
Quoted 2 lines · Taehee Yoo
The goal of this patch set are to use the ASN.1 decoder library to parse SNMP ASN.1 payload.
Series applied, thanks.
I'm hitting this here:
net/ipv4/netfilter/nf_nat_snmp_basic.c:57:36: fatal error: nf_nat_snmp_basic-asn1.h: File or directory does not exist