Skip to content

Commit 7eb9282

Browse files
committed
netfilter: ipt_LOG/ip6t_LOG: add option to print decoded MAC header
The LOG targets print the entire MAC header as one long string, which is not readable very well: IN=eth0 OUT= MAC=00:15:f2:24:91:f8:00:1b:24:dc:61:e6:08:00 ... Add an option to decode known header formats (currently just ARPHRD_ETHER devices) in their individual fields: IN=eth0 OUT= MACSRC=00:1b:24:dc:61:e6 MACDST=00:15:f2:24:91:f8 MACPROTO=0800 ... IN=eth0 OUT= MACSRC=00:1b:24:dc:61:e6 MACDST=00:15:f2:24:91:f8 MACPROTO=86dd ... The option needs to be explicitly enabled by userspace to avoid breaking existing parsers. Signed-off-by: Patrick McHardy <kaber@trash.net>
1 parent cf377eb commit 7eb9282

4 files changed

Lines changed: 97 additions & 44 deletions

File tree

‎include/linux/netfilter_ipv4/ipt_LOG.h‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
#define IPT_LOG_IPOPT 0x04 /* Log IP options */
88
#define IPT_LOG_UID 0x08 /* Log UID owning local socket */
99
#define IPT_LOG_NFLOG 0x10 /* Unsupported, don't reuse */
10-
#define IPT_LOG_MASK 0x1f
10+
#define IPT_LOG_MACDECODE 0x20 /* Decode MAC header */
11+
#define IPT_LOG_MASK 0x2f
1112

