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