5 * Bart De Schuymer <bdschuym@pandora.be>
6 * Harald Welte <laforge@netfilter.org>
13 #include <linux/netfilter_bridge/ebtables.h>
14 #include <linux/netfilter_bridge/ebt_log.h>
15 #include <linux/netfilter.h>
16 #include <linux/module.h>
19 #include <linux/if_arp.h>
20 #include <linux/spinlock.h>
22 static DEFINE_SPINLOCK(ebt_log_lock
);
24 static int ebt_log_check(const char *tablename
, unsigned int hookmask
,
25 const struct ebt_entry
*e
, void *data
, unsigned int datalen
)
27 struct ebt_log_info
*info
= (struct ebt_log_info
*)data
;
29 if (datalen
!= EBT_ALIGN(sizeof(struct ebt_log_info
)))
31 if (info
->bitmask
& ~EBT_LOG_MASK
)
33 if (info
->loglevel
>= 8)
35 info
->prefix
[EBT_LOG_PREFIX_SIZE
- 1] = '\0';
47 unsigned char mac_src
[ETH_ALEN
];
48 unsigned char ip_src
[4];
49 unsigned char mac_dst
[ETH_ALEN
];
50 unsigned char ip_dst
[4];
53 static void print_MAC(unsigned char *p
)
57 for (i
= 0; i
< ETH_ALEN
; i
++, p
++)
58 printk("%02x%c", *p
, i
== ETH_ALEN
- 1 ? ' ':':');
61 #define myNIPQUAD(a) a[0], a[1], a[2], a[3]
63 ebt_log_packet(unsigned int pf
, unsigned int hooknum
,
64 const struct sk_buff
*skb
, const struct net_device
*in
,
65 const struct net_device
*out
, const struct nf_loginfo
*loginfo
,
70 spin_lock_bh(&ebt_log_lock
);
71 printk("<%c>%s IN=%s OUT=%s MAC source = ", '0' + loginfo
->u
.log
.level
,
72 prefix
, in
? in
->name
: "", out
? out
->name
: "");
74 print_MAC(eth_hdr(skb
)->h_source
);
75 printk("MAC dest = ");
76 print_MAC(eth_hdr(skb
)->h_dest
);
78 printk("proto = 0x%04x", ntohs(eth_hdr(skb
)->h_proto
));
80 if (loginfo
->type
== NF_LOG_TYPE_LOG
)
81 bitmask
= loginfo
->u
.log
.logflags
;
83 bitmask
= NF_LOG_MASK
;
85 if ((bitmask
& EBT_LOG_IP
) && eth_hdr(skb
)->h_proto
==
87 struct iphdr _iph
, *ih
;
89 ih
= skb_header_pointer(skb
, 0, sizeof(_iph
), &_iph
);
91 printk(" INCOMPLETE IP header");
94 printk(" IP SRC=%u.%u.%u.%u IP DST=%u.%u.%u.%u, IP "
95 "tos=0x%02X, IP proto=%d", NIPQUAD(ih
->saddr
),
96 NIPQUAD(ih
->daddr
), ih
->tos
, ih
->protocol
);
97 if (ih
->protocol
== IPPROTO_TCP
||
98 ih
->protocol
== IPPROTO_UDP
||
99 ih
->protocol
== IPPROTO_UDPLITE
||
100 ih
->protocol
== IPPROTO_SCTP
||
101 ih
->protocol
== IPPROTO_DCCP
) {
102 struct tcpudphdr _ports
, *pptr
;
104 pptr
= skb_header_pointer(skb
, ih
->ihl
*4,
105 sizeof(_ports
), &_ports
);
107 printk(" INCOMPLETE TCP/UDP header");
110 printk(" SPT=%u DPT=%u", ntohs(pptr
->src
),
116 if ((bitmask
& EBT_LOG_ARP
) &&
117 ((eth_hdr(skb
)->h_proto
== htons(ETH_P_ARP
)) ||
118 (eth_hdr(skb
)->h_proto
== htons(ETH_P_RARP
)))) {
119 struct arphdr _arph
, *ah
;
121 ah
= skb_header_pointer(skb
, 0, sizeof(_arph
), &_arph
);
123 printk(" INCOMPLETE ARP header");
126 printk(" ARP HTYPE=%d, PTYPE=0x%04x, OPCODE=%d",
127 ntohs(ah
->ar_hrd
), ntohs(ah
->ar_pro
),
130 /* If it's for Ethernet and the lengths are OK,
131 * then log the ARP payload */
132 if (ah
->ar_hrd
== htons(1) &&
133 ah
->ar_hln
== ETH_ALEN
&&
134 ah
->ar_pln
== sizeof(__be32
)) {
135 struct arppayload _arpp
, *ap
;
137 ap
= skb_header_pointer(skb
, sizeof(_arph
),
138 sizeof(_arpp
), &_arpp
);
140 printk(" INCOMPLETE ARP payload");
143 printk(" ARP MAC SRC=");
144 print_MAC(ap
->mac_src
);
145 printk(" ARP IP SRC=%u.%u.%u.%u",
146 myNIPQUAD(ap
->ip_src
));
147 printk(" ARP MAC DST=");
148 print_MAC(ap
->mac_dst
);
149 printk(" ARP IP DST=%u.%u.%u.%u",
150 myNIPQUAD(ap
->ip_dst
));
155 spin_unlock_bh(&ebt_log_lock
);
159 static void ebt_log(const struct sk_buff
*skb
, unsigned int hooknr
,
160 const struct net_device
*in
, const struct net_device
*out
,
161 const void *data
, unsigned int datalen
)
163 struct ebt_log_info
*info
= (struct ebt_log_info
*)data
;
164 struct nf_loginfo li
;
166 li
.type
= NF_LOG_TYPE_LOG
;
167 li
.u
.log
.level
= info
->loglevel
;
168 li
.u
.log
.logflags
= info
->bitmask
;
170 if (info
->bitmask
& EBT_LOG_NFLOG
)
171 nf_log_packet(PF_BRIDGE
, hooknr
, skb
, in
, out
, &li
,
174 ebt_log_packet(PF_BRIDGE
, hooknr
, skb
, in
, out
, &li
,
178 static struct ebt_watcher log
=
180 .name
= EBT_LOG_WATCHER
,
182 .check
= ebt_log_check
,
186 static struct nf_logger ebt_log_logger
= {
188 .logfn
= &ebt_log_packet
,
192 static int __init
ebt_log_init(void)
196 ret
= ebt_register_watcher(&log
);
199 ret
= nf_log_register(PF_BRIDGE
, &ebt_log_logger
);
200 if (ret
< 0 && ret
!= -EEXIST
)
201 ebt_unregister_watcher(&log
);
205 static void __exit
ebt_log_fini(void)
207 nf_log_unregister(&ebt_log_logger
);
208 ebt_unregister_watcher(&log
);
211 module_init(ebt_log_init
);
212 module_exit(ebt_log_fini
);
213 MODULE_LICENSE("GPL");