]> git.karo-electronics.de Git - karo-tx-linux.git/blob - net/bridge/netfilter/ebt_log.c
Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/jesse/openvswitch
[karo-tx-linux.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 int 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 -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         __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
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
74 static void
75 ebt_log_packet(u_int8_t pf, unsigned int hooknum,
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)
79 {
80         unsigned int bitmask;
81         struct net *net = dev_net(in ? in : out);
82
83         /* FIXME: Disabled from containers until syslog ns is supported */
84         if (!net_eq(net, &init_net))
85                 return;
86
87         spin_lock_bh(&ebt_log_lock);
88         printk(KERN_SOH "%c%s IN=%s OUT=%s MAC source = %pM MAC dest = %pM proto = 0x%04x",
89                '0' + loginfo->u.log.level, prefix,
90                in ? in->name : "", out ? out->name : "",
91                eth_hdr(skb)->h_source, eth_hdr(skb)->h_dest,
92                ntohs(eth_hdr(skb)->h_proto));
93
94         if (loginfo->type == NF_LOG_TYPE_LOG)
95                 bitmask = loginfo->u.log.logflags;
96         else
97                 bitmask = NF_LOG_MASK;
98
99         if ((bitmask & EBT_LOG_IP) && eth_hdr(skb)->h_proto ==
100            htons(ETH_P_IP)){
101                 const struct iphdr *ih;
102                 struct iphdr _iph;
103
104                 ih = skb_header_pointer(skb, 0, sizeof(_iph), &_iph);
105                 if (ih == NULL) {
106                         printk(" INCOMPLETE IP header");
107                         goto out;
108                 }
109                 printk(" IP SRC=%pI4 IP DST=%pI4, IP tos=0x%02X, IP proto=%d",
110                        &ih->saddr, &ih->daddr, ih->tos, ih->protocol);
111                 print_ports(skb, ih->protocol, ih->ihl*4);
112                 goto out;
113         }
114
115 #if IS_ENABLED(CONFIG_BRIDGE_EBT_IP6)
116         if ((bitmask & EBT_LOG_IP6) && eth_hdr(skb)->h_proto ==
117            htons(ETH_P_IPV6)) {
118                 const struct ipv6hdr *ih;
119                 struct ipv6hdr _iph;
120                 uint8_t nexthdr;
121                 __be16 frag_off;
122                 int offset_ph;
123
124                 ih = skb_header_pointer(skb, 0, sizeof(_iph), &_iph);
125                 if (ih == NULL) {
126                         printk(" INCOMPLETE IPv6 header");
127                         goto out;
128                 }
129                 printk(" IPv6 SRC=%pI6 IPv6 DST=%pI6, IPv6 priority=0x%01X, Next Header=%d",
130                        &ih->saddr, &ih->daddr, ih->priority, ih->nexthdr);
131                 nexthdr = ih->nexthdr;
132                 offset_ph = ipv6_skip_exthdr(skb, sizeof(_iph), &nexthdr, &frag_off);
133                 if (offset_ph == -1)
134                         goto out;
135                 print_ports(skb, nexthdr, offset_ph);
136                 goto out;
137         }
138 #endif
139
140         if ((bitmask & EBT_LOG_ARP) &&
141             ((eth_hdr(skb)->h_proto == htons(ETH_P_ARP)) ||
142              (eth_hdr(skb)->h_proto == htons(ETH_P_RARP)))) {
143                 const struct arphdr *ah;
144                 struct arphdr _arph;
145
146                 ah = skb_header_pointer(skb, 0, sizeof(_arph), &_arph);
147                 if (ah == NULL) {
148                         printk(" INCOMPLETE ARP header");
149                         goto out;
150                 }
151                 printk(" ARP HTYPE=%d, PTYPE=0x%04x, OPCODE=%d",
152                        ntohs(ah->ar_hrd), ntohs(ah->ar_pro),
153                        ntohs(ah->ar_op));
154
155                 /* If it's for Ethernet and the lengths are OK,
156                  * then log the ARP payload */
157                 if (ah->ar_hrd == htons(1) &&
158                     ah->ar_hln == ETH_ALEN &&
159                     ah->ar_pln == sizeof(__be32)) {
160                         const struct arppayload *ap;
161                         struct arppayload _arpp;
162
163                         ap = skb_header_pointer(skb, sizeof(_arph),
164                                                 sizeof(_arpp), &_arpp);
165                         if (ap == NULL) {
166                                 printk(" INCOMPLETE ARP payload");
167                                 goto out;
168                         }
169                         printk(" ARP MAC SRC=%pM ARP IP SRC=%pI4 ARP MAC DST=%pM ARP IP DST=%pI4",
170                                         ap->mac_src, ap->ip_src, ap->mac_dst, ap->ip_dst);
171                 }
172         }
173 out:
174         printk("\n");
175         spin_unlock_bh(&ebt_log_lock);
176
177 }
178
179 static unsigned int
180 ebt_log_tg(struct sk_buff *skb, const struct xt_action_param *par)
181 {
182         const struct ebt_log_info *info = par->targinfo;
183         struct nf_loginfo li;
184         struct net *net = dev_net(par->in ? par->in : par->out);
185
186         li.type = NF_LOG_TYPE_LOG;
187         li.u.log.level = info->loglevel;
188         li.u.log.logflags = info->bitmask;
189
190         if (info->bitmask & EBT_LOG_NFLOG)
191                 nf_log_packet(net, NFPROTO_BRIDGE, par->hooknum, skb,
192                               par->in, par->out, &li, "%s", info->prefix);
193         else
194                 ebt_log_packet(NFPROTO_BRIDGE, par->hooknum, skb, par->in,
195                                par->out, &li, info->prefix);
196         return EBT_CONTINUE;
197 }
198
199 static struct xt_target ebt_log_tg_reg __read_mostly = {
200         .name           = "log",
201         .revision       = 0,
202         .family         = NFPROTO_BRIDGE,
203         .target         = ebt_log_tg,
204         .checkentry     = ebt_log_tg_check,
205         .targetsize     = sizeof(struct ebt_log_info),
206         .me             = THIS_MODULE,
207 };
208
209 static struct nf_logger ebt_log_logger __read_mostly = {
210         .name           = "ebt_log",
211         .logfn          = &ebt_log_packet,
212         .me             = THIS_MODULE,
213 };
214
215 static int __net_init ebt_log_net_init(struct net *net)
216 {
217         nf_log_set(net, NFPROTO_BRIDGE, &ebt_log_logger);
218         return 0;
219 }
220
221 static void __net_exit ebt_log_net_fini(struct net *net)
222 {
223         nf_log_unset(net, &ebt_log_logger);
224 }
225
226 static struct pernet_operations ebt_log_net_ops = {
227         .init = ebt_log_net_init,
228         .exit = ebt_log_net_fini,
229 };
230
231 static int __init ebt_log_init(void)
232 {
233         int ret;
234
235         ret = register_pernet_subsys(&ebt_log_net_ops);
236         if (ret < 0)
237                 goto err_pernet;
238
239         ret = xt_register_target(&ebt_log_tg_reg);
240         if (ret < 0)
241                 goto err_target;
242
243         nf_log_register(NFPROTO_BRIDGE, &ebt_log_logger);
244
245         return ret;
246
247 err_target:
248         unregister_pernet_subsys(&ebt_log_net_ops);
249 err_pernet:
250         return ret;
251 }
252
253 static void __exit ebt_log_fini(void)
254 {
255         unregister_pernet_subsys(&ebt_log_net_ops);
256         nf_log_unregister(&ebt_log_logger);
257         xt_unregister_target(&ebt_log_tg_reg);
258 }
259
260 module_init(ebt_log_init);
261 module_exit(ebt_log_fini);
262 MODULE_DESCRIPTION("Ebtables: Packet logging to syslog");
263 MODULE_LICENSE("GPL");