5 * Bart De Schuymer <bdschuym@pandora.be>
6 * Harald Welte <laforge@netfilter.org>
11 #include <linux/module.h>
14 #include <linux/if_arp.h>
15 #include <linux/spinlock.h>
16 #include <net/netfilter/nf_log.h>
17 #include <linux/ipv6.h>
19 #include <linux/in6.h>
20 #include <linux/netfilter/x_tables.h>
21 #include <linux/netfilter_bridge/ebtables.h>
22 #include <linux/netfilter_bridge/ebt_log.h>
23 #include <linux/netfilter.h>
25 static DEFINE_SPINLOCK(ebt_log_lock
);
27 static bool ebt_log_tg_check(const struct xt_tgchk_param
*par
)
29 struct ebt_log_info
*info
= par
->targinfo
;
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(const unsigned char *p
)
57 for (i
= 0; i
< ETH_ALEN
; i
++, p
++)
58 printk("%02x%c", *p
, i
== ETH_ALEN
- 1 ? ' ':':');
62 print_ports(const struct sk_buff
*skb
, uint8_t protocol
, int offset
)
64 if (protocol
== IPPROTO_TCP
||
65 protocol
== IPPROTO_UDP
||
66 protocol
== IPPROTO_UDPLITE
||
67 protocol
== IPPROTO_SCTP
||
68 protocol
== IPPROTO_DCCP
) {
69 const struct tcpudphdr
*pptr
;
70 struct tcpudphdr _ports
;
72 pptr
= skb_header_pointer(skb
, offset
,
73 sizeof(_ports
), &_ports
);
75 printk(" INCOMPLETE TCP/UDP header");
78 printk(" SPT=%u DPT=%u", ntohs(pptr
->src
), ntohs(pptr
->dst
));
82 #define myNIPQUAD(a) a[0], a[1], a[2], a[3]
84 ebt_log_packet(u_int8_t pf
, unsigned int hooknum
,
85 const struct sk_buff
*skb
, const struct net_device
*in
,
86 const struct net_device
*out
, const struct nf_loginfo
*loginfo
,
91 spin_lock_bh(&ebt_log_lock
);
92 printk("<%c>%s IN=%s OUT=%s MAC source = ", '0' + loginfo
->u
.log
.level
,
93 prefix
, in
? in
->name
: "", out
? out
->name
: "");
95 print_MAC(eth_hdr(skb
)->h_source
);
96 printk("MAC dest = ");
97 print_MAC(eth_hdr(skb
)->h_dest
);
99 printk("proto = 0x%04x", ntohs(eth_hdr(skb
)->h_proto
));
101 if (loginfo
->type
== NF_LOG_TYPE_LOG
)
102 bitmask
= loginfo
->u
.log
.logflags
;
104 bitmask
= NF_LOG_MASK
;
106 if ((bitmask
& EBT_LOG_IP
) && eth_hdr(skb
)->h_proto
==
108 const struct iphdr
*ih
;
111 ih
= skb_header_pointer(skb
, 0, sizeof(_iph
), &_iph
);
113 printk(" INCOMPLETE IP header");
116 printk(" IP SRC=%u.%u.%u.%u IP DST=%u.%u.%u.%u, IP "
117 "tos=0x%02X, IP proto=%d", NIPQUAD(ih
->saddr
),
118 NIPQUAD(ih
->daddr
), ih
->tos
, ih
->protocol
);
119 print_ports(skb
, ih
->protocol
, ih
->ihl
*4);
123 #if defined(CONFIG_BRIDGE_EBT_IP6) || defined(CONFIG_BRIDGE_EBT_IP6_MODULE)
124 if ((bitmask
& EBT_LOG_IP6
) && eth_hdr(skb
)->h_proto
==
126 const struct ipv6hdr
*ih
;
131 ih
= skb_header_pointer(skb
, 0, sizeof(_iph
), &_iph
);
133 printk(" INCOMPLETE IPv6 header");
136 printk(" IPv6 SRC=%x:%x:%x:%x:%x:%x:%x:%x "
137 "IPv6 DST=%x:%x:%x:%x:%x:%x:%x:%x, IPv6 "
138 "priority=0x%01X, Next Header=%d", NIP6(ih
->saddr
),
139 NIP6(ih
->daddr
), ih
->priority
, ih
->nexthdr
);
140 nexthdr
= ih
->nexthdr
;
141 offset_ph
= ipv6_skip_exthdr(skb
, sizeof(_iph
), &nexthdr
);
144 print_ports(skb
, nexthdr
, offset_ph
);
149 if ((bitmask
& EBT_LOG_ARP
) &&
150 ((eth_hdr(skb
)->h_proto
== htons(ETH_P_ARP
)) ||
151 (eth_hdr(skb
)->h_proto
== htons(ETH_P_RARP
)))) {
152 const struct arphdr
*ah
;
155 ah
= skb_header_pointer(skb
, 0, sizeof(_arph
), &_arph
);
157 printk(" INCOMPLETE ARP header");
160 printk(" ARP HTYPE=%d, PTYPE=0x%04x, OPCODE=%d",
161 ntohs(ah
->ar_hrd
), ntohs(ah
->ar_pro
),
164 /* If it's for Ethernet and the lengths are OK,
165 * then log the ARP payload */
166 if (ah
->ar_hrd
== htons(1) &&
167 ah
->ar_hln
== ETH_ALEN
&&
168 ah
->ar_pln
== sizeof(__be32
)) {
169 const struct arppayload
*ap
;
170 struct arppayload _arpp
;
172 ap
= skb_header_pointer(skb
, sizeof(_arph
),
173 sizeof(_arpp
), &_arpp
);
175 printk(" INCOMPLETE ARP payload");
178 printk(" ARP MAC SRC=");
179 print_MAC(ap
->mac_src
);
180 printk(" ARP IP SRC=%u.%u.%u.%u",
181 myNIPQUAD(ap
->ip_src
));
182 printk(" ARP MAC DST=");
183 print_MAC(ap
->mac_dst
);
184 printk(" ARP IP DST=%u.%u.%u.%u",
185 myNIPQUAD(ap
->ip_dst
));
190 spin_unlock_bh(&ebt_log_lock
);
195 ebt_log_tg(struct sk_buff
*skb
, const struct xt_target_param
*par
)
197 const struct ebt_log_info
*info
= par
->targinfo
;
198 struct nf_loginfo li
;
200 li
.type
= NF_LOG_TYPE_LOG
;
201 li
.u
.log
.level
= info
->loglevel
;
202 li
.u
.log
.logflags
= info
->bitmask
;
204 if (info
->bitmask
& EBT_LOG_NFLOG
)
205 nf_log_packet(NFPROTO_BRIDGE
, par
->hooknum
, skb
, par
->in
,
206 par
->out
, &li
, "%s", info
->prefix
);
208 ebt_log_packet(NFPROTO_BRIDGE
, par
->hooknum
, skb
, par
->in
,
209 par
->out
, &li
, info
->prefix
);
213 static struct xt_target ebt_log_tg_reg __read_mostly
= {
216 .family
= NFPROTO_BRIDGE
,
217 .target
= ebt_log_tg
,
218 .checkentry
= ebt_log_tg_check
,
219 .targetsize
= XT_ALIGN(sizeof(struct ebt_log_info
)),
223 static const struct nf_logger ebt_log_logger
= {
225 .logfn
= &ebt_log_packet
,
229 static int __init
ebt_log_init(void)
233 ret
= xt_register_target(&ebt_log_tg_reg
);
236 nf_log_register(NFPROTO_BRIDGE
, &ebt_log_logger
);
240 static void __exit
ebt_log_fini(void)
242 nf_log_unregister(&ebt_log_logger
);
243 xt_unregister_target(&ebt_log_tg_reg
);
246 module_init(ebt_log_init
);
247 module_exit(ebt_log_fini
);
248 MODULE_DESCRIPTION("Ebtables: Packet logging to syslog");
249 MODULE_LICENSE("GPL");