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 int 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];
54 print_ports(const struct sk_buff *skb, uint8_t protocol, int offset)
56 if (protocol == IPPROTO_TCP ||
57 protocol == IPPROTO_UDP ||
58 protocol == IPPROTO_UDPLITE ||
59 protocol == IPPROTO_SCTP ||
60 protocol == IPPROTO_DCCP) {
61 const struct tcpudphdr *pptr;
62 struct tcpudphdr _ports;
64 pptr = skb_header_pointer(skb, offset,
65 sizeof(_ports), &_ports);
67 printk(" INCOMPLETE TCP/UDP header");
70 printk(" SPT=%u DPT=%u", ntohs(pptr->src), ntohs(pptr->dst));
75 ebt_log_packet(u_int8_t pf, unsigned int hooknum,
76 const struct sk_buff *skb, const struct net_device *in,
77 const struct net_device *out, const struct nf_loginfo *loginfo,
82 spin_lock_bh(&ebt_log_lock);
83 printk(KERN_SOH "%c%s IN=%s OUT=%s MAC source = %pM MAC dest = %pM proto = 0x%04x",
84 '0' + loginfo->u.log.level, prefix,
85 in ? in->name : "", out ? out->name : "",
86 eth_hdr(skb)->h_source, eth_hdr(skb)->h_dest,
87 ntohs(eth_hdr(skb)->h_proto));
89 if (loginfo->type == NF_LOG_TYPE_LOG)
90 bitmask = loginfo->u.log.logflags;
92 bitmask = NF_LOG_MASK;
94 if ((bitmask & EBT_LOG_IP) && eth_hdr(skb)->h_proto ==
96 const struct iphdr *ih;
99 ih = skb_header_pointer(skb, 0, sizeof(_iph), &_iph);
101 printk(" INCOMPLETE IP header");
104 printk(" IP SRC=%pI4 IP DST=%pI4, IP tos=0x%02X, IP proto=%d",
105 &ih->saddr, &ih->daddr, ih->tos, ih->protocol);
106 print_ports(skb, ih->protocol, ih->ihl*4);
110 #if IS_ENABLED(CONFIG_BRIDGE_EBT_IP6)
111 if ((bitmask & EBT_LOG_IP6) && eth_hdr(skb)->h_proto ==
113 const struct ipv6hdr *ih;
119 ih = skb_header_pointer(skb, 0, sizeof(_iph), &_iph);
121 printk(" INCOMPLETE IPv6 header");
124 printk(" IPv6 SRC=%pI6 IPv6 DST=%pI6, IPv6 priority=0x%01X, Next Header=%d",
125 &ih->saddr, &ih->daddr, ih->priority, ih->nexthdr);
126 nexthdr = ih->nexthdr;
127 offset_ph = ipv6_skip_exthdr(skb, sizeof(_iph), &nexthdr, &frag_off);
130 print_ports(skb, nexthdr, offset_ph);
135 if ((bitmask & EBT_LOG_ARP) &&
136 ((eth_hdr(skb)->h_proto == htons(ETH_P_ARP)) ||
137 (eth_hdr(skb)->h_proto == htons(ETH_P_RARP)))) {
138 const struct arphdr *ah;
141 ah = skb_header_pointer(skb, 0, sizeof(_arph), &_arph);
143 printk(" INCOMPLETE ARP header");
146 printk(" ARP HTYPE=%d, PTYPE=0x%04x, OPCODE=%d",
147 ntohs(ah->ar_hrd), ntohs(ah->ar_pro),
150 /* If it's for Ethernet and the lengths are OK,
151 * then log the ARP payload */
152 if (ah->ar_hrd == htons(1) &&
153 ah->ar_hln == ETH_ALEN &&
154 ah->ar_pln == sizeof(__be32)) {
155 const struct arppayload *ap;
156 struct arppayload _arpp;
158 ap = skb_header_pointer(skb, sizeof(_arph),
159 sizeof(_arpp), &_arpp);
161 printk(" INCOMPLETE ARP payload");
164 printk(" ARP MAC SRC=%pM ARP IP SRC=%pI4 ARP MAC DST=%pM ARP IP DST=%pI4",
165 ap->mac_src, ap->ip_src, ap->mac_dst, ap->ip_dst);
170 spin_unlock_bh(&ebt_log_lock);
175 ebt_log_tg(struct sk_buff *skb, const struct xt_action_param *par)
177 const struct ebt_log_info *info = par->targinfo;
178 struct nf_loginfo li;
180 li.type = NF_LOG_TYPE_LOG;
181 li.u.log.level = info->loglevel;
182 li.u.log.logflags = info->bitmask;
184 if (info->bitmask & EBT_LOG_NFLOG)
185 nf_log_packet(NFPROTO_BRIDGE, par->hooknum, skb, par->in,
186 par->out, &li, "%s", info->prefix);
188 ebt_log_packet(NFPROTO_BRIDGE, par->hooknum, skb, par->in,
189 par->out, &li, info->prefix);
193 static struct xt_target ebt_log_tg_reg __read_mostly = {
196 .family = NFPROTO_BRIDGE,
197 .target = ebt_log_tg,
198 .checkentry = ebt_log_tg_check,
199 .targetsize = sizeof(struct ebt_log_info),
203 static struct nf_logger ebt_log_logger __read_mostly = {
205 .logfn = &ebt_log_packet,
209 static int __init ebt_log_init(void)
213 ret = xt_register_target(&ebt_log_tg_reg);
216 nf_log_register(NFPROTO_BRIDGE, &ebt_log_logger);
220 static void __exit ebt_log_fini(void)
222 nf_log_unregister(&ebt_log_logger);
223 xt_unregister_target(&ebt_log_tg_reg);
226 module_init(ebt_log_init);
227 module_exit(ebt_log_fini);
228 MODULE_DESCRIPTION("Ebtables: Packet logging to syslog");
229 MODULE_LICENSE("GPL");