Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * ebt_log | |
3 | * | |
4 | * Authors: | |
5 | * Bart De Schuymer <bdschuym@pandora.be> | |
d5228a4f | 6 | * Harald Welte <laforge@netfilter.org> |
1da177e4 LT |
7 | * |
8 | * April, 2002 | |
9 | * | |
10 | */ | |
1da177e4 LT |
11 | #include <linux/module.h> |
12 | #include <linux/ip.h> | |
2e4e6a17 | 13 | #include <linux/in.h> |
1da177e4 LT |
14 | #include <linux/if_arp.h> |
15 | #include <linux/spinlock.h> | |
f01ffbd6 | 16 | #include <net/netfilter/nf_log.h> |
93f65158 KT |
17 | #include <linux/ipv6.h> |
18 | #include <net/ipv6.h> | |
19 | #include <linux/in6.h> | |
18219d3f JE |
20 | #include <linux/netfilter/x_tables.h> |
21 | #include <linux/netfilter_bridge/ebtables.h> | |
22 | #include <linux/netfilter_bridge/ebt_log.h> | |
23 | #include <linux/netfilter.h> | |
1da177e4 LT |
24 | |
25 | static DEFINE_SPINLOCK(ebt_log_lock); | |
26 | ||
135367b8 | 27 | static int ebt_log_tg_check(const struct xt_tgchk_param *par) |
1da177e4 | 28 | { |
af5d6dc2 | 29 | struct ebt_log_info *info = par->targinfo; |
1da177e4 | 30 | |
1da177e4 | 31 | if (info->bitmask & ~EBT_LOG_MASK) |
d6b00a53 | 32 | return -EINVAL; |
1da177e4 | 33 | if (info->loglevel >= 8) |
d6b00a53 | 34 | return -EINVAL; |
1da177e4 | 35 | info->prefix[EBT_LOG_PREFIX_SIZE - 1] = '\0'; |
d6b00a53 | 36 | return 0; |
1da177e4 LT |
37 | } |
38 | ||
39 | struct tcpudphdr | |
40 | { | |
47c183fa AV |
41 | __be16 src; |
42 | __be16 dst; | |
1da177e4 LT |
43 | }; |
44 | ||
45 | struct arppayload | |
46 | { | |
47 | unsigned char mac_src[ETH_ALEN]; | |
48 | unsigned char ip_src[4]; | |
49 | unsigned char mac_dst[ETH_ALEN]; | |
50 | unsigned char ip_dst[4]; | |
51 | }; | |
52 | ||
93f65158 KT |
53 | static void |
54 | print_ports(const struct sk_buff *skb, uint8_t protocol, int offset) | |
55 | { | |
56 | if (protocol == IPPROTO_TCP || | |
57 | protocol == IPPROTO_UDP || | |
58 | protocol == IPPROTO_UDPLITE || | |
59 | protocol == IPPROTO_SCTP || | |
60 | protocol == IPPROTO_DCCP) { | |
61 | const struct tcpudphdr *pptr; | |
62 | struct tcpudphdr _ports; | |
63 | ||
64 | pptr = skb_header_pointer(skb, offset, | |
65 | sizeof(_ports), &_ports); | |
66 | if (pptr == NULL) { | |
67 | printk(" INCOMPLETE TCP/UDP header"); | |
68 | return; | |
69 | } | |
70 | printk(" SPT=%u DPT=%u", ntohs(pptr->src), ntohs(pptr->dst)); | |
71 | } | |
72 | } | |
73 | ||
d5228a4f | 74 | static void |
8cdb46da HS |
75 | ebt_log_packet(struct net *net, u_int8_t pf, unsigned int hooknum, |
76 | const struct sk_buff *skb, const struct net_device *in, | |
77 | const struct net_device *out, const struct nf_loginfo *loginfo, | |
78 | const char *prefix) | |
1da177e4 | 79 | { |
d5228a4f | 80 | unsigned int bitmask; |
7d278924 G |
81 | |
82 | /* FIXME: Disabled from containers until syslog ns is supported */ | |
83 | if (!net_eq(net, &init_net)) | |
84 | return; | |
1da177e4 | 85 | |
1da177e4 | 86 | spin_lock_bh(&ebt_log_lock); |
16af511a | 87 | printk(KERN_SOH "%c%s IN=%s OUT=%s MAC source = %pM MAC dest = %pM proto = 0x%04x", |
be39ee11 TK |
88 | '0' + loginfo->u.log.level, prefix, |
89 | in ? in->name : "", out ? out->name : "", | |
90 | eth_hdr(skb)->h_source, eth_hdr(skb)->h_dest, | |
91 | ntohs(eth_hdr(skb)->h_proto)); | |
1da177e4 | 92 | |
d5228a4f BDS |
93 | if (loginfo->type == NF_LOG_TYPE_LOG) |
94 | bitmask = loginfo->u.log.logflags; | |
95 | else | |
96 | bitmask = NF_LOG_MASK; | |
97 | ||
98 | if ((bitmask & EBT_LOG_IP) && eth_hdr(skb)->h_proto == | |
1da177e4 | 99 | htons(ETH_P_IP)){ |
abfdf1c4 JE |
100 | const struct iphdr *ih; |
101 | struct iphdr _iph; | |
1da177e4 LT |
102 | |
103 | ih = skb_header_pointer(skb, 0, sizeof(_iph), &_iph); | |
104 | if (ih == NULL) { | |
105 | printk(" INCOMPLETE IP header"); | |
106 | goto out; | |
107 | } | |
21454aaa HH |
108 | printk(" IP SRC=%pI4 IP DST=%pI4, IP tos=0x%02X, IP proto=%d", |
109 | &ih->saddr, &ih->daddr, ih->tos, ih->protocol); | |
93f65158 KT |
110 | print_ports(skb, ih->protocol, ih->ihl*4); |
111 | goto out; | |
112 | } | |
113 | ||
e6373c4c | 114 | #if IS_ENABLED(CONFIG_BRIDGE_EBT_IP6) |
93f65158 KT |
115 | if ((bitmask & EBT_LOG_IP6) && eth_hdr(skb)->h_proto == |
116 | htons(ETH_P_IPV6)) { | |
117 | const struct ipv6hdr *ih; | |
118 | struct ipv6hdr _iph; | |
119 | uint8_t nexthdr; | |
75f2811c | 120 | __be16 frag_off; |
93f65158 KT |
121 | int offset_ph; |
122 | ||
123 | ih = skb_header_pointer(skb, 0, sizeof(_iph), &_iph); | |
124 | if (ih == NULL) { | |
125 | printk(" INCOMPLETE IPv6 header"); | |
126 | goto out; | |
1da177e4 | 127 | } |
5b095d98 | 128 | printk(" IPv6 SRC=%pI6 IPv6 DST=%pI6, IPv6 priority=0x%01X, Next Header=%d", |
b189db5d | 129 | &ih->saddr, &ih->daddr, ih->priority, ih->nexthdr); |
93f65158 | 130 | nexthdr = ih->nexthdr; |
75f2811c | 131 | offset_ph = ipv6_skip_exthdr(skb, sizeof(_iph), &nexthdr, &frag_off); |
93f65158 KT |
132 | if (offset_ph == -1) |
133 | goto out; | |
134 | print_ports(skb, nexthdr, offset_ph); | |
1da177e4 LT |
135 | goto out; |
136 | } | |
f586287e | 137 | #endif |
1da177e4 | 138 | |
d5228a4f | 139 | if ((bitmask & EBT_LOG_ARP) && |
1da177e4 LT |
140 | ((eth_hdr(skb)->h_proto == htons(ETH_P_ARP)) || |
141 | (eth_hdr(skb)->h_proto == htons(ETH_P_RARP)))) { | |
abfdf1c4 JE |
142 | const struct arphdr *ah; |
143 | struct arphdr _arph; | |
1da177e4 LT |
144 | |
145 | ah = skb_header_pointer(skb, 0, sizeof(_arph), &_arph); | |
146 | if (ah == NULL) { | |
147 | printk(" INCOMPLETE ARP header"); | |
148 | goto out; | |
149 | } | |
150 | printk(" ARP HTYPE=%d, PTYPE=0x%04x, OPCODE=%d", | |
151 | ntohs(ah->ar_hrd), ntohs(ah->ar_pro), | |
152 | ntohs(ah->ar_op)); | |
153 | ||
154 | /* If it's for Ethernet and the lengths are OK, | |
155 | * then log the ARP payload */ | |
156 | if (ah->ar_hrd == htons(1) && | |
157 | ah->ar_hln == ETH_ALEN && | |
47c183fa | 158 | ah->ar_pln == sizeof(__be32)) { |
abfdf1c4 JE |
159 | const struct arppayload *ap; |
160 | struct arppayload _arpp; | |
1da177e4 | 161 | |
85c1937b | 162 | ap = skb_header_pointer(skb, sizeof(_arph), |
1da177e4 LT |
163 | sizeof(_arpp), &_arpp); |
164 | if (ap == NULL) { | |
165 | printk(" INCOMPLETE ARP payload"); | |
166 | goto out; | |
167 | } | |
be39ee11 TK |
168 | printk(" ARP MAC SRC=%pM ARP IP SRC=%pI4 ARP MAC DST=%pM ARP IP DST=%pI4", |
169 | ap->mac_src, ap->ip_src, ap->mac_dst, ap->ip_dst); | |
1da177e4 LT |
170 | } |
171 | } | |
172 | out: | |
173 | printk("\n"); | |
174 | spin_unlock_bh(&ebt_log_lock); | |
d5228a4f BDS |
175 | |
176 | } | |
177 | ||
2d06d4a5 | 178 | static unsigned int |
4b560b44 | 179 | ebt_log_tg(struct sk_buff *skb, const struct xt_action_param *par) |
d5228a4f | 180 | { |
7eb35586 | 181 | const struct ebt_log_info *info = par->targinfo; |
d5228a4f | 182 | struct nf_loginfo li; |
30e0c6a6 | 183 | struct net *net = dev_net(par->in ? par->in : par->out); |
d5228a4f BDS |
184 | |
185 | li.type = NF_LOG_TYPE_LOG; | |
186 | li.u.log.level = info->loglevel; | |
187 | li.u.log.logflags = info->bitmask; | |
188 | ||
bafac2a5 | 189 | if (info->bitmask & EBT_LOG_NFLOG) |
30e0c6a6 G |
190 | nf_log_packet(net, NFPROTO_BRIDGE, par->hooknum, skb, |
191 | par->in, par->out, &li, "%s", info->prefix); | |
bafac2a5 | 192 | else |
8cdb46da | 193 | ebt_log_packet(net, NFPROTO_BRIDGE, par->hooknum, skb, par->in, |
30e0c6a6 | 194 | par->out, &li, info->prefix); |
0ac6ab1f | 195 | return EBT_CONTINUE; |
1da177e4 LT |
196 | } |
197 | ||
043ef46c JE |
198 | static struct xt_target ebt_log_tg_reg __read_mostly = { |
199 | .name = "log", | |
001a18d3 JE |
200 | .revision = 0, |
201 | .family = NFPROTO_BRIDGE, | |
2d06d4a5 JE |
202 | .target = ebt_log_tg, |
203 | .checkentry = ebt_log_tg_check, | |
fc0e3df4 | 204 | .targetsize = sizeof(struct ebt_log_info), |
1da177e4 LT |
205 | .me = THIS_MODULE, |
206 | }; | |
207 | ||
704b3ea3 | 208 | static struct nf_logger ebt_log_logger __read_mostly = { |
d5228a4f BDS |
209 | .name = "ebt_log", |
210 | .logfn = &ebt_log_packet, | |
211 | .me = THIS_MODULE, | |
212 | }; | |
213 | ||
7d278924 G |
214 | static int __net_init ebt_log_net_init(struct net *net) |
215 | { | |
216 | nf_log_set(net, NFPROTO_BRIDGE, &ebt_log_logger); | |
217 | return 0; | |
218 | } | |
219 | ||
220 | static void __net_exit ebt_log_net_fini(struct net *net) | |
221 | { | |
222 | nf_log_unset(net, &ebt_log_logger); | |
223 | } | |
224 | ||
225 | static struct pernet_operations ebt_log_net_ops = { | |
226 | .init = ebt_log_net_init, | |
227 | .exit = ebt_log_net_fini, | |
228 | }; | |
229 | ||
65b4b4e8 | 230 | static int __init ebt_log_init(void) |
1da177e4 | 231 | { |
d5228a4f BDS |
232 | int ret; |
233 | ||
7d278924 G |
234 | ret = register_pernet_subsys(&ebt_log_net_ops); |
235 | if (ret < 0) | |
236 | goto err_pernet; | |
237 | ||
043ef46c | 238 | ret = xt_register_target(&ebt_log_tg_reg); |
d5228a4f | 239 | if (ret < 0) |
7d278924 G |
240 | goto err_target; |
241 | ||
ee999d8b | 242 | nf_log_register(NFPROTO_BRIDGE, &ebt_log_logger); |
7d278924 G |
243 | |
244 | return ret; | |
245 | ||
246 | err_target: | |
247 | unregister_pernet_subsys(&ebt_log_net_ops); | |
248 | err_pernet: | |
249 | return ret; | |
1da177e4 LT |
250 | } |
251 | ||
65b4b4e8 | 252 | static void __exit ebt_log_fini(void) |
1da177e4 | 253 | { |
7d278924 | 254 | unregister_pernet_subsys(&ebt_log_net_ops); |
e92ad99c | 255 | nf_log_unregister(&ebt_log_logger); |
043ef46c | 256 | xt_unregister_target(&ebt_log_tg_reg); |
1da177e4 LT |
257 | } |
258 | ||
65b4b4e8 AM |
259 | module_init(ebt_log_init); |
260 | module_exit(ebt_log_fini); | |
f776c4cd | 261 | MODULE_DESCRIPTION("Ebtables: Packet logging to syslog"); |
1da177e4 | 262 | MODULE_LICENSE("GPL"); |