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 |
76108cea | 75 | ebt_log_packet(u_int8_t pf, unsigned int hooknum, |
d5228a4f BDS |
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; |
1da177e4 | 81 | |
1da177e4 | 82 | spin_lock_bh(&ebt_log_lock); |
16af511a | 83 | printk(KERN_SOH "%c%s IN=%s OUT=%s MAC source = %pM MAC dest = %pM proto = 0x%04x", |
be39ee11 TK |
84 | '0' + loginfo->u.log.level, prefix, |
85 | in ? in->name : "", out ? out->name : "", | |
86 | eth_hdr(skb)->h_source, eth_hdr(skb)->h_dest, | |
87 | ntohs(eth_hdr(skb)->h_proto)); | |
1da177e4 | 88 | |
d5228a4f BDS |
89 | if (loginfo->type == NF_LOG_TYPE_LOG) |
90 | bitmask = loginfo->u.log.logflags; | |
91 | else | |
92 | bitmask = NF_LOG_MASK; | |
93 | ||
94 | if ((bitmask & EBT_LOG_IP) && eth_hdr(skb)->h_proto == | |
1da177e4 | 95 | htons(ETH_P_IP)){ |
abfdf1c4 JE |
96 | const struct iphdr *ih; |
97 | struct iphdr _iph; | |
1da177e4 LT |
98 | |
99 | ih = skb_header_pointer(skb, 0, sizeof(_iph), &_iph); | |
100 | if (ih == NULL) { | |
101 | printk(" INCOMPLETE IP header"); | |
102 | goto out; | |
103 | } | |
21454aaa HH |
104 | printk(" IP SRC=%pI4 IP DST=%pI4, IP tos=0x%02X, IP proto=%d", |
105 | &ih->saddr, &ih->daddr, ih->tos, ih->protocol); | |
93f65158 KT |
106 | print_ports(skb, ih->protocol, ih->ihl*4); |
107 | goto out; | |
108 | } | |
109 | ||
e6373c4c | 110 | #if IS_ENABLED(CONFIG_BRIDGE_EBT_IP6) |
93f65158 KT |
111 | if ((bitmask & EBT_LOG_IP6) && eth_hdr(skb)->h_proto == |
112 | htons(ETH_P_IPV6)) { | |
113 | const struct ipv6hdr *ih; | |
114 | struct ipv6hdr _iph; | |
115 | uint8_t nexthdr; | |
75f2811c | 116 | __be16 frag_off; |
93f65158 KT |
117 | int offset_ph; |
118 | ||
119 | ih = skb_header_pointer(skb, 0, sizeof(_iph), &_iph); | |
120 | if (ih == NULL) { | |
121 | printk(" INCOMPLETE IPv6 header"); | |
122 | goto out; | |
1da177e4 | 123 | } |
5b095d98 | 124 | printk(" IPv6 SRC=%pI6 IPv6 DST=%pI6, IPv6 priority=0x%01X, Next Header=%d", |
b189db5d | 125 | &ih->saddr, &ih->daddr, ih->priority, ih->nexthdr); |
93f65158 | 126 | nexthdr = ih->nexthdr; |
75f2811c | 127 | offset_ph = ipv6_skip_exthdr(skb, sizeof(_iph), &nexthdr, &frag_off); |
93f65158 KT |
128 | if (offset_ph == -1) |
129 | goto out; | |
130 | print_ports(skb, nexthdr, offset_ph); | |
1da177e4 LT |
131 | goto out; |
132 | } | |
f586287e | 133 | #endif |
1da177e4 | 134 | |
d5228a4f | 135 | if ((bitmask & EBT_LOG_ARP) && |
1da177e4 LT |
136 | ((eth_hdr(skb)->h_proto == htons(ETH_P_ARP)) || |
137 | (eth_hdr(skb)->h_proto == htons(ETH_P_RARP)))) { | |
abfdf1c4 JE |
138 | const struct arphdr *ah; |
139 | struct arphdr _arph; | |
1da177e4 LT |
140 | |
141 | ah = skb_header_pointer(skb, 0, sizeof(_arph), &_arph); | |
142 | if (ah == NULL) { | |
143 | printk(" INCOMPLETE ARP header"); | |
144 | goto out; | |
145 | } | |
146 | printk(" ARP HTYPE=%d, PTYPE=0x%04x, OPCODE=%d", | |
147 | ntohs(ah->ar_hrd), ntohs(ah->ar_pro), | |
148 | ntohs(ah->ar_op)); | |
149 | ||
150 | /* If it's for Ethernet and the lengths are OK, | |
151 | * then log the ARP payload */ | |
152 | if (ah->ar_hrd == htons(1) && | |
153 | ah->ar_hln == ETH_ALEN && | |
47c183fa | 154 | ah->ar_pln == sizeof(__be32)) { |
abfdf1c4 JE |
155 | const struct arppayload *ap; |
156 | struct arppayload _arpp; | |
1da177e4 | 157 | |
85c1937b | 158 | ap = skb_header_pointer(skb, sizeof(_arph), |
1da177e4 LT |
159 | sizeof(_arpp), &_arpp); |
160 | if (ap == NULL) { | |
161 | printk(" INCOMPLETE ARP payload"); | |
162 | goto out; | |
163 | } | |
be39ee11 TK |
164 | printk(" ARP MAC SRC=%pM ARP IP SRC=%pI4 ARP MAC DST=%pM ARP IP DST=%pI4", |
165 | ap->mac_src, ap->ip_src, ap->mac_dst, ap->ip_dst); | |
1da177e4 LT |
166 | } |
167 | } | |
168 | out: | |
169 | printk("\n"); | |
170 | spin_unlock_bh(&ebt_log_lock); | |
d5228a4f BDS |
171 | |
172 | } | |
173 | ||
2d06d4a5 | 174 | static unsigned int |
4b560b44 | 175 | ebt_log_tg(struct sk_buff *skb, const struct xt_action_param *par) |
d5228a4f | 176 | { |
7eb35586 | 177 | const struct ebt_log_info *info = par->targinfo; |
d5228a4f BDS |
178 | struct nf_loginfo li; |
179 | ||
180 | li.type = NF_LOG_TYPE_LOG; | |
181 | li.u.log.level = info->loglevel; | |
182 | li.u.log.logflags = info->bitmask; | |
183 | ||
bafac2a5 | 184 | if (info->bitmask & EBT_LOG_NFLOG) |
7eb35586 JE |
185 | nf_log_packet(NFPROTO_BRIDGE, par->hooknum, skb, par->in, |
186 | par->out, &li, "%s", info->prefix); | |
bafac2a5 | 187 | else |
7eb35586 JE |
188 | ebt_log_packet(NFPROTO_BRIDGE, par->hooknum, skb, par->in, |
189 | par->out, &li, info->prefix); | |
0ac6ab1f | 190 | return EBT_CONTINUE; |
1da177e4 LT |
191 | } |
192 | ||
043ef46c JE |
193 | static struct xt_target ebt_log_tg_reg __read_mostly = { |
194 | .name = "log", | |
001a18d3 JE |
195 | .revision = 0, |
196 | .family = NFPROTO_BRIDGE, | |
2d06d4a5 JE |
197 | .target = ebt_log_tg, |
198 | .checkentry = ebt_log_tg_check, | |
fc0e3df4 | 199 | .targetsize = sizeof(struct ebt_log_info), |
1da177e4 LT |
200 | .me = THIS_MODULE, |
201 | }; | |
202 | ||
704b3ea3 | 203 | static struct nf_logger ebt_log_logger __read_mostly = { |
d5228a4f BDS |
204 | .name = "ebt_log", |
205 | .logfn = &ebt_log_packet, | |
206 | .me = THIS_MODULE, | |
207 | }; | |
208 | ||
65b4b4e8 | 209 | static int __init ebt_log_init(void) |
1da177e4 | 210 | { |
d5228a4f BDS |
211 | int ret; |
212 | ||
043ef46c | 213 | ret = xt_register_target(&ebt_log_tg_reg); |
d5228a4f BDS |
214 | if (ret < 0) |
215 | return ret; | |
ee999d8b | 216 | nf_log_register(NFPROTO_BRIDGE, &ebt_log_logger); |
7e2acc7e | 217 | return 0; |
1da177e4 LT |
218 | } |
219 | ||
65b4b4e8 | 220 | static void __exit ebt_log_fini(void) |
1da177e4 | 221 | { |
e92ad99c | 222 | nf_log_unregister(&ebt_log_logger); |
043ef46c | 223 | xt_unregister_target(&ebt_log_tg_reg); |
1da177e4 LT |
224 | } |
225 | ||
65b4b4e8 AM |
226 | module_init(ebt_log_init); |
227 | module_exit(ebt_log_fini); | |
f776c4cd | 228 | MODULE_DESCRIPTION("Ebtables: Packet logging to syslog"); |
1da177e4 | 229 | MODULE_LICENSE("GPL"); |