2 * (C) 2014 by Pablo Neira Ayuso <pablo@netfilter.org>
4 * Based on code from ebt_log from:
6 * Bart De Schuymer <bdschuym@pandora.be>
7 * Harald Welte <laforge@netfilter.org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
14 #include <linux/module.h>
15 #include <linux/spinlock.h>
16 #include <linux/skbuff.h>
17 #include <linux/if_arp.h>
19 #include <net/route.h>
21 #include <linux/netfilter.h>
22 #include <linux/netfilter/xt_LOG.h>
23 #include <net/netfilter/nf_log.h>
25 static struct nf_loginfo default_loginfo = {
26 .type = NF_LOG_TYPE_LOG,
30 .logflags = NF_LOG_MASK,
36 unsigned char mac_src[ETH_ALEN];
37 unsigned char ip_src[4];
38 unsigned char mac_dst[ETH_ALEN];
39 unsigned char ip_dst[4];
42 static void dump_arp_packet(struct nf_log_buf *m,
43 const struct nf_loginfo *info,
44 const struct sk_buff *skb, unsigned int nhoff)
46 const struct arphdr *ah;
48 const struct arppayload *ap;
49 struct arppayload _arpp;
51 ah = skb_header_pointer(skb, 0, sizeof(_arph), &_arph);
53 nf_log_buf_add(m, "TRUNCATED");
56 nf_log_buf_add(m, "ARP HTYPE=%d PTYPE=0x%04x OPCODE=%d",
57 ntohs(ah->ar_hrd), ntohs(ah->ar_pro), ntohs(ah->ar_op));
59 /* If it's for Ethernet and the lengths are OK, then log the ARP
62 if (ah->ar_hrd != htons(1) ||
63 ah->ar_hln != ETH_ALEN ||
64 ah->ar_pln != sizeof(__be32))
67 ap = skb_header_pointer(skb, sizeof(_arph), sizeof(_arpp), &_arpp);
69 nf_log_buf_add(m, " INCOMPLETE [%Zu bytes]",
70 skb->len - sizeof(_arph));
73 nf_log_buf_add(m, " MACSRC=%pM IPSRC=%pI4 MACDST=%pM IPDST=%pI4",
74 ap->mac_src, ap->ip_src, ap->mac_dst, ap->ip_dst);
77 void nf_log_arp_packet(struct net *net, u_int8_t pf,
78 unsigned int hooknum, const struct sk_buff *skb,
79 const struct net_device *in,
80 const struct net_device *out,
81 const struct nf_loginfo *loginfo,
86 /* FIXME: Disabled from containers until syslog ns is supported */
87 if (!net_eq(net, &init_net))
90 m = nf_log_buf_open();
93 loginfo = &default_loginfo;
95 nf_log_dump_packet_common(m, pf, hooknum, skb, in, out, loginfo,
97 dump_arp_packet(m, loginfo, skb, 0);
102 static struct nf_logger nf_arp_logger __read_mostly = {
103 .name = "nf_log_arp",
104 .type = NF_LOG_TYPE_LOG,
105 .logfn = nf_log_arp_packet,
109 static int __net_init nf_log_arp_net_init(struct net *net)
111 nf_log_set(net, NFPROTO_ARP, &nf_arp_logger);
115 static void __net_exit nf_log_arp_net_exit(struct net *net)
117 nf_log_unset(net, &nf_arp_logger);
120 static struct pernet_operations nf_log_arp_net_ops = {
121 .init = nf_log_arp_net_init,
122 .exit = nf_log_arp_net_exit,
125 static int __init nf_log_arp_init(void)
129 ret = register_pernet_subsys(&nf_log_arp_net_ops);
133 nf_log_register(NFPROTO_ARP, &nf_arp_logger);
137 static void __exit nf_log_arp_exit(void)
139 unregister_pernet_subsys(&nf_log_arp_net_ops);
140 nf_log_unregister(&nf_arp_logger);
143 module_init(nf_log_arp_init);
144 module_exit(nf_log_arp_exit);
146 MODULE_AUTHOR("Pablo Neira Ayuso <pablo@netfilter.org>");
147 MODULE_DESCRIPTION("Netfilter ARP packet logging");
148 MODULE_LICENSE("GPL");
149 MODULE_ALIAS_NF_LOGGER(3, 0);