5 * Bart De Schuymer <bdschuym@pandora.be>
11 #include <linux/netfilter_bridge/ebtables.h>
12 #include <linux/netfilter_bridge/ebt_log.h>
13 #include <linux/module.h>
15 #include <linux/if_arp.h>
16 #include <linux/spinlock.h>
18 static spinlock_t ebt_log_lock
= SPIN_LOCK_UNLOCKED
;
20 static int ebt_log_check(const char *tablename
, unsigned int hookmask
,
21 const struct ebt_entry
*e
, void *data
, unsigned int datalen
)
23 struct ebt_log_info
*info
= (struct ebt_log_info
*)data
;
25 if (datalen
!= EBT_ALIGN(sizeof(struct ebt_log_info
)))
27 if (info
->bitmask
& ~EBT_LOG_MASK
)
29 if (info
->loglevel
>= 8)
31 info
->prefix
[EBT_LOG_PREFIX_SIZE
- 1] = '\0';
43 unsigned char mac_src
[ETH_ALEN
];
44 unsigned char ip_src
[4];
45 unsigned char mac_dst
[ETH_ALEN
];
46 unsigned char ip_dst
[4];
49 static void print_MAC(unsigned char *p
)
53 for (i
= 0; i
< ETH_ALEN
; i
++, p
++)
54 printk("%02x%c", *p
, i
== ETH_ALEN
- 1 ? ' ':':');
57 #define myNIPQUAD(a) a[0], a[1], a[2], a[3]
58 static void ebt_log(const struct sk_buff
*skb
, const struct net_device
*in
,
59 const struct net_device
*out
, const void *data
, unsigned int datalen
)
61 struct ebt_log_info
*info
= (struct ebt_log_info
*)data
;
62 char level_string
[4] = "< >";
63 union {struct iphdr iph
; struct tcpudphdr ports
;
64 struct arphdr arph
; struct arppayload arpp
;} u
;
66 level_string
[1] = '0' + info
->loglevel
;
67 spin_lock_bh(&ebt_log_lock
);
69 printk("%s IN=%s OUT=%s ", info
->prefix
, in
? in
->name
: "",
70 out
? out
->name
: "");
72 printk("MAC source = ");
73 print_MAC(eth_hdr(skb
)->h_source
);
74 printk("MAC dest = ");
75 print_MAC(eth_hdr(skb
)->h_dest
);
77 printk("proto = 0x%04x", ntohs(eth_hdr(skb
)->h_proto
));
79 if ((info
->bitmask
& EBT_LOG_IP
) && eth_hdr(skb
)->h_proto
==
81 struct iphdr _iph
, *ih
;
83 ih
= skb_header_pointer(skb
, 0, sizeof(_iph
), &_iph
);
85 printk(" INCOMPLETE IP header");
88 printk(" IP SRC=%u.%u.%u.%u IP DST=%u.%u.%u.%u,",
89 NIPQUAD(ih
->saddr
), NIPQUAD(ih
->daddr
));
90 printk(" IP tos=0x%02X, IP proto=%d", u
.iph
.tos
,
92 if (ih
->protocol
== IPPROTO_TCP
||
93 ih
->protocol
== IPPROTO_UDP
) {
94 struct tcpudphdr _ports
, *pptr
;
96 pptr
= skb_header_pointer(skb
, ih
->ihl
*4,
97 sizeof(_ports
), &_ports
);
99 printk(" INCOMPLETE TCP/UDP header");
102 printk(" SPT=%u DPT=%u", ntohs(pptr
->src
),
108 if ((info
->bitmask
& EBT_LOG_ARP
) &&
109 ((eth_hdr(skb
)->h_proto
== htons(ETH_P_ARP
)) ||
110 (eth_hdr(skb
)->h_proto
== htons(ETH_P_RARP
)))) {
111 struct arphdr _arph
, *ah
;
113 ah
= skb_header_pointer(skb
, 0, sizeof(_arph
), &_arph
);
115 printk(" INCOMPLETE ARP header");
118 printk(" ARP HTYPE=%d, PTYPE=0x%04x, OPCODE=%d",
119 ntohs(ah
->ar_hrd
), ntohs(ah
->ar_pro
),
122 /* If it's for Ethernet and the lengths are OK,
123 * then log the ARP payload */
124 if (ah
->ar_hrd
== htons(1) &&
125 ah
->ar_hln
== ETH_ALEN
&&
126 ah
->ar_pln
== sizeof(uint32_t)) {
127 struct arppayload _arpp
, *ap
;
129 ap
= skb_header_pointer(skb
, sizeof(u
.arph
),
130 sizeof(_arpp
), &_arpp
);
132 printk(" INCOMPLETE ARP payload");
135 printk(" ARP MAC SRC=");
136 print_MAC(ap
->mac_src
);
137 printk(" ARP IP SRC=%u.%u.%u.%u",
138 myNIPQUAD(ap
->ip_src
));
139 printk(" ARP MAC DST=");
140 print_MAC(ap
->mac_dst
);
141 printk(" ARP IP DST=%u.%u.%u.%u",
142 myNIPQUAD(ap
->ip_dst
));
147 spin_unlock_bh(&ebt_log_lock
);
150 static struct ebt_watcher log
=
152 .name
= EBT_LOG_WATCHER
,
154 .check
= ebt_log_check
,
158 static int __init
init(void)
160 return ebt_register_watcher(&log
);
163 static void __exit
fini(void)
165 ebt_unregister_watcher(&log
);
170 MODULE_LICENSE("GPL");