]>
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 | */ | |
11 | ||
12 | #include <linux/netfilter_bridge/ebtables.h> | |
13 | #include <linux/netfilter_bridge/ebt_log.h> | |
d5228a4f | 14 | #include <linux/netfilter.h> |
1da177e4 LT |
15 | #include <linux/module.h> |
16 | #include <linux/ip.h> | |
2e4e6a17 | 17 | #include <linux/in.h> |
1da177e4 LT |
18 | #include <linux/if_arp.h> |
19 | #include <linux/spinlock.h> | |
f01ffbd6 | 20 | #include <net/netfilter/nf_log.h> |
1da177e4 LT |
21 | |
22 | static DEFINE_SPINLOCK(ebt_log_lock); | |
23 | ||
24 | static int ebt_log_check(const char *tablename, unsigned int hookmask, | |
25 | const struct ebt_entry *e, void *data, unsigned int datalen) | |
26 | { | |
abfdf1c4 | 27 | struct ebt_log_info *info = data; |
1da177e4 LT |
28 | |
29 | if (datalen != EBT_ALIGN(sizeof(struct ebt_log_info))) | |
30 | return -EINVAL; | |
31 | if (info->bitmask & ~EBT_LOG_MASK) | |
32 | return -EINVAL; | |
33 | if (info->loglevel >= 8) | |
34 | return -EINVAL; | |
35 | info->prefix[EBT_LOG_PREFIX_SIZE - 1] = '\0'; | |
36 | return 0; | |
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 | ||
abfdf1c4 | 53 | static void print_MAC(const unsigned char *p) |
1da177e4 LT |
54 | { |
55 | int i; | |
56 | ||
57 | for (i = 0; i < ETH_ALEN; i++, p++) | |
58 | printk("%02x%c", *p, i == ETH_ALEN - 1 ? ' ':':'); | |
59 | } | |
60 | ||
61 | #define myNIPQUAD(a) a[0], a[1], a[2], a[3] | |
d5228a4f BDS |
62 | static void |
63 | ebt_log_packet(unsigned int pf, unsigned int hooknum, | |
64 | const struct sk_buff *skb, const struct net_device *in, | |
65 | const struct net_device *out, const struct nf_loginfo *loginfo, | |
66 | const char *prefix) | |
1da177e4 | 67 | { |
d5228a4f | 68 | unsigned int bitmask; |
1da177e4 | 69 | |
1da177e4 | 70 | spin_lock_bh(&ebt_log_lock); |
d5228a4f BDS |
71 | printk("<%c>%s IN=%s OUT=%s MAC source = ", '0' + loginfo->u.log.level, |
72 | prefix, in ? in->name : "", out ? out->name : ""); | |
1da177e4 | 73 | |
1da177e4 LT |
74 | print_MAC(eth_hdr(skb)->h_source); |
75 | printk("MAC dest = "); | |
76 | print_MAC(eth_hdr(skb)->h_dest); | |
77 | ||
78 | printk("proto = 0x%04x", ntohs(eth_hdr(skb)->h_proto)); | |
79 | ||
d5228a4f BDS |
80 | if (loginfo->type == NF_LOG_TYPE_LOG) |
81 | bitmask = loginfo->u.log.logflags; | |
82 | else | |
83 | bitmask = NF_LOG_MASK; | |
84 | ||
85 | if ((bitmask & EBT_LOG_IP) && eth_hdr(skb)->h_proto == | |
1da177e4 | 86 | htons(ETH_P_IP)){ |
abfdf1c4 JE |
87 | const struct iphdr *ih; |
88 | struct iphdr _iph; | |
1da177e4 LT |
89 | |
90 | ih = skb_header_pointer(skb, 0, sizeof(_iph), &_iph); | |
91 | if (ih == NULL) { | |
92 | printk(" INCOMPLETE IP header"); | |
93 | goto out; | |
94 | } | |
d5228a4f BDS |
95 | printk(" IP SRC=%u.%u.%u.%u IP DST=%u.%u.%u.%u, IP " |
96 | "tos=0x%02X, IP proto=%d", NIPQUAD(ih->saddr), | |
97 | NIPQUAD(ih->daddr), ih->tos, ih->protocol); | |
1da177e4 | 98 | if (ih->protocol == IPPROTO_TCP || |
ab67a4d5 | 99 | ih->protocol == IPPROTO_UDP || |
a8d0f952 | 100 | ih->protocol == IPPROTO_UDPLITE || |
ab67a4d5 PM |
101 | ih->protocol == IPPROTO_SCTP || |
102 | ih->protocol == IPPROTO_DCCP) { | |
abfdf1c4 JE |
103 | const struct tcpudphdr *pptr; |
104 | struct tcpudphdr _ports; | |
1da177e4 LT |
105 | |
106 | pptr = skb_header_pointer(skb, ih->ihl*4, | |
107 | sizeof(_ports), &_ports); | |
108 | if (pptr == NULL) { | |
109 | printk(" INCOMPLETE TCP/UDP header"); | |
110 | goto out; | |
111 | } | |
112 | printk(" SPT=%u DPT=%u", ntohs(pptr->src), | |
113 | ntohs(pptr->dst)); | |
114 | } | |
115 | goto out; | |
116 | } | |
117 | ||
d5228a4f | 118 | if ((bitmask & EBT_LOG_ARP) && |
1da177e4 LT |
119 | ((eth_hdr(skb)->h_proto == htons(ETH_P_ARP)) || |
120 | (eth_hdr(skb)->h_proto == htons(ETH_P_RARP)))) { | |
abfdf1c4 JE |
121 | const struct arphdr *ah; |
122 | struct arphdr _arph; | |
1da177e4 LT |
123 | |
124 | ah = skb_header_pointer(skb, 0, sizeof(_arph), &_arph); | |
125 | if (ah == NULL) { | |
126 | printk(" INCOMPLETE ARP header"); | |
127 | goto out; | |
128 | } | |
129 | printk(" ARP HTYPE=%d, PTYPE=0x%04x, OPCODE=%d", | |
130 | ntohs(ah->ar_hrd), ntohs(ah->ar_pro), | |
131 | ntohs(ah->ar_op)); | |
132 | ||
133 | /* If it's for Ethernet and the lengths are OK, | |
134 | * then log the ARP payload */ | |
135 | if (ah->ar_hrd == htons(1) && | |
136 | ah->ar_hln == ETH_ALEN && | |
47c183fa | 137 | ah->ar_pln == sizeof(__be32)) { |
abfdf1c4 JE |
138 | const struct arppayload *ap; |
139 | struct arppayload _arpp; | |
1da177e4 | 140 | |
85c1937b | 141 | ap = skb_header_pointer(skb, sizeof(_arph), |
1da177e4 LT |
142 | sizeof(_arpp), &_arpp); |
143 | if (ap == NULL) { | |
144 | printk(" INCOMPLETE ARP payload"); | |
145 | goto out; | |
146 | } | |
147 | printk(" ARP MAC SRC="); | |
148 | print_MAC(ap->mac_src); | |
149 | printk(" ARP IP SRC=%u.%u.%u.%u", | |
150 | myNIPQUAD(ap->ip_src)); | |
151 | printk(" ARP MAC DST="); | |
152 | print_MAC(ap->mac_dst); | |
153 | printk(" ARP IP DST=%u.%u.%u.%u", | |
154 | myNIPQUAD(ap->ip_dst)); | |
155 | } | |
156 | } | |
157 | out: | |
158 | printk("\n"); | |
159 | spin_unlock_bh(&ebt_log_lock); | |
d5228a4f BDS |
160 | |
161 | } | |
162 | ||
163 | static void ebt_log(const struct sk_buff *skb, unsigned int hooknr, | |
164 | const struct net_device *in, const struct net_device *out, | |
165 | const void *data, unsigned int datalen) | |
166 | { | |
abfdf1c4 | 167 | const struct ebt_log_info *info = data; |
d5228a4f BDS |
168 | struct nf_loginfo li; |
169 | ||
170 | li.type = NF_LOG_TYPE_LOG; | |
171 | li.u.log.level = info->loglevel; | |
172 | li.u.log.logflags = info->bitmask; | |
173 | ||
bafac2a5 PM |
174 | if (info->bitmask & EBT_LOG_NFLOG) |
175 | nf_log_packet(PF_BRIDGE, hooknr, skb, in, out, &li, | |
9d6f229f | 176 | "%s", info->prefix); |
bafac2a5 PM |
177 | else |
178 | ebt_log_packet(PF_BRIDGE, hooknr, skb, in, out, &li, | |
9d6f229f | 179 | info->prefix); |
1da177e4 LT |
180 | } |
181 | ||
182 | static struct ebt_watcher log = | |
183 | { | |
184 | .name = EBT_LOG_WATCHER, | |
185 | .watcher = ebt_log, | |
186 | .check = ebt_log_check, | |
187 | .me = THIS_MODULE, | |
188 | }; | |
189 | ||
7b2f9631 | 190 | static const struct nf_logger ebt_log_logger = { |
d5228a4f BDS |
191 | .name = "ebt_log", |
192 | .logfn = &ebt_log_packet, | |
193 | .me = THIS_MODULE, | |
194 | }; | |
195 | ||
65b4b4e8 | 196 | static int __init ebt_log_init(void) |
1da177e4 | 197 | { |
d5228a4f BDS |
198 | int ret; |
199 | ||
200 | ret = ebt_register_watcher(&log); | |
201 | if (ret < 0) | |
202 | return ret; | |
7e2acc7e PM |
203 | nf_log_register(PF_BRIDGE, &ebt_log_logger); |
204 | return 0; | |
1da177e4 LT |
205 | } |
206 | ||
65b4b4e8 | 207 | static void __exit ebt_log_fini(void) |
1da177e4 | 208 | { |
e92ad99c | 209 | nf_log_unregister(&ebt_log_logger); |
1da177e4 LT |
210 | ebt_unregister_watcher(&log); |
211 | } | |
212 | ||
65b4b4e8 AM |
213 | module_init(ebt_log_init); |
214 | module_exit(ebt_log_fini); | |
1da177e4 | 215 | MODULE_LICENSE("GPL"); |