2 * This is a module which is used for logging packets.
5 /* (C) 1999-2001 Paul `Rusty' Russell
6 * (C) 2002-2004 Netfilter Core Team <coreteam@netfilter.org>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
13 #include <linux/module.h>
14 #include <linux/spinlock.h>
15 #include <linux/skbuff.h>
20 #include <net/route.h>
22 #include <linux/netfilter.h>
23 #include <linux/netfilter_ipv4/ip_tables.h>
24 #include <linux/netfilter_ipv4/ipt_LOG.h>
26 MODULE_LICENSE("GPL");
27 MODULE_AUTHOR("Netfilter Core Team <coreteam@netfilter.org>");
28 MODULE_DESCRIPTION("iptables syslog logging module");
30 static unsigned int nflog
= 1;
31 module_param(nflog
, int, 0400);
32 MODULE_PARM_DESC(nflog
, "register as internal netfilter logging module");
37 #define DEBUGP(format, args...)
40 /* Use lock to serialize, so printks don't overlap */
41 static DEFINE_SPINLOCK(log_lock
);
43 /* One level of recursion won't kill us */
44 static void dump_packet(const struct ipt_log_info
*info
,
45 const struct sk_buff
*skb
,
48 struct iphdr _iph
, *ih
;
50 ih
= skb_header_pointer(skb
, iphoff
, sizeof(_iph
), &_iph
);
57 * TOS, len, DF/MF, fragment offset, TTL, src, dst, options. */
58 /* Max length: 40 "SRC=255.255.255.255 DST=255.255.255.255 " */
59 printk("SRC=%u.%u.%u.%u DST=%u.%u.%u.%u ",
60 NIPQUAD(ih
->saddr
), NIPQUAD(ih
->daddr
));
62 /* Max length: 46 "LEN=65535 TOS=0xFF PREC=0xFF TTL=255 ID=65535 " */
63 printk("LEN=%u TOS=0x%02X PREC=0x%02X TTL=%u ID=%u ",
64 ntohs(ih
->tot_len
), ih
->tos
& IPTOS_TOS_MASK
,
65 ih
->tos
& IPTOS_PREC_MASK
, ih
->ttl
, ntohs(ih
->id
));
67 /* Max length: 6 "CE DF MF " */
68 if (ntohs(ih
->frag_off
) & IP_CE
)
70 if (ntohs(ih
->frag_off
) & IP_DF
)
72 if (ntohs(ih
->frag_off
) & IP_MF
)
75 /* Max length: 11 "FRAG:65535 " */
76 if (ntohs(ih
->frag_off
) & IP_OFFSET
)
77 printk("FRAG:%u ", ntohs(ih
->frag_off
) & IP_OFFSET
);
79 if ((info
->logflags
& IPT_LOG_IPOPT
)
80 && ih
->ihl
* 4 > sizeof(struct iphdr
)) {
81 unsigned char _opt
[4 * 15 - sizeof(struct iphdr
)], *op
;
82 unsigned int i
, optsize
;
84 optsize
= ih
->ihl
* 4 - sizeof(struct iphdr
);
85 op
= skb_header_pointer(skb
, iphoff
+sizeof(_iph
),
92 /* Max length: 127 "OPT (" 15*4*2chars ") " */
94 for (i
= 0; i
< optsize
; i
++)
95 printk("%02X", op
[i
]);
99 switch (ih
->protocol
) {
101 struct tcphdr _tcph
, *th
;
103 /* Max length: 10 "PROTO=TCP " */
104 printk("PROTO=TCP ");
106 if (ntohs(ih
->frag_off
) & IP_OFFSET
)
109 /* Max length: 25 "INCOMPLETE [65535 bytes] " */
110 th
= skb_header_pointer(skb
, iphoff
+ ih
->ihl
* 4,
111 sizeof(_tcph
), &_tcph
);
113 printk("INCOMPLETE [%u bytes] ",
114 skb
->len
- iphoff
- ih
->ihl
*4);
118 /* Max length: 20 "SPT=65535 DPT=65535 " */
119 printk("SPT=%u DPT=%u ",
120 ntohs(th
->source
), ntohs(th
->dest
));
121 /* Max length: 30 "SEQ=4294967295 ACK=4294967295 " */
122 if (info
->logflags
& IPT_LOG_TCPSEQ
)
123 printk("SEQ=%u ACK=%u ",
124 ntohl(th
->seq
), ntohl(th
->ack_seq
));
125 /* Max length: 13 "WINDOW=65535 " */
126 printk("WINDOW=%u ", ntohs(th
->window
));
127 /* Max length: 9 "RES=0x3F " */
128 printk("RES=0x%02x ", (u8
)(ntohl(tcp_flag_word(th
) & TCP_RESERVED_BITS
) >> 22));
129 /* Max length: 32 "CWR ECE URG ACK PSH RST SYN FIN " */
146 /* Max length: 11 "URGP=65535 " */
147 printk("URGP=%u ", ntohs(th
->urg_ptr
));
149 if ((info
->logflags
& IPT_LOG_TCPOPT
)
150 && th
->doff
* 4 > sizeof(struct tcphdr
)) {
151 unsigned char _opt
[4 * 15 - sizeof(struct tcphdr
)];
153 unsigned int i
, optsize
;
155 optsize
= th
->doff
* 4 - sizeof(struct tcphdr
);
156 op
= skb_header_pointer(skb
,
157 iphoff
+ih
->ihl
*4+sizeof(_tcph
),
164 /* Max length: 127 "OPT (" 15*4*2chars ") " */
166 for (i
= 0; i
< optsize
; i
++)
167 printk("%02X", op
[i
]);
173 struct udphdr _udph
, *uh
;
175 /* Max length: 10 "PROTO=UDP " */
176 printk("PROTO=UDP ");
178 if (ntohs(ih
->frag_off
) & IP_OFFSET
)
181 /* Max length: 25 "INCOMPLETE [65535 bytes] " */
182 uh
= skb_header_pointer(skb
, iphoff
+ih
->ihl
*4,
183 sizeof(_udph
), &_udph
);
185 printk("INCOMPLETE [%u bytes] ",
186 skb
->len
- iphoff
- ih
->ihl
*4);
190 /* Max length: 20 "SPT=65535 DPT=65535 " */
191 printk("SPT=%u DPT=%u LEN=%u ",
192 ntohs(uh
->source
), ntohs(uh
->dest
),
197 struct icmphdr _icmph
, *ich
;
198 static size_t required_len
[NR_ICMP_TYPES
+1]
199 = { [ICMP_ECHOREPLY
] = 4,
201 = 8 + sizeof(struct iphdr
),
203 = 8 + sizeof(struct iphdr
),
205 = 8 + sizeof(struct iphdr
),
208 = 8 + sizeof(struct iphdr
),
210 = 8 + sizeof(struct iphdr
),
211 [ICMP_TIMESTAMP
] = 20,
212 [ICMP_TIMESTAMPREPLY
] = 20,
214 [ICMP_ADDRESSREPLY
] = 12 };
216 /* Max length: 11 "PROTO=ICMP " */
217 printk("PROTO=ICMP ");
219 if (ntohs(ih
->frag_off
) & IP_OFFSET
)
222 /* Max length: 25 "INCOMPLETE [65535 bytes] " */
223 ich
= skb_header_pointer(skb
, iphoff
+ ih
->ihl
* 4,
224 sizeof(_icmph
), &_icmph
);
226 printk("INCOMPLETE [%u bytes] ",
227 skb
->len
- iphoff
- ih
->ihl
*4);
231 /* Max length: 18 "TYPE=255 CODE=255 " */
232 printk("TYPE=%u CODE=%u ", ich
->type
, ich
->code
);
234 /* Max length: 25 "INCOMPLETE [65535 bytes] " */
235 if (ich
->type
<= NR_ICMP_TYPES
236 && required_len
[ich
->type
]
237 && skb
->len
-iphoff
-ih
->ihl
*4 < required_len
[ich
->type
]) {
238 printk("INCOMPLETE [%u bytes] ",
239 skb
->len
- iphoff
- ih
->ihl
*4);
246 /* Max length: 19 "ID=65535 SEQ=65535 " */
247 printk("ID=%u SEQ=%u ",
248 ntohs(ich
->un
.echo
.id
),
249 ntohs(ich
->un
.echo
.sequence
));
252 case ICMP_PARAMETERPROB
:
253 /* Max length: 14 "PARAMETER=255 " */
254 printk("PARAMETER=%u ",
255 ntohl(ich
->un
.gateway
) >> 24);
258 /* Max length: 24 "GATEWAY=255.255.255.255 " */
259 printk("GATEWAY=%u.%u.%u.%u ",
260 NIPQUAD(ich
->un
.gateway
));
262 case ICMP_DEST_UNREACH
:
263 case ICMP_SOURCE_QUENCH
:
264 case ICMP_TIME_EXCEEDED
:
265 /* Max length: 3+maxlen */
266 if (!iphoff
) { /* Only recurse once. */
268 dump_packet(info
, skb
,
269 iphoff
+ ih
->ihl
*4+sizeof(_icmph
));
273 /* Max length: 10 "MTU=65535 " */
274 if (ich
->type
== ICMP_DEST_UNREACH
275 && ich
->code
== ICMP_FRAG_NEEDED
)
276 printk("MTU=%u ", ntohs(ich
->un
.frag
.mtu
));
282 struct ip_auth_hdr _ahdr
, *ah
;
284 if (ntohs(ih
->frag_off
) & IP_OFFSET
)
287 /* Max length: 9 "PROTO=AH " */
290 /* Max length: 25 "INCOMPLETE [65535 bytes] " */
291 ah
= skb_header_pointer(skb
, iphoff
+ih
->ihl
*4,
292 sizeof(_ahdr
), &_ahdr
);
294 printk("INCOMPLETE [%u bytes] ",
295 skb
->len
- iphoff
- ih
->ihl
*4);
299 /* Length: 15 "SPI=0xF1234567 " */
300 printk("SPI=0x%x ", ntohl(ah
->spi
));
304 struct ip_esp_hdr _esph
, *eh
;
306 /* Max length: 10 "PROTO=ESP " */
307 printk("PROTO=ESP ");
309 if (ntohs(ih
->frag_off
) & IP_OFFSET
)
312 /* Max length: 25 "INCOMPLETE [65535 bytes] " */
313 eh
= skb_header_pointer(skb
, iphoff
+ih
->ihl
*4,
314 sizeof(_esph
), &_esph
);
316 printk("INCOMPLETE [%u bytes] ",
317 skb
->len
- iphoff
- ih
->ihl
*4);
321 /* Length: 15 "SPI=0xF1234567 " */
322 printk("SPI=0x%x ", ntohl(eh
->spi
));
325 /* Max length: 10 "PROTO 255 " */
327 printk("PROTO=%u ", ih
->protocol
);
330 /* Max length: 15 "UID=4294967295 " */
331 if ((info
->logflags
& IPT_LOG_UID
) && !iphoff
&& skb
->sk
) {
332 read_lock_bh(&skb
->sk
->sk_callback_lock
);
333 if (skb
->sk
->sk_socket
&& skb
->sk
->sk_socket
->file
)
334 printk("UID=%u ", skb
->sk
->sk_socket
->file
->f_uid
);
335 read_unlock_bh(&skb
->sk
->sk_callback_lock
);
338 /* Proto Max log string length */
339 /* IP: 40+46+6+11+127 = 230 */
340 /* TCP: 10+max(25,20+30+13+9+32+11+127) = 252 */
341 /* UDP: 10+max(25,20) = 35 */
342 /* ICMP: 11+max(25, 18+25+max(19,14,24+3+n+10,3+n+10)) = 91+n */
343 /* ESP: 10+max(25)+15 = 50 */
344 /* AH: 9+max(25)+15 = 49 */
347 /* (ICMP allows recursion one level deep) */
348 /* maxlen = IP + ICMP + IP + max(TCP,UDP,ICMP,unknown) */
349 /* maxlen = 230+ 91 + 230 + 252 = 803 */
353 ipt_log_packet(unsigned int hooknum
,
354 const struct sk_buff
*skb
,
355 const struct net_device
*in
,
356 const struct net_device
*out
,
357 const struct ipt_log_info
*loginfo
,
358 const char *level_string
,
361 spin_lock_bh(&log_lock
);
362 printk(level_string
);
363 printk("%sIN=%s OUT=%s ",
364 prefix
== NULL
? loginfo
->prefix
: prefix
,
366 out
? out
->name
: "");
367 #ifdef CONFIG_BRIDGE_NETFILTER
368 if (skb
->nf_bridge
) {
369 struct net_device
*physindev
= skb
->nf_bridge
->physindev
;
370 struct net_device
*physoutdev
= skb
->nf_bridge
->physoutdev
;
372 if (physindev
&& in
!= physindev
)
373 printk("PHYSIN=%s ", physindev
->name
);
374 if (physoutdev
&& out
!= physoutdev
)
375 printk("PHYSOUT=%s ", physoutdev
->name
);
380 /* MAC logging for input chain only. */
382 if (skb
->dev
&& skb
->dev
->hard_header_len
383 && skb
->mac
.raw
!= (void*)skb
->nh
.iph
) {
385 unsigned char *p
= skb
->mac
.raw
;
386 for (i
= 0; i
< skb
->dev
->hard_header_len
; i
++,p
++)
388 i
==skb
->dev
->hard_header_len
- 1
394 dump_packet(loginfo
, skb
, 0);
396 spin_unlock_bh(&log_lock
);
400 ipt_log_target(struct sk_buff
**pskb
,
401 const struct net_device
*in
,
402 const struct net_device
*out
,
403 unsigned int hooknum
,
404 const void *targinfo
,
407 const struct ipt_log_info
*loginfo
= targinfo
;
408 char level_string
[4] = "< >";
410 level_string
[1] = '0' + (loginfo
->level
% 8);
411 ipt_log_packet(hooknum
, *pskb
, in
, out
, loginfo
, level_string
, NULL
);
417 ipt_logfn(unsigned int hooknum
,
418 const struct sk_buff
*skb
,
419 const struct net_device
*in
,
420 const struct net_device
*out
,
423 struct ipt_log_info loginfo
= {
425 .logflags
= IPT_LOG_MASK
,
429 ipt_log_packet(hooknum
, skb
, in
, out
, &loginfo
, KERN_WARNING
, prefix
);
432 static int ipt_log_checkentry(const char *tablename
,
433 const struct ipt_entry
*e
,
435 unsigned int targinfosize
,
436 unsigned int hook_mask
)
438 const struct ipt_log_info
*loginfo
= targinfo
;
440 if (targinfosize
!= IPT_ALIGN(sizeof(struct ipt_log_info
))) {
441 DEBUGP("LOG: targinfosize %u != %u\n",
442 targinfosize
, IPT_ALIGN(sizeof(struct ipt_log_info
)));
446 if (loginfo
->level
>= 8) {
447 DEBUGP("LOG: level %u >= 8\n", loginfo
->level
);
451 if (loginfo
->prefix
[sizeof(loginfo
->prefix
)-1] != '\0') {
452 DEBUGP("LOG: prefix term %i\n",
453 loginfo
->prefix
[sizeof(loginfo
->prefix
)-1]);
460 static struct ipt_target ipt_log_reg
= {
462 .target
= ipt_log_target
,
463 .checkentry
= ipt_log_checkentry
,
467 static int __init
init(void)
469 if (ipt_register_target(&ipt_log_reg
))
472 nf_log_register(PF_INET
, &ipt_logfn
);
477 static void __exit
fini(void)
480 nf_log_unregister(PF_INET
, &ipt_logfn
);
481 ipt_unregister_target(&ipt_log_reg
);