12 #include <linux/netfilter_bridge/ebtables.h>
13 #include <linux/netfilter_bridge/ebt_log.h>
14 #include <linux/netfilter.h>
15 #include <linux/module.h>
18 #include <linux/if_arp.h>
19 #include <linux/spinlock.h>
20 #include <net/netfilter/nf_log.h>
21 #include <linux/ipv6.h>
23 #include <linux/in6.h>
25 static DEFINE_SPINLOCK(ebt_log_lock);
27 static int ebt_log_check(const char *tablename, unsigned int hookmask,
28 const struct ebt_entry *e, void *data, unsigned int datalen)
30 struct ebt_log_info *info = data;
32 if (datalen != EBT_ALIGN(sizeof(struct ebt_log_info)))
34 if (info->bitmask & ~EBT_LOG_MASK)
36 if (info->loglevel >= 8)
38 info->prefix[EBT_LOG_PREFIX_SIZE - 1] = '\0';
50 unsigned char mac_src[ETH_ALEN];
51 unsigned char ip_src[4];
52 unsigned char mac_dst[ETH_ALEN];
53 unsigned char ip_dst[4];
56 static void print_MAC(const unsigned char *p)
60 for (i = 0; i < ETH_ALEN; i++, p++)
61 printk("%02x%c", *p, i == ETH_ALEN - 1 ? ' ':':');
65 print_ports(const struct sk_buff *skb, uint8_t protocol, int offset)
67 if (protocol == IPPROTO_TCP ||
68 protocol == IPPROTO_UDP ||
69 protocol == IPPROTO_UDPLITE ||
70 protocol == IPPROTO_SCTP ||
71 protocol == IPPROTO_DCCP) {
72 const struct tcpudphdr *pptr;
73 struct tcpudphdr _ports;
75 pptr = skb_header_pointer(skb, offset,
76 sizeof(_ports), &_ports);
78 printk(" INCOMPLETE TCP/UDP header");
81 printk(" SPT=%u DPT=%u", ntohs(pptr->src), ntohs(pptr->dst));
85 #define myNIPQUAD(a) a[0], a[1], a[2], a[3]
87 ebt_log_packet(unsigned int pf, unsigned int hooknum,
88 const struct sk_buff *skb, const struct net_device *in,
89 const struct net_device *out, const struct nf_loginfo *loginfo,
94 spin_lock_bh(&ebt_log_lock);
95 printk("<%c>%s IN=%s OUT=%s MAC source = ", '0' + loginfo->u.log.level,
96 prefix, in ? in->name : "", out ? out->name : "");
98 print_MAC(eth_hdr(skb)->h_source);
99 printk("MAC dest = ");
100 print_MAC(eth_hdr(skb)->h_dest);
102 printk("proto = 0x%04x", ntohs(eth_hdr(skb)->h_proto));
104 if (loginfo->type == NF_LOG_TYPE_LOG)
105 bitmask = loginfo->u.log.logflags;
107 bitmask = NF_LOG_MASK;
109 if ((bitmask & EBT_LOG_IP) && eth_hdr(skb)->h_proto ==
111 const struct iphdr *ih;
114 ih = skb_header_pointer(skb, 0, sizeof(_iph), &_iph);
116 printk(" INCOMPLETE IP header");
119 printk(" IP SRC=%u.%u.%u.%u IP DST=%u.%u.%u.%u, IP "
120 "tos=0x%02X, IP proto=%d", NIPQUAD(ih->saddr),
121 NIPQUAD(ih->daddr), ih->tos, ih->protocol);
122 print_ports(skb, ih->protocol, ih->ihl*4);
126 #if defined(CONFIG_BRIDGE_EBT_IP6) || defined(CONFIG_BRIDGE_EBT_IP6_MODULE)
127 if ((bitmask & EBT_LOG_IP6) && eth_hdr(skb)->h_proto ==
129 const struct ipv6hdr *ih;
134 ih = skb_header_pointer(skb, 0, sizeof(_iph), &_iph);
136 printk(" INCOMPLETE IPv6 header");
139 printk(" IPv6 SRC=%x:%x:%x:%x:%x:%x:%x:%x "
140 "IPv6 DST=%x:%x:%x:%x:%x:%x:%x:%x, IPv6 "
141 "priority=0x%01X, Next Header=%d", NIP6(ih->saddr),
142 NIP6(ih->daddr), ih->priority, ih->nexthdr);
143 nexthdr = ih->nexthdr;
144 offset_ph = ipv6_skip_exthdr(skb, sizeof(_iph), &nexthdr);
147 print_ports(skb, nexthdr, offset_ph);
152 if ((bitmask & EBT_LOG_ARP) &&
153 ((eth_hdr(skb)->h_proto == htons(ETH_P_ARP)) ||
154 (eth_hdr(skb)->h_proto == htons(ETH_P_RARP)))) {
155 const struct arphdr *ah;
158 ah = skb_header_pointer(skb, 0, sizeof(_arph), &_arph);
160 printk(" INCOMPLETE ARP header");
163 printk(" ARP HTYPE=%d, PTYPE=0x%04x, OPCODE=%d",
164 ntohs(ah->ar_hrd), ntohs(ah->ar_pro),
167 /* If it's for Ethernet and the lengths are OK,
168 * then log the ARP payload */
169 if (ah->ar_hrd == htons(1) &&
170 ah->ar_hln == ETH_ALEN &&
171 ah->ar_pln == sizeof(__be32)) {
172 const struct arppayload *ap;
173 struct arppayload _arpp;
175 ap = skb_header_pointer(skb, sizeof(_arph),
176 sizeof(_arpp), &_arpp);
178 printk(" INCOMPLETE ARP payload");
181 printk(" ARP MAC SRC=");
182 print_MAC(ap->mac_src);
183 printk(" ARP IP SRC=%u.%u.%u.%u",
184 myNIPQUAD(ap->ip_src));
185 printk(" ARP MAC DST=");
186 print_MAC(ap->mac_dst);
187 printk(" ARP IP DST=%u.%u.%u.%u",
188 myNIPQUAD(ap->ip_dst));
193 spin_unlock_bh(&ebt_log_lock);
197 static void ebt_log(const struct sk_buff *skb, unsigned int hooknr,
198 const struct net_device *in, const struct net_device *out,
199 const void *data, unsigned int datalen)
201 const struct ebt_log_info *info = data;
202 struct nf_loginfo li;
204 li.type = NF_LOG_TYPE_LOG;
205 li.u.log.level = info->loglevel;
206 li.u.log.logflags = info->bitmask;
208 if (info->bitmask & EBT_LOG_NFLOG)
209 nf_log_packet(PF_BRIDGE, hooknr, skb, in, out, &li,
212 ebt_log_packet(PF_BRIDGE, hooknr, skb, in, out, &li,
216 static struct ebt_watcher log =
218 .name = EBT_LOG_WATCHER,
220 .check = ebt_log_check,
224 static const struct nf_logger ebt_log_logger = {
226 .logfn = &ebt_log_packet,
230 static int __init ebt_log_init(void)
234 ret = ebt_register_watcher(&log);
237 nf_log_register(PF_BRIDGE, &ebt_log_logger);
241 static void __exit ebt_log_fini(void)
243 nf_log_unregister(&ebt_log_logger);
244 ebt_unregister_watcher(&log);
247 module_init(ebt_log_init);
248 module_exit(ebt_log_fini);
249 MODULE_DESCRIPTION("Ebtables: Packet logging to syslog");
250 MODULE_LICENSE("GPL");