]>
Commit | Line | Data |
---|---|---|
09c434b8 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
1da177e4 LT |
2 | /* |
3 | * ebt_log | |
4 | * | |
5 | * Authors: | |
6 | * Bart De Schuymer <bdschuym@pandora.be> | |
d5228a4f | 7 | * Harald Welte <laforge@netfilter.org> |
1da177e4 LT |
8 | * |
9 | * April, 2002 | |
10 | * | |
11 | */ | |
1da177e4 LT |
12 | #include <linux/module.h> |
13 | #include <linux/ip.h> | |
2e4e6a17 | 14 | #include <linux/in.h> |
1da177e4 LT |
15 | #include <linux/if_arp.h> |
16 | #include <linux/spinlock.h> | |
f01ffbd6 | 17 | #include <net/netfilter/nf_log.h> |
93f65158 KT |
18 | #include <linux/ipv6.h> |
19 | #include <net/ipv6.h> | |
20 | #include <linux/in6.h> | |
18219d3f JE |
21 | #include <linux/netfilter/x_tables.h> |
22 | #include <linux/netfilter_bridge/ebtables.h> | |
23 | #include <linux/netfilter_bridge/ebt_log.h> | |
24 | #include <linux/netfilter.h> | |
1da177e4 LT |
25 | |
26 | static DEFINE_SPINLOCK(ebt_log_lock); | |
27 | ||
135367b8 | 28 | static int ebt_log_tg_check(const struct xt_tgchk_param *par) |
1da177e4 | 29 | { |
af5d6dc2 | 30 | struct ebt_log_info *info = par->targinfo; |
1da177e4 | 31 | |
1da177e4 | 32 | if (info->bitmask & ~EBT_LOG_MASK) |
d6b00a53 | 33 | return -EINVAL; |
1da177e4 | 34 | if (info->loglevel >= 8) |
d6b00a53 | 35 | return -EINVAL; |
1da177e4 | 36 | info->prefix[EBT_LOG_PREFIX_SIZE - 1] = '\0'; |
d6b00a53 | 37 | return 0; |
1da177e4 LT |
38 | } |
39 | ||
abcdd9a6 | 40 | struct tcpudphdr { |
47c183fa AV |
41 | __be16 src; |
42 | __be16 dst; | |
1da177e4 LT |
43 | }; |
44 | ||
abcdd9a6 | 45 | struct arppayload { |
1da177e4 LT |
46 | unsigned char mac_src[ETH_ALEN]; |
47 | unsigned char ip_src[4]; | |
48 | unsigned char mac_dst[ETH_ALEN]; | |
49 | unsigned char ip_dst[4]; | |
50 | }; | |
51 | ||
93f65158 KT |
52 | static void |
53 | print_ports(const struct sk_buff *skb, uint8_t protocol, int offset) | |
54 | { | |
55 | if (protocol == IPPROTO_TCP || | |
56 | protocol == IPPROTO_UDP || | |
57 | protocol == IPPROTO_UDPLITE || | |
58 | protocol == IPPROTO_SCTP || | |
59 | protocol == IPPROTO_DCCP) { | |
60 | const struct tcpudphdr *pptr; | |
61 | struct tcpudphdr _ports; | |
62 | ||
63 | pptr = skb_header_pointer(skb, offset, | |
64 | sizeof(_ports), &_ports); | |
65 | if (pptr == NULL) { | |
13fa745d | 66 | pr_cont(" INCOMPLETE TCP/UDP header"); |
93f65158 KT |
67 | return; |
68 | } | |
13fa745d | 69 | pr_cont(" SPT=%u DPT=%u", ntohs(pptr->src), ntohs(pptr->dst)); |
93f65158 KT |
70 | } |
71 | } | |
72 | ||
d5228a4f | 73 | static void |
8cdb46da HS |
74 | ebt_log_packet(struct net *net, u_int8_t pf, unsigned int hooknum, |
75 | const struct sk_buff *skb, const struct net_device *in, | |
76 | const struct net_device *out, const struct nf_loginfo *loginfo, | |
77 | const char *prefix) | |
1da177e4 | 78 | { |
d5228a4f | 79 | unsigned int bitmask; |
7d278924 G |
80 | |
81 | /* FIXME: Disabled from containers until syslog ns is supported */ | |
2851940f | 82 | if (!net_eq(net, &init_net) && !sysctl_nf_log_all_netns) |
7d278924 | 83 | return; |
1da177e4 | 84 | |
1da177e4 | 85 | spin_lock_bh(&ebt_log_lock); |
16af511a | 86 | printk(KERN_SOH "%c%s IN=%s OUT=%s MAC source = %pM MAC dest = %pM proto = 0x%04x", |
be39ee11 TK |
87 | '0' + loginfo->u.log.level, prefix, |
88 | in ? in->name : "", out ? out->name : "", | |
89 | eth_hdr(skb)->h_source, eth_hdr(skb)->h_dest, | |
90 | ntohs(eth_hdr(skb)->h_proto)); | |
1da177e4 | 91 | |
d5228a4f BDS |
92 | if (loginfo->type == NF_LOG_TYPE_LOG) |
93 | bitmask = loginfo->u.log.logflags; | |
94 | else | |
ff107d27 | 95 | bitmask = NF_LOG_DEFAULT_MASK; |
d5228a4f BDS |
96 | |
97 | if ((bitmask & EBT_LOG_IP) && eth_hdr(skb)->h_proto == | |
31a5b837 | 98 | htons(ETH_P_IP)) { |
abfdf1c4 JE |
99 | const struct iphdr *ih; |
100 | struct iphdr _iph; | |
1da177e4 LT |
101 | |
102 | ih = skb_header_pointer(skb, 0, sizeof(_iph), &_iph); | |
103 | if (ih == NULL) { | |
13fa745d | 104 | pr_cont(" INCOMPLETE IP header"); |
1da177e4 LT |
105 | goto out; |
106 | } | |
13fa745d JP |
107 | pr_cont(" IP SRC=%pI4 IP DST=%pI4, IP tos=0x%02X, IP proto=%d", |
108 | &ih->saddr, &ih->daddr, ih->tos, ih->protocol); | |
93f65158 KT |
109 | print_ports(skb, ih->protocol, ih->ihl*4); |
110 | goto out; | |
111 | } | |
112 | ||
e6373c4c | 113 | #if IS_ENABLED(CONFIG_BRIDGE_EBT_IP6) |
93f65158 KT |
114 | if ((bitmask & EBT_LOG_IP6) && eth_hdr(skb)->h_proto == |
115 | htons(ETH_P_IPV6)) { | |
116 | const struct ipv6hdr *ih; | |
117 | struct ipv6hdr _iph; | |
118 | uint8_t nexthdr; | |
75f2811c | 119 | __be16 frag_off; |
93f65158 KT |
120 | int offset_ph; |
121 | ||
122 | ih = skb_header_pointer(skb, 0, sizeof(_iph), &_iph); | |
123 | if (ih == NULL) { | |
13fa745d | 124 | pr_cont(" INCOMPLETE IPv6 header"); |
93f65158 | 125 | goto out; |
1da177e4 | 126 | } |
13fa745d JP |
127 | pr_cont(" IPv6 SRC=%pI6 IPv6 DST=%pI6, IPv6 priority=0x%01X, Next Header=%d", |
128 | &ih->saddr, &ih->daddr, ih->priority, ih->nexthdr); | |
93f65158 | 129 | nexthdr = ih->nexthdr; |
75f2811c | 130 | offset_ph = ipv6_skip_exthdr(skb, sizeof(_iph), &nexthdr, &frag_off); |
93f65158 KT |
131 | if (offset_ph == -1) |
132 | goto out; | |
133 | print_ports(skb, nexthdr, offset_ph); | |
1da177e4 LT |
134 | goto out; |
135 | } | |
f586287e | 136 | #endif |
1da177e4 | 137 | |
d5228a4f | 138 | if ((bitmask & EBT_LOG_ARP) && |
1da177e4 LT |
139 | ((eth_hdr(skb)->h_proto == htons(ETH_P_ARP)) || |
140 | (eth_hdr(skb)->h_proto == htons(ETH_P_RARP)))) { | |
abfdf1c4 JE |
141 | const struct arphdr *ah; |
142 | struct arphdr _arph; | |
1da177e4 LT |
143 | |
144 | ah = skb_header_pointer(skb, 0, sizeof(_arph), &_arph); | |
145 | if (ah == NULL) { | |
13fa745d | 146 | pr_cont(" INCOMPLETE ARP header"); |
1da177e4 LT |
147 | goto out; |
148 | } | |
13fa745d JP |
149 | pr_cont(" ARP HTYPE=%d, PTYPE=0x%04x, OPCODE=%d", |
150 | ntohs(ah->ar_hrd), ntohs(ah->ar_pro), | |
151 | ntohs(ah->ar_op)); | |
1da177e4 LT |
152 | |
153 | /* If it's for Ethernet and the lengths are OK, | |
7f495ad9 IM |
154 | * then log the ARP payload |
155 | */ | |
1da177e4 LT |
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) { | |
13fa745d | 165 | pr_cont(" INCOMPLETE ARP payload"); |
1da177e4 LT |
166 | goto out; |
167 | } | |
13fa745d JP |
168 | pr_cont(" ARP MAC SRC=%pM ARP IP SRC=%pI4 ARP MAC DST=%pM ARP IP DST=%pI4", |
169 | ap->mac_src, ap->ip_src, | |
170 | ap->mac_dst, ap->ip_dst); | |
1da177e4 LT |
171 | } |
172 | } | |
173 | out: | |
13fa745d | 174 | pr_cont("\n"); |
1da177e4 | 175 | spin_unlock_bh(&ebt_log_lock); |
d5228a4f BDS |
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; |
613dbd95 | 183 | struct net *net = xt_net(par); |
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 | ||
960649d1 PNA |
189 | /* Remember that we have to use ebt_log_packet() not to break backward |
190 | * compatibility. We cannot use the default bridge packet logger via | |
191 | * nf_log_packet() with NFT_LOG_TYPE_LOG here. --Pablo | |
192 | */ | |
bafac2a5 | 193 | if (info->bitmask & EBT_LOG_NFLOG) |
613dbd95 PNA |
194 | nf_log_packet(net, NFPROTO_BRIDGE, xt_hooknum(par), skb, |
195 | xt_in(par), xt_out(par), &li, "%s", | |
196 | info->prefix); | |
bafac2a5 | 197 | else |
613dbd95 PNA |
198 | ebt_log_packet(net, NFPROTO_BRIDGE, xt_hooknum(par), skb, |
199 | xt_in(par), xt_out(par), &li, info->prefix); | |
0ac6ab1f | 200 | return EBT_CONTINUE; |
1da177e4 LT |
201 | } |
202 | ||
043ef46c JE |
203 | static struct xt_target ebt_log_tg_reg __read_mostly = { |
204 | .name = "log", | |
001a18d3 JE |
205 | .revision = 0, |
206 | .family = NFPROTO_BRIDGE, | |
2d06d4a5 JE |
207 | .target = ebt_log_tg, |
208 | .checkentry = ebt_log_tg_check, | |
fc0e3df4 | 209 | .targetsize = sizeof(struct ebt_log_info), |
1da177e4 LT |
210 | .me = THIS_MODULE, |
211 | }; | |
212 | ||
65b4b4e8 | 213 | static int __init ebt_log_init(void) |
1da177e4 | 214 | { |
960649d1 | 215 | return xt_register_target(&ebt_log_tg_reg); |
1da177e4 LT |
216 | } |
217 | ||
65b4b4e8 | 218 | static void __exit ebt_log_fini(void) |
1da177e4 | 219 | { |
043ef46c | 220 | xt_unregister_target(&ebt_log_tg_reg); |
1da177e4 LT |
221 | } |
222 | ||
65b4b4e8 AM |
223 | module_init(ebt_log_init); |
224 | module_exit(ebt_log_fini); | |
f776c4cd | 225 | MODULE_DESCRIPTION("Ebtables: Packet logging to syslog"); |
1da177e4 | 226 | MODULE_LICENSE("GPL"); |