1213
struct ipt_log_info {
1314
unsigned char level;

‎include/linux/netfilter_ipv6/ip6t_LOG.h‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
#define IP6T_LOG_IPOPT 0x04 /* Log IP options */
88
#define IP6T_LOG_UID 0x08 /* Log UID owning local socket */
99
#define IP6T_LOG_NFLOG 0x10 /* Unsupported, don't use */
10-
#define IP6T_LOG_MASK 0x1f
10+
#define IP6T_LOG_MACDECODE 0x20 /* Decode MAC header */
11+
#define IP6T_LOG_MASK 0x2f
1112

1213
struct ip6t_log_info {
1314
unsigned char level;

‎net/ipv4/netfilter/ipt_LOG.c‎

Lines changed: 40 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <linux/module.h>
1414
#include <linux/spinlock.h>
1515
#include <linux/skbuff.h>
16+
#include <linux/if_arp.h>
1617
#include <linux/ip.h>
1718
#include <net/icmp.h>
1819
#include <net/udp.h>
@@ -363,6 +364,42 @@ static void dump_packet(const struct nf_loginfo *info,
363364
/* maxlen = 230+ 91 + 230 + 252 = 803 */
364365
}
365366

367+
static void dump_mac_header(const struct nf_loginfo *info,
368+
const struct sk_buff *skb)
369+
{
370+
struct net_device *dev = skb->dev;
371+
unsigned int logflags = 0;
372+
373+
if (info->type == NF_LOG_TYPE_LOG)
374+
logflags = info->u.log.logflags;
375+
376+
if (!(logflags & IPT_LOG_MACDECODE))
377+
goto fallback;
378+
379+
switch (dev->type) {
380+
case ARPHRD_ETHER:
381+
printk("MACSRC=%pM MACDST=%pM MACPROTO=%04x ",
382+
eth_hdr(skb)->h_source, eth_hdr(skb)->h_dest,
383+
ntohs(eth_hdr(skb)->h_proto));
384+
return;
385+
default:
386+
break;
387+
}
388+
389+
fallback:
390+
printk("MAC=");
391+
if (dev->hard_header_len &&
392+
skb->mac_header != skb->network_header) {
393+
const unsigned char *p = skb_mac_header(skb);
394+
unsigned int i;
395+
396+
printk("%02x", *p++);
397+
for (i = 1; i < dev->hard_header_len; i++, p++)
398+
printk(":%02x", *p);
399+
}
400+
printk(" ");
401+
}
402+
366403
static struct nf_loginfo default_loginfo = {
367404
.type = NF_LOG_TYPE_LOG,
368405
.u = {
@@ -404,20 +441,9 @@ ipt_log_packet(u_int8_t pf,
404441
}
405442
#endif
406443

407-
if (in && !out) {
408-
/* MAC logging for input chain only. */
409-
printk("MAC=");
410-
if (skb->dev && skb->dev->hard_header_len &&
411-
skb->mac_header != skb->network_header) {
412-
int i;
413-
const unsigned char *p = skb_mac_header(skb);
414-
415-
printk("%02x", *p++);
416-
for (i = 1; i < skb->dev->hard_header_len; i++, p++)
417-
printk(":%02x", *p);
418-
}
419-
printk(" ");
420-
}
444+
/* MAC logging for input path only. */
445+
if (in && !out)
446+
dump_mac_header(loginfo, skb);
421447

422448
dump_packet(loginfo, skb, 0);
423449
printk("\n");

‎net/ipv6/netfilter/ip6t_LOG.c‎

Lines changed: 53 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,56 @@ static void dump_packet(const struct nf_loginfo *info,
373373
printk("MARK=0x%x ", skb->mark);
374374
}
375375

376+
static void dump_mac_header(const struct nf_loginfo *info,
377+
const struct sk_buff *skb)
378+
{
379+
struct net_device *dev = skb->dev;
380+
unsigned int logflags = 0;
381+
382+
if (info->type == NF_LOG_TYPE_LOG)
383+
logflags = info->u.log.logflags;
384+
385+
if (!(logflags & IP6T_LOG_MACDECODE))
386+
goto fallback;
387+
388+
switch (dev->type) {
389+
case ARPHRD_ETHER:
390+
printk("MACSRC=%pM MACDST=%pM MACPROTO=%04x ",
391+
eth_hdr(skb)->h_source, eth_hdr(skb)->h_dest,
392+
ntohs(eth_hdr(skb)->h_proto));
393+
return;
394+
default:
395+
break;
396+
}
397+
398+
fallback:
399+
printk("MAC=");
400+
if (dev->hard_header_len &&
401+
skb->mac_header != skb->network_header) {
402+
const unsigned char *p = skb_mac_header(skb);
403+
unsigned int len = dev->hard_header_len;
404+
unsigned int i;
405+
406+
if (dev->type == ARPHRD_SIT &&
407+
(p -= ETH_HLEN) < skb->head)
408+
p = NULL;
409+
410+
if (p != NULL) {
411+
printk("%02x", *p++);
412+
for (i = 1; i < len; i++)
413+
printk(":%02x", p[i]);
414+
}
415+
printk(" ");
416+
417+
if (dev->type == ARPHRD_SIT) {
418+
const struct iphdr *iph =
419+
(struct iphdr *)skb_mac_header(skb);
420+
printk("TUNNEL=%pI4->%pI4 ", &iph->saddr, &iph->daddr);
421+
}
422+
} else
423+
printk(" ");
424+
}
425+
376426
static struct nf_loginfo default_loginfo = {
377427
.type = NF_LOG_TYPE_LOG,
378428
.u = {
@@ -400,35 +450,10 @@ ip6t_log_packet(u_int8_t pf,
400450
prefix,
401451
in ? in->name : "",
402452
out ? out->name : "");
403-
if (in && !out) {
404-
unsigned int len;
405-
/* MAC logging for input chain only. */
406-
printk("MAC=");
407-
if (skb->dev && (len = skb->dev->hard_header_len) &&
408-
skb->mac_header != skb->network_header) {
409-
const unsigned char *p = skb_mac_header(skb);
410-
int i;
411-
412-
if (skb->dev->type == ARPHRD_SIT &&
413-
(p -= ETH_HLEN) < skb->head)
414-
p = NULL;
415-
416-
if (p != NULL) {
417-
printk("%02x", *p++);
418-
for (i = 1; i < len; i++)
419-
printk(":%02x", p[i]);
420-
}
421-
printk(" ");
422453

423-
if (skb->dev->type == ARPHRD_SIT) {
424-
const struct iphdr *iph =
425-
(struct iphdr *)skb_mac_header(skb);
426-
printk("TUNNEL=%pI4->%pI4 ",
427-
&iph->saddr, &iph->daddr);
428-
}
429-
} else
430-
printk(" ");
431-
}
454+
/* MAC logging for input path only. */
455+
if (in && !out)
456+
dump_mac_header(loginfo, skb);
432457

433458
dump_packet(loginfo, skb, skb_network_offset(skb), 1);
434459
printk("\n");

0 commit comments

Comments
 (0)