[EBTABLES]: Handle SCTP/DCCP in ebt_{ip,log}
[linux-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
12 #include <linux/in.h>
13 #include <linux/netfilter_bridge/ebtables.h>
14 #include <linux/netfilter_bridge/ebt_log.h>
15 #include <linux/netfilter.h>
16 #include <linux/module.h>
17 #include <linux/ip.h>
18 #include <linux/in.h>
19 #include <linux/if_arp.h>
20 #include <linux/spinlock.h>
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 {
27         struct ebt_log_info *info = (struct ebt_log_info *)data;
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 {
41         uint16_t src;
42         uint16_t 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(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 #define myNIPQUAD(a) a[0], a[1], a[2], a[3]
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)
67 {
68         unsigned int bitmask;
69
70         spin_lock_bh(&ebt_log_lock);
71         printk("<%c>%s IN=%s OUT=%s MAC source = ", '0' + loginfo->u.log.level,
72                prefix, in ? in->name : "", out ? out->name : "");
73
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
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 ==
86            htons(ETH_P_IP)){
87                 struct iphdr _iph, *ih;
88
89                 ih = skb_header_pointer(skb, 0, sizeof(_iph), &_iph);
90                 if (ih == NULL) {
91                         printk(" INCOMPLETE IP header");
92                         goto out;
93                 }
94                 printk(" IP SRC=%u.%u.%u.%u IP DST=%u.%u.%u.%u, IP "
95                        "tos=0x%02X, IP proto=%d", NIPQUAD(ih->saddr),
96                        NIPQUAD(ih->daddr), ih->tos, ih->protocol);
97                 if (ih->protocol == IPPROTO_TCP ||
98                     ih->protocol == IPPROTO_UDP ||
99                     ih->protocol == IPPROTO_SCTP ||
100                     ih->protocol == IPPROTO_DCCP) {
101                         struct tcpudphdr _ports, *pptr;
102
103                         pptr = skb_header_pointer(skb, ih->ihl*4,
104                                                   sizeof(_ports), &_ports);
105                         if (pptr == NULL) {
106                                 printk(" INCOMPLETE TCP/UDP header");
107                                 goto out;
108                         }
109                         printk(" SPT=%u DPT=%u", ntohs(pptr->src),
110                            ntohs(pptr->dst));
111                 }
112                 goto out;
113         }
114
115         if ((bitmask & EBT_LOG_ARP) &&
116             ((eth_hdr(skb)->h_proto == htons(ETH_P_ARP)) ||
117              (eth_hdr(skb)->h_proto == htons(ETH_P_RARP)))) {
118                 struct arphdr _arph, *ah;
119
120                 ah = skb_header_pointer(skb, 0, sizeof(_arph), &_arph);
121                 if (ah == NULL) {
122                         printk(" INCOMPLETE ARP header");
123                         goto out;
124                 }
125                 printk(" ARP HTYPE=%d, PTYPE=0x%04x, OPCODE=%d",
126                        ntohs(ah->ar_hrd), ntohs(ah->ar_pro),
127                        ntohs(ah->ar_op));
128
129                 /* If it's for Ethernet and the lengths are OK,
130                  * then log the ARP payload */
131                 if (ah->ar_hrd == htons(1) &&
132                     ah->ar_hln == ETH_ALEN &&
133                     ah->ar_pln == sizeof(uint32_t)) {
134                         struct arppayload _arpp, *ap;
135
136                         ap = skb_header_pointer(skb, sizeof(_arph),
137                                                 sizeof(_arpp), &_arpp);
138                         if (ap == NULL) {
139                                 printk(" INCOMPLETE ARP payload");
140                                 goto out;
141                         }
142                         printk(" ARP MAC SRC=");
143                         print_MAC(ap->mac_src);
144                         printk(" ARP IP SRC=%u.%u.%u.%u",
145                                myNIPQUAD(ap->ip_src));
146                         printk(" ARP MAC DST=");
147                         print_MAC(ap->mac_dst);
148                         printk(" ARP IP DST=%u.%u.%u.%u",
149                                myNIPQUAD(ap->ip_dst));
150                 }
151         }
152 out:
153         printk("\n");
154         spin_unlock_bh(&ebt_log_lock);
155
156 }
157
158 static void ebt_log(const struct sk_buff *skb, unsigned int hooknr,
159    const struct net_device *in, const struct net_device *out,
160    const void *data, unsigned int datalen)
161 {
162         struct ebt_log_info *info = (struct ebt_log_info *)data;
163         struct nf_loginfo li;
164
165         li.type = NF_LOG_TYPE_LOG;
166         li.u.log.level = info->loglevel;
167         li.u.log.logflags = info->bitmask;
168
169         nf_log_packet(PF_BRIDGE, hooknr, skb, in, out, &li, info->prefix);
170 }
171
172 static struct ebt_watcher log =
173 {
174         .name           = EBT_LOG_WATCHER,
175         .watcher        = ebt_log,
176         .check          = ebt_log_check,
177         .me             = THIS_MODULE,
178 };
179
180 static struct nf_logger ebt_log_logger = {
181         .name           = "ebt_log",
182         .logfn          = &ebt_log_packet,
183         .me             = THIS_MODULE,
184 };
185
186 static int __init init(void)
187 {
188         int ret;
189
190         ret = ebt_register_watcher(&log);
191         if (ret < 0)
192                 return ret;
193         if (nf_log_register(PF_BRIDGE, &ebt_log_logger) < 0) {
194                 printk(KERN_WARNING "ebt_log: not logging via system console "
195                        "since somebody else already registered for PF_INET\n");
196                 /* we cannot make module load fail here, since otherwise 
197                  * ebtables userspace would abort */
198         }
199
200         return 0;
201 }
202
203 static void __exit fini(void)
204 {
205         nf_log_unregister_logger(&ebt_log_logger);
206         ebt_unregister_watcher(&log);
207 }
208
209 module_init(init);
210 module_exit(fini);
211 MODULE_LICENSE("GPL");