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