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 | ||
abcdd9a6 | 39 | struct tcpudphdr { |
47c183fa AV |
40 | __be16 src; |
41 | __be16 dst; | |
1da177e4 LT |
42 | }; |
43 | ||
abcdd9a6 | 44 | struct arppayload { |
1da177e4 LT |
45 | unsigned char mac_src[ETH_ALEN]; |
46 | unsigned char ip_src[4]; | |
47 | unsigned char mac_dst[ETH_ALEN]; | |
48 | unsigned char ip_dst[4]; | |
49 | }; | |
50 | ||
93f65158 KT |
51 | static void |
52 | print_ports(const struct sk_buff *skb, uint8_t protocol, int offset) | |
53 | { | |
54 | if (protocol == IPPROTO_TCP || | |
55 | protocol == IPPROTO_UDP || | |
56 | protocol == IPPROTO_UDPLITE || | |
57 | protocol == IPPROTO_SCTP || | |
58 | protocol == IPPROTO_DCCP) { | |
59 | const struct tcpudphdr *pptr; | |
60 | struct tcpudphdr _ports; | |
61 | ||
62 | pptr = skb_header_pointer(skb, offset, | |
63 | sizeof(_ports), &_ports); | |
64 | if (pptr == NULL) { | |
65 | printk(" INCOMPLETE TCP/UDP header"); | |
66 | return; | |
67 | } | |
68 | printk(" SPT=%u DPT=%u", ntohs(pptr->src), ntohs(pptr->dst)); | |
69 | } | |
70 | } | |
71 | ||
d5228a4f | 72 | static void |
8cdb46da HS |
73 | ebt_log_packet(struct net *net, u_int8_t pf, unsigned int hooknum, |
74 | const struct sk_buff *skb, const struct net_device *in, | |
75 | const struct net_device *out, const struct nf_loginfo *loginfo, | |
76 | const char *prefix) | |
1da177e4 | 77 | { |
d5228a4f | 78 | unsigned int bitmask; |
7d278924 G |
79 | |
80 | /* FIXME: Disabled from containers until syslog ns is supported */ | |
81 | if (!net_eq(net, &init_net)) | |
82 | return; | |
1da177e4 | 83 | |
1da177e4 | 84 | spin_lock_bh(&ebt_log_lock); |
16af511a | 85 | printk(KERN_SOH "%c%s IN=%s OUT=%s MAC source = %pM MAC dest = %pM proto = 0x%04x", |
be39ee11 TK |
86 | '0' + loginfo->u.log.level, prefix, |
87 | in ? in->name : "", out ? out->name : "", | |
88 | eth_hdr(skb)->h_source, eth_hdr(skb)->h_dest, | |
89 | ntohs(eth_hdr(skb)->h_proto)); | |
1da177e4 | 90 | |
d5228a4f BDS |
91 | if (loginfo->type == NF_LOG_TYPE_LOG) |
92 | bitmask = loginfo->u.log.logflags; | |
93 | else | |
94 | bitmask = NF_LOG_MASK; | |
95 | ||
96 | if ((bitmask & EBT_LOG_IP) && eth_hdr(skb)->h_proto == | |
31a5b837 | 97 | htons(ETH_P_IP)) { |
abfdf1c4 JE |
98 | const struct iphdr *ih; |
99 | struct iphdr _iph; | |
1da177e4 LT |
100 | |
101 | ih = skb_header_pointer(skb, 0, sizeof(_iph), &_iph); | |
102 | if (ih == NULL) { | |
103 | printk(" INCOMPLETE IP header"); | |
104 | goto out; | |
105 | } | |
21454aaa HH |
106 | printk(" IP SRC=%pI4 IP DST=%pI4, IP tos=0x%02X, IP proto=%d", |
107 | &ih->saddr, &ih->daddr, ih->tos, ih->protocol); | |
93f65158 KT |
108 | print_ports(skb, ih->protocol, ih->ihl*4); |
109 | goto out; | |
110 | } | |
111 | ||
e6373c4c | 112 | #if IS_ENABLED(CONFIG_BRIDGE_EBT_IP6) |
93f65158 KT |
113 | if ((bitmask & EBT_LOG_IP6) && eth_hdr(skb)->h_proto == |
114 | htons(ETH_P_IPV6)) { | |
115 | const struct ipv6hdr *ih; | |
116 | struct ipv6hdr _iph; | |
117 | uint8_t nexthdr; | |
75f2811c | 118 | __be16 frag_off; |
93f65158 KT |
119 | int offset_ph; |
120 | ||
121 | ih = skb_header_pointer(skb, 0, sizeof(_iph), &_iph); | |
122 | if (ih == NULL) { | |
123 | printk(" INCOMPLETE IPv6 header"); | |
124 | goto out; | |
1da177e4 | 125 | } |
5b095d98 | 126 | printk(" IPv6 SRC=%pI6 IPv6 DST=%pI6, IPv6 priority=0x%01X, Next Header=%d", |
b189db5d | 127 | &ih->saddr, &ih->daddr, ih->priority, ih->nexthdr); |
93f65158 | 128 | nexthdr = ih->nexthdr; |
75f2811c | 129 | offset_ph = ipv6_skip_exthdr(skb, sizeof(_iph), &nexthdr, &frag_off); |
93f65158 KT |
130 | if (offset_ph == -1) |
131 | goto out; | |
132 | print_ports(skb, nexthdr, offset_ph); | |
1da177e4 LT |
133 | goto out; |
134 | } | |
f586287e | 135 | #endif |
1da177e4 | 136 | |
d5228a4f | 137 | if ((bitmask & EBT_LOG_ARP) && |
1da177e4 LT |
138 | ((eth_hdr(skb)->h_proto == htons(ETH_P_ARP)) || |
139 | (eth_hdr(skb)->h_proto == htons(ETH_P_RARP)))) { | |
abfdf1c4 JE |
140 | const struct arphdr *ah; |
141 | struct arphdr _arph; | |
1da177e4 LT |
142 | |
143 | ah = skb_header_pointer(skb, 0, sizeof(_arph), &_arph); | |
144 | if (ah == NULL) { | |
145 | printk(" INCOMPLETE ARP header"); | |
146 | goto out; | |
147 | } | |
148 | printk(" ARP HTYPE=%d, PTYPE=0x%04x, OPCODE=%d", | |
149 | ntohs(ah->ar_hrd), ntohs(ah->ar_pro), | |
150 | ntohs(ah->ar_op)); | |
151 | ||
152 | /* If it's for Ethernet and the lengths are OK, | |
7f495ad9 IM |
153 | * then log the ARP payload |
154 | */ | |
1da177e4 LT |
155 | if (ah->ar_hrd == htons(1) && |
156 | ah->ar_hln == ETH_ALEN && | |
47c183fa | 157 | ah->ar_pln == sizeof(__be32)) { |
abfdf1c4 JE |
158 | const struct arppayload *ap; |
159 | struct arppayload _arpp; | |
1da177e4 | 160 | |
85c1937b | 161 | ap = skb_header_pointer(skb, sizeof(_arph), |
1da177e4 LT |
162 | sizeof(_arpp), &_arpp); |
163 | if (ap == NULL) { | |
164 | printk(" INCOMPLETE ARP payload"); | |
165 | goto out; | |
166 | } | |
be39ee11 TK |
167 | printk(" ARP MAC SRC=%pM ARP IP SRC=%pI4 ARP MAC DST=%pM ARP IP DST=%pI4", |
168 | ap->mac_src, ap->ip_src, ap->mac_dst, ap->ip_dst); | |
1da177e4 LT |
169 | } |
170 | } | |
171 | out: | |
172 | printk("\n"); | |
173 | spin_unlock_bh(&ebt_log_lock); | |
d5228a4f BDS |
174 | |
175 | } | |
176 | ||
2d06d4a5 | 177 | static unsigned int |
4b560b44 | 178 | ebt_log_tg(struct sk_buff *skb, const struct xt_action_param *par) |
d5228a4f | 179 | { |
7eb35586 | 180 | const struct ebt_log_info *info = par->targinfo; |
d5228a4f | 181 | struct nf_loginfo li; |
686c9b50 | 182 | struct net *net = par->net; |
d5228a4f BDS |
183 | |
184 | li.type = NF_LOG_TYPE_LOG; | |
185 | li.u.log.level = info->loglevel; | |
186 | li.u.log.logflags = info->bitmask; | |
187 | ||
960649d1 PNA |
188 | /* Remember that we have to use ebt_log_packet() not to break backward |
189 | * compatibility. We cannot use the default bridge packet logger via | |
190 | * nf_log_packet() with NFT_LOG_TYPE_LOG here. --Pablo | |
191 | */ | |
bafac2a5 | 192 | if (info->bitmask & EBT_LOG_NFLOG) |
30e0c6a6 G |
193 | nf_log_packet(net, NFPROTO_BRIDGE, par->hooknum, skb, |
194 | par->in, par->out, &li, "%s", info->prefix); | |
bafac2a5 | 195 | else |
8cdb46da | 196 | ebt_log_packet(net, NFPROTO_BRIDGE, par->hooknum, skb, par->in, |
30e0c6a6 | 197 | par->out, &li, info->prefix); |
0ac6ab1f | 198 | return EBT_CONTINUE; |
1da177e4 LT |
199 | } |
200 | ||
043ef46c JE |
201 | static struct xt_target ebt_log_tg_reg __read_mostly = { |
202 | .name = "log", | |
001a18d3 JE |
203 | .revision = 0, |
204 | .family = NFPROTO_BRIDGE, | |
2d06d4a5 JE |
205 | .target = ebt_log_tg, |
206 | .checkentry = ebt_log_tg_check, | |
fc0e3df4 | 207 | .targetsize = sizeof(struct ebt_log_info), |
1da177e4 LT |
208 | .me = THIS_MODULE, |
209 | }; | |
210 | ||
65b4b4e8 | 211 | static int __init ebt_log_init(void) |
1da177e4 | 212 | { |
960649d1 | 213 | return xt_register_target(&ebt_log_tg_reg); |
1da177e4 LT |
214 | } |
215 | ||
65b4b4e8 | 216 | static void __exit ebt_log_fini(void) |
1da177e4 | 217 | { |
043ef46c | 218 | xt_unregister_target(&ebt_log_tg_reg); |
1da177e4 LT |
219 | } |
220 | ||
65b4b4e8 AM |
221 | module_init(ebt_log_init); |
222 | module_exit(ebt_log_fini); | |
f776c4cd | 223 | MODULE_DESCRIPTION("Ebtables: Packet logging to syslog"); |
1da177e4 | 224 | MODULE_LICENSE("GPL"); |