]> bbs.cooldavid.org Git - net-next-2.6.git/blob - net/bridge/netfilter/ebt_log.c
net: replace %p6 with %pI6
[net-next-2.6.git] / net / bridge / netfilter / ebt_log.c
1 /*
2  *  ebt_log
3  *
4  *      Authors:
5  *      Bart De Schuymer <bdschuym@pandora.be>
6  *      Harald Welte <laforge@netfilter.org>
7  *
8  *  April, 2002
9  *
10  */
11 #include <linux/module.h>
12 #include <linux/ip.h>
13 #include <linux/in.h>
14 #include <linux/if_arp.h>
15 #include <linux/spinlock.h>
16 #include <net/netfilter/nf_log.h>
17 #include <linux/ipv6.h>
18 #include <net/ipv6.h>
19 #include <linux/in6.h>
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>
24
25 static DEFINE_SPINLOCK(ebt_log_lock);
26
27 static bool ebt_log_tg_check(const struct xt_tgchk_param *par)
28 {
29         struct ebt_log_info *info = par->targinfo;
30
31         if (info->bitmask & ~EBT_LOG_MASK)
32                 return false;
33         if (info->loglevel >= 8)
34                 return false;
35         info->prefix[EBT_LOG_PREFIX_SIZE - 1] = '\0';
36         return true;
37 }
38
39 struct tcpudphdr
40 {
41         __be16 src;
42         __be16 dst;
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
53 static void print_MAC(const unsigned char *p)
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 static void
62 print_ports(const struct sk_buff *skb, uint8_t protocol, int offset)
63 {
64         if (protocol == IPPROTO_TCP ||
65             protocol == IPPROTO_UDP ||
66             protocol == IPPROTO_UDPLITE ||
67             protocol == IPPROTO_SCTP ||
68             protocol == IPPROTO_DCCP) {
69                 const struct tcpudphdr *pptr;
70                 struct tcpudphdr _ports;
71
72                 pptr = skb_header_pointer(skb, offset,
73                                           sizeof(_ports), &_ports);
74                 if (pptr == NULL) {
75                         printk(" INCOMPLETE TCP/UDP header");
76                         return;
77                 }
78                 printk(" SPT=%u DPT=%u", ntohs(pptr->src), ntohs(pptr->dst));
79         }
80 }
81
82 #define myNIPQUAD(a) a[0], a[1], a[2], a[3]
83 static void
84 ebt_log_packet(u_int8_t pf, unsigned int hooknum,
85    const struct sk_buff *skb, const struct net_device *in,
86    const struct net_device *out, const struct nf_loginfo *loginfo,
87    const char *prefix)
88 {
89         unsigned int bitmask;
90
91         spin_lock_bh(&ebt_log_lock);
92         printk("<%c>%s IN=%s OUT=%s MAC source = ", '0' + loginfo->u.log.level,
93                prefix, in ? in->name : "", out ? out->name : "");
94
95         print_MAC(eth_hdr(skb)->h_source);
96         printk("MAC dest = ");
97         print_MAC(eth_hdr(skb)->h_dest);
98
99         printk("proto = 0x%04x", ntohs(eth_hdr(skb)->h_proto));
100
101         if (loginfo->type == NF_LOG_TYPE_LOG)
102                 bitmask = loginfo->u.log.logflags;
103         else
104                 bitmask = NF_LOG_MASK;
105
106         if ((bitmask & EBT_LOG_IP) && eth_hdr(skb)->h_proto ==
107            htons(ETH_P_IP)){
108                 const struct iphdr *ih;
109                 struct iphdr _iph;
110
111                 ih = skb_header_pointer(skb, 0, sizeof(_iph), &_iph);
112                 if (ih == NULL) {
113                         printk(" INCOMPLETE IP header");
114                         goto out;
115                 }
116                 printk(" IP SRC=%u.%u.%u.%u IP DST=%u.%u.%u.%u, IP "
117                        "tos=0x%02X, IP proto=%d", NIPQUAD(ih->saddr),
118                        NIPQUAD(ih->daddr), ih->tos, ih->protocol);
119                 print_ports(skb, ih->protocol, ih->ihl*4);
120                 goto out;
121         }
122
123 #if defined(CONFIG_BRIDGE_EBT_IP6) || defined(CONFIG_BRIDGE_EBT_IP6_MODULE)
124         if ((bitmask & EBT_LOG_IP6) && eth_hdr(skb)->h_proto ==
125            htons(ETH_P_IPV6)) {
126                 const struct ipv6hdr *ih;
127                 struct ipv6hdr _iph;
128                 uint8_t nexthdr;
129                 int offset_ph;
130
131                 ih = skb_header_pointer(skb, 0, sizeof(_iph), &_iph);
132                 if (ih == NULL) {
133                         printk(" INCOMPLETE IPv6 header");
134                         goto out;
135                 }
136                 printk(" IPv6 SRC=%pI6 IPv6 DST=%pI6, IPv6 priority=0x%01X, Next Header=%d",
137                        &ih->saddr, &ih->daddr, ih->priority, ih->nexthdr);
138                 nexthdr = ih->nexthdr;
139                 offset_ph = ipv6_skip_exthdr(skb, sizeof(_iph), &nexthdr);
140                 if (offset_ph == -1)
141                         goto out;
142                 print_ports(skb, nexthdr, offset_ph);
143                 goto out;
144         }
145 #endif
146
147         if ((bitmask & EBT_LOG_ARP) &&
148             ((eth_hdr(skb)->h_proto == htons(ETH_P_ARP)) ||
149              (eth_hdr(skb)->h_proto == htons(ETH_P_RARP)))) {
150                 const struct arphdr *ah;
151                 struct arphdr _arph;
152
153                 ah = skb_header_pointer(skb, 0, sizeof(_arph), &_arph);
154                 if (ah == NULL) {
155                         printk(" INCOMPLETE ARP header");
156                         goto out;
157                 }
158                 printk(" ARP HTYPE=%d, PTYPE=0x%04x, OPCODE=%d",
159                        ntohs(ah->ar_hrd), ntohs(ah->ar_pro),
160                        ntohs(ah->ar_op));
161
162                 /* If it's for Ethernet and the lengths are OK,
163                  * then log the ARP payload */
164                 if (ah->ar_hrd == htons(1) &&
165                     ah->ar_hln == ETH_ALEN &&
166                     ah->ar_pln == sizeof(__be32)) {
167                         const struct arppayload *ap;
168                         struct arppayload _arpp;
169
170                         ap = skb_header_pointer(skb, sizeof(_arph),
171                                                 sizeof(_arpp), &_arpp);
172                         if (ap == NULL) {
173                                 printk(" INCOMPLETE ARP payload");
174                                 goto out;
175                         }
176                         printk(" ARP MAC SRC=");
177                         print_MAC(ap->mac_src);
178                         printk(" ARP IP SRC=%u.%u.%u.%u",
179                                myNIPQUAD(ap->ip_src));
180                         printk(" ARP MAC DST=");
181                         print_MAC(ap->mac_dst);
182                         printk(" ARP IP DST=%u.%u.%u.%u",
183                                myNIPQUAD(ap->ip_dst));
184                 }
185         }
186 out:
187         printk("\n");
188         spin_unlock_bh(&ebt_log_lock);
189
190 }
191
192 static unsigned int
193 ebt_log_tg(struct sk_buff *skb, const struct xt_target_param *par)
194 {
195         const struct ebt_log_info *info = par->targinfo;
196         struct nf_loginfo li;
197
198         li.type = NF_LOG_TYPE_LOG;
199         li.u.log.level = info->loglevel;
200         li.u.log.logflags = info->bitmask;
201
202         if (info->bitmask & EBT_LOG_NFLOG)
203                 nf_log_packet(NFPROTO_BRIDGE, par->hooknum, skb, par->in,
204                               par->out, &li, "%s", info->prefix);
205         else
206                 ebt_log_packet(NFPROTO_BRIDGE, par->hooknum, skb, par->in,
207                                par->out, &li, info->prefix);
208         return EBT_CONTINUE;
209 }
210
211 static struct xt_target ebt_log_tg_reg __read_mostly = {
212         .name           = "log",
213         .revision       = 0,
214         .family         = NFPROTO_BRIDGE,
215         .target         = ebt_log_tg,
216         .checkentry     = ebt_log_tg_check,
217         .targetsize     = XT_ALIGN(sizeof(struct ebt_log_info)),
218         .me             = THIS_MODULE,
219 };
220
221 static const struct nf_logger ebt_log_logger = {
222         .name           = "ebt_log",
223         .logfn          = &ebt_log_packet,
224         .me             = THIS_MODULE,
225 };
226
227 static int __init ebt_log_init(void)
228 {
229         int ret;
230
231         ret = xt_register_target(&ebt_log_tg_reg);
232         if (ret < 0)
233                 return ret;
234         nf_log_register(NFPROTO_BRIDGE, &ebt_log_logger);
235         return 0;
236 }
237
238 static void __exit ebt_log_fini(void)
239 {
240         nf_log_unregister(&ebt_log_logger);
241         xt_unregister_target(&ebt_log_tg_reg);
242 }
243
244 module_init(ebt_log_init);
245 module_exit(ebt_log_fini);
246 MODULE_DESCRIPTION("Ebtables: Packet logging to syslog");
247 MODULE_LICENSE("GPL");