Merge tag 'please-pull-misc-3.19' of git://git.kernel.org/pub/scm/linux/kernel/git...
[firefly-linux-kernel-4.4.55.git] / net / ipv4 / netfilter / nf_log_arp.c
1 /*
2  * (C) 2014 by Pablo Neira Ayuso <pablo@netfilter.org>
3  *
4  * Based on code from ebt_log from:
5  *
6  * Bart De Schuymer <bdschuym@pandora.be>
7  * Harald Welte <laforge@netfilter.org>
8  *
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.
12  */
13 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14
15 #include <linux/module.h>
16 #include <linux/spinlock.h>
17 #include <linux/skbuff.h>
18 #include <linux/if_arp.h>
19 #include <linux/ip.h>
20 #include <net/route.h>
21
22 #include <linux/netfilter.h>
23 #include <linux/netfilter/xt_LOG.h>
24 #include <net/netfilter/nf_log.h>
25
26 static struct nf_loginfo default_loginfo = {
27         .type   = NF_LOG_TYPE_LOG,
28         .u = {
29                 .log = {
30                         .level    = 5,
31                         .logflags = NF_LOG_MASK,
32                 },
33         },
34 };
35
36 struct arppayload {
37         unsigned char mac_src[ETH_ALEN];
38         unsigned char ip_src[4];
39         unsigned char mac_dst[ETH_ALEN];
40         unsigned char ip_dst[4];
41 };
42
43 static void dump_arp_packet(struct nf_log_buf *m,
44                             const struct nf_loginfo *info,
45                             const struct sk_buff *skb, unsigned int nhoff)
46 {
47         const struct arphdr *ah;
48         struct arphdr _arph;
49         const struct arppayload *ap;
50         struct arppayload _arpp;
51
52         ah = skb_header_pointer(skb, 0, sizeof(_arph), &_arph);
53         if (ah == NULL) {
54                 nf_log_buf_add(m, "TRUNCATED");
55                 return;
56         }
57         nf_log_buf_add(m, "ARP HTYPE=%d PTYPE=0x%04x OPCODE=%d",
58                        ntohs(ah->ar_hrd), ntohs(ah->ar_pro), ntohs(ah->ar_op));
59
60         /* If it's for Ethernet and the lengths are OK, then log the ARP
61          * payload.
62          */
63         if (ah->ar_hrd != htons(1) ||
64             ah->ar_hln != ETH_ALEN ||
65             ah->ar_pln != sizeof(__be32))
66                 return;
67
68         ap = skb_header_pointer(skb, sizeof(_arph), sizeof(_arpp), &_arpp);
69         if (ap == NULL) {
70                 nf_log_buf_add(m, " INCOMPLETE [%Zu bytes]",
71                                skb->len - sizeof(_arph));
72                 return;
73         }
74         nf_log_buf_add(m, " MACSRC=%pM IPSRC=%pI4 MACDST=%pM IPDST=%pI4",
75                        ap->mac_src, ap->ip_src, ap->mac_dst, ap->ip_dst);
76 }
77
78 static void nf_log_arp_packet(struct net *net, u_int8_t pf,
79                               unsigned int hooknum, const struct sk_buff *skb,
80                               const struct net_device *in,
81                               const struct net_device *out,
82                               const struct nf_loginfo *loginfo,
83                               const char *prefix)
84 {
85         struct nf_log_buf *m;
86
87         /* FIXME: Disabled from containers until syslog ns is supported */
88         if (!net_eq(net, &init_net))
89                 return;
90
91         m = nf_log_buf_open();
92
93         if (!loginfo)
94                 loginfo = &default_loginfo;
95
96         nf_log_dump_packet_common(m, pf, hooknum, skb, in, out, loginfo,
97                                   prefix);
98         dump_arp_packet(m, loginfo, skb, 0);
99
100         nf_log_buf_close(m);
101 }
102
103 static struct nf_logger nf_arp_logger __read_mostly = {
104         .name           = "nf_log_arp",
105         .type           = NF_LOG_TYPE_LOG,
106         .logfn          = nf_log_arp_packet,
107         .me             = THIS_MODULE,
108 };
109
110 static int __net_init nf_log_arp_net_init(struct net *net)
111 {
112         nf_log_set(net, NFPROTO_ARP, &nf_arp_logger);
113         return 0;
114 }
115
116 static void __net_exit nf_log_arp_net_exit(struct net *net)
117 {
118         nf_log_unset(net, &nf_arp_logger);
119 }
120
121 static struct pernet_operations nf_log_arp_net_ops = {
122         .init = nf_log_arp_net_init,
123         .exit = nf_log_arp_net_exit,
124 };
125
126 static int __init nf_log_arp_init(void)
127 {
128         int ret;
129
130         ret = register_pernet_subsys(&nf_log_arp_net_ops);
131         if (ret < 0)
132                 return ret;
133
134         ret = nf_log_register(NFPROTO_ARP, &nf_arp_logger);
135         if (ret < 0) {
136                 pr_err("failed to register logger\n");
137                 goto err1;
138         }
139
140         return 0;
141
142 err1:
143         unregister_pernet_subsys(&nf_log_arp_net_ops);
144         return ret;
145 }
146
147 static void __exit nf_log_arp_exit(void)
148 {
149         unregister_pernet_subsys(&nf_log_arp_net_ops);
150         nf_log_unregister(&nf_arp_logger);
151 }
152
153 module_init(nf_log_arp_init);
154 module_exit(nf_log_arp_exit);
155
156 MODULE_AUTHOR("Pablo Neira Ayuso <pablo@netfilter.org>");
157 MODULE_DESCRIPTION("Netfilter ARP packet logging");
158 MODULE_LICENSE("GPL");
159 MODULE_ALIAS_NF_LOGGER(3, 0);