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");
33 #define DEBUGP(format, args...)
36 /* Use lock to serialize, so printks don't overlap */
37 static DEFINE_SPINLOCK(log_lock
);
39 /* One level of recursion won't kill us */
40 static void dump_packet(const struct nf_loginfo
*info
,
41 const struct sk_buff
*skb
,
44 struct iphdr _iph
, *ih
;
45 unsigned int logflags
;
47 if (info
->type
== NF_LOG_TYPE_LOG
)
48 logflags
= info
->u
.log
.logflags
;
50 logflags
= NF_LOG_MASK
;
52 ih
= skb_header_pointer(skb
, iphoff
, sizeof(_iph
), &_iph
);
59 * TOS, len, DF/MF, fragment offset, TTL, src, dst, options. */
60 /* Max length: 40 "SRC=255.255.255.255 DST=255.255.255.255 " */
61 printk("SRC=%u.%u.%u.%u DST=%u.%u.%u.%u ",
62 NIPQUAD(ih
->saddr
), NIPQUAD(ih
->daddr
));
64 /* Max length: 46 "LEN=65535 TOS=0xFF PREC=0xFF TTL=255 ID=65535 " */
65 printk("LEN=%u TOS=0x%02X PREC=0x%02X TTL=%u ID=%u ",
66 ntohs(ih
->tot_len
), ih
->tos
& IPTOS_TOS_MASK
,
67 ih
->tos
& IPTOS_PREC_MASK
, ih
->ttl
, ntohs(ih
->id
));
69 /* Max length: 6 "CE DF MF " */
70 if (ntohs(ih
->frag_off
) & IP_CE
)
72 if (ntohs(ih
->frag_off
) & IP_DF
)
74 if (ntohs(ih
->frag_off
) & IP_MF
)
77 /* Max length: 11 "FRAG:65535 " */
78 if (ntohs(ih
->frag_off
) & IP_OFFSET
)
79 printk("FRAG:%u ", ntohs(ih
->frag_off
) & IP_OFFSET
);
81 if ((logflags
& IPT_LOG_IPOPT
)
82 && ih
->ihl
* 4 > sizeof(struct iphdr
)) {
83 unsigned char _opt
[4 * 15 - sizeof(struct iphdr
)], *op
;
84 unsigned int i
, optsize
;
86 optsize
= ih
->ihl
* 4 - sizeof(struct iphdr
);
87 op
= skb_header_pointer(skb
, iphoff
+sizeof(_iph
),
94 /* Max length: 127 "OPT (" 15*4*2chars ") " */
96 for (i
= 0; i
< optsize
; i
++)
97 printk("%02X", op
[i
]);
101 switch (ih
->protocol
) {
103 struct tcphdr _tcph
, *th
;
105 /* Max length: 10 "PROTO=TCP " */
106 printk("PROTO=TCP ");
108 if (ntohs(ih
->frag_off
) & IP_OFFSET
)
111 /* Max length: 25 "INCOMPLETE [65535 bytes] " */
112 th
= skb_header_pointer(skb
, iphoff
+ ih
->ihl
* 4,
113 sizeof(_tcph
), &_tcph
);
115 printk("INCOMPLETE [%u bytes] ",
116 skb
->len
- iphoff
- ih
->ihl
*4);
120 /* Max length: 20 "SPT=65535 DPT=65535 " */
121 printk("SPT=%u DPT=%u ",
122 ntohs(th
->source
), ntohs(th
->dest
));
123 /* Max length: 30 "SEQ=4294967295 ACK=4294967295 " */
124 if (logflags
& IPT_LOG_TCPSEQ
)
125 printk("SEQ=%u ACK=%u ",
126 ntohl(th
->seq
), ntohl(th
->ack_seq
));
127 /* Max length: 13 "WINDOW=65535 " */
128 printk("WINDOW=%u ", ntohs(th
->window
));
129 /* Max length: 9 "RES=0x3F " */
130 printk("RES=0x%02x ", (u8
)(ntohl(tcp_flag_word(th
) & TCP_RESERVED_BITS
) >> 22));
131 /* Max length: 32 "CWR ECE URG ACK PSH RST SYN FIN " */
148 /* Max length: 11 "URGP=65535 " */
149 printk("URGP=%u ", ntohs(th
->urg_ptr
));
151 if ((logflags
& IPT_LOG_TCPOPT
)
152 && th
->doff
* 4 > sizeof(struct tcphdr
)) {
153 unsigned char _opt
[4 * 15 - sizeof(struct tcphdr
)];
155 unsigned int i
, optsize
;
157 optsize
= th
->doff
* 4 - sizeof(struct tcphdr
);
158 op
= skb_header_pointer(skb
,
159 iphoff
+ih
->ihl
*4+sizeof(_tcph
),
166 /* Max length: 127 "OPT (" 15*4*2chars ") " */
168 for (i
= 0; i
< optsize
; i
++)
169 printk("%02X", op
[i
]);
175 struct udphdr _udph
, *uh
;
177 /* Max length: 10 "PROTO=UDP " */
178 printk("PROTO=UDP ");
180 if (ntohs(ih
->frag_off
) & IP_OFFSET
)
183 /* Max length: 25 "INCOMPLETE [65535 bytes] " */
184 uh
= skb_header_pointer(skb
, iphoff
+ih
->ihl
*4,
185 sizeof(_udph
), &_udph
);
187 printk("INCOMPLETE [%u bytes] ",
188 skb
->len
- iphoff
- ih
->ihl
*4);
192 /* Max length: 20 "SPT=65535 DPT=65535 " */
193 printk("SPT=%u DPT=%u LEN=%u ",
194 ntohs(uh
->source
), ntohs(uh
->dest
),
199 struct icmphdr _icmph
, *ich
;
200 static const size_t required_len
[NR_ICMP_TYPES
+1]
201 = { [ICMP_ECHOREPLY
] = 4,
203 = 8 + sizeof(struct iphdr
),
205 = 8 + sizeof(struct iphdr
),
207 = 8 + sizeof(struct iphdr
),
210 = 8 + sizeof(struct iphdr
),
212 = 8 + sizeof(struct iphdr
),
213 [ICMP_TIMESTAMP
] = 20,
214 [ICMP_TIMESTAMPREPLY
] = 20,
216 [ICMP_ADDRESSREPLY
] = 12 };
218 /* Max length: 11 "PROTO=ICMP " */
219 printk("PROTO=ICMP ");
221 if (ntohs(ih
->frag_off
) & IP_OFFSET
)
224 /* Max length: 25 "INCOMPLETE [65535 bytes] " */
225 ich
= skb_header_pointer(skb
, iphoff
+ ih
->ihl
* 4,
226 sizeof(_icmph
), &_icmph
);
228 printk("INCOMPLETE [%u bytes] ",
229 skb
->len
- iphoff
- ih
->ihl
*4);
233 /* Max length: 18 "TYPE=255 CODE=255 " */
234 printk("TYPE=%u CODE=%u ", ich
->type
, ich
->code
);
236 /* Max length: 25 "INCOMPLETE [65535 bytes] " */
237 if (ich
->type
<= NR_ICMP_TYPES
238 && required_len
[ich
->type
]
239 && skb
->len
-iphoff
-ih
->ihl
*4 < required_len
[ich
->type
]) {
240 printk("INCOMPLETE [%u bytes] ",
241 skb
->len
- iphoff
- ih
->ihl
*4);
248 /* Max length: 19 "ID=65535 SEQ=65535 " */
249 printk("ID=%u SEQ=%u ",
250 ntohs(ich
->un
.echo
.id
),
251 ntohs(ich
->un
.echo
.sequence
));
254 case ICMP_PARAMETERPROB
:
255 /* Max length: 14 "PARAMETER=255 " */
256 printk("PARAMETER=%u ",
257 ntohl(ich
->un
.gateway
) >> 24);
260 /* Max length: 24 "GATEWAY=255.255.255.255 " */
261 printk("GATEWAY=%u.%u.%u.%u ",
262 NIPQUAD(ich
->un
.gateway
));
264 case ICMP_DEST_UNREACH
:
265 case ICMP_SOURCE_QUENCH
:
266 case ICMP_TIME_EXCEEDED
:
267 /* Max length: 3+maxlen */
268 if (!iphoff
) { /* Only recurse once. */
270 dump_packet(info
, skb
,
271 iphoff
+ ih
->ihl
*4+sizeof(_icmph
));
275 /* Max length: 10 "MTU=65535 " */
276 if (ich
->type
== ICMP_DEST_UNREACH
277 && ich
->code
== ICMP_FRAG_NEEDED
)
278 printk("MTU=%u ", ntohs(ich
->un
.frag
.mtu
));
284 struct ip_auth_hdr _ahdr
, *ah
;
286 if (ntohs(ih
->frag_off
) & IP_OFFSET
)
289 /* Max length: 9 "PROTO=AH " */
292 /* Max length: 25 "INCOMPLETE [65535 bytes] " */
293 ah
= skb_header_pointer(skb
, iphoff
+ih
->ihl
*4,
294 sizeof(_ahdr
), &_ahdr
);
296 printk("INCOMPLETE [%u bytes] ",
297 skb
->len
- iphoff
- ih
->ihl
*4);
301 /* Length: 15 "SPI=0xF1234567 " */
302 printk("SPI=0x%x ", ntohl(ah
->spi
));
306 struct ip_esp_hdr _esph
, *eh
;
308 /* Max length: 10 "PROTO=ESP " */
309 printk("PROTO=ESP ");
311 if (ntohs(ih
->frag_off
) & IP_OFFSET
)
314 /* Max length: 25 "INCOMPLETE [65535 bytes] " */
315 eh
= skb_header_pointer(skb
, iphoff
+ih
->ihl
*4,
316 sizeof(_esph
), &_esph
);
318 printk("INCOMPLETE [%u bytes] ",
319 skb
->len
- iphoff
- ih
->ihl
*4);
323 /* Length: 15 "SPI=0xF1234567 " */
324 printk("SPI=0x%x ", ntohl(eh
->spi
));
327 /* Max length: 10 "PROTO 255 " */
329 printk("PROTO=%u ", ih
->protocol
);
332 /* Max length: 15 "UID=4294967295 " */
333 if ((logflags
& IPT_LOG_UID
) && !iphoff
&& skb
->sk
) {
334 read_lock_bh(&skb
->sk
->sk_callback_lock
);
335 if (skb
->sk
->sk_socket
&& skb
->sk
->sk_socket
->file
)
336 printk("UID=%u ", skb
->sk
->sk_socket
->file
->f_uid
);
337 read_unlock_bh(&skb
->sk
->sk_callback_lock
);
340 /* Proto Max log string length */
341 /* IP: 40+46+6+11+127 = 230 */
342 /* TCP: 10+max(25,20+30+13+9+32+11+127) = 252 */
343 /* UDP: 10+max(25,20) = 35 */
344 /* ICMP: 11+max(25, 18+25+max(19,14,24+3+n+10,3+n+10)) = 91+n */
345 /* ESP: 10+max(25)+15 = 50 */
346 /* AH: 9+max(25)+15 = 49 */
349 /* (ICMP allows recursion one level deep) */
350 /* maxlen = IP + ICMP + IP + max(TCP,UDP,ICMP,unknown) */
351 /* maxlen = 230+ 91 + 230 + 252 = 803 */
354 static struct nf_loginfo default_loginfo
= {
355 .type
= NF_LOG_TYPE_LOG
,
359 .logflags
= NF_LOG_MASK
,
365 ipt_log_packet(unsigned int pf
,
366 unsigned int hooknum
,
367 const struct sk_buff
*skb
,
368 const struct net_device
*in
,
369 const struct net_device
*out
,
370 const struct nf_loginfo
*loginfo
,
374 loginfo
= &default_loginfo
;
376 spin_lock_bh(&log_lock
);
377 printk("<%d>%sIN=%s OUT=%s ", loginfo
->u
.log
.level
,
380 out
? out
->name
: "");
381 #ifdef CONFIG_BRIDGE_NETFILTER
382 if (skb
->nf_bridge
) {
383 struct net_device
*physindev
= skb
->nf_bridge
->physindev
;
384 struct net_device
*physoutdev
= skb
->nf_bridge
->physoutdev
;
386 if (physindev
&& in
!= physindev
)
387 printk("PHYSIN=%s ", physindev
->name
);
388 if (physoutdev
&& out
!= physoutdev
)
389 printk("PHYSOUT=%s ", physoutdev
->name
);
394 /* MAC logging for input chain only. */
396 if (skb
->dev
&& skb
->dev
->hard_header_len
397 && skb
->mac
.raw
!= (void*)skb
->nh
.iph
) {
399 unsigned char *p
= skb
->mac
.raw
;
400 for (i
= 0; i
< skb
->dev
->hard_header_len
; i
++,p
++)
402 i
==skb
->dev
->hard_header_len
- 1
408 dump_packet(loginfo
, skb
, 0);
410 spin_unlock_bh(&log_lock
);
414 ipt_log_target(struct sk_buff
**pskb
,
415 const struct net_device
*in
,
416 const struct net_device
*out
,
417 unsigned int hooknum
,
418 const struct xt_target
*target
,
419 const void *targinfo
)
421 const struct ipt_log_info
*loginfo
= targinfo
;
422 struct nf_loginfo li
;
424 li
.type
= NF_LOG_TYPE_LOG
;
425 li
.u
.log
.level
= loginfo
->level
;
426 li
.u
.log
.logflags
= loginfo
->logflags
;
428 if (loginfo
->logflags
& IPT_LOG_NFLOG
)
429 nf_log_packet(PF_INET
, hooknum
, *pskb
, in
, out
, &li
,
430 "%s", loginfo
->prefix
);
432 ipt_log_packet(PF_INET
, hooknum
, *pskb
, in
, out
, &li
,
438 static int ipt_log_checkentry(const char *tablename
,
440 const struct xt_target
*target
,
442 unsigned int hook_mask
)
444 const struct ipt_log_info
*loginfo
= targinfo
;
446 if (loginfo
->level
>= 8) {
447 DEBUGP("LOG: level %u >= 8\n", loginfo
->level
);
450 if (loginfo
->prefix
[sizeof(loginfo
->prefix
)-1] != '\0') {
451 DEBUGP("LOG: prefix term %i\n",
452 loginfo
->prefix
[sizeof(loginfo
->prefix
)-1]);
458 static struct ipt_target ipt_log_reg
= {
460 .target
= ipt_log_target
,
461 .targetsize
= sizeof(struct ipt_log_info
),
462 .checkentry
= ipt_log_checkentry
,
466 static struct nf_logger ipt_log_logger
={
468 .logfn
= &ipt_log_packet
,
472 static int __init
ipt_log_init(void)
474 if (ipt_register_target(&ipt_log_reg
))
476 if (nf_log_register(PF_INET
, &ipt_log_logger
) < 0) {
477 printk(KERN_WARNING
"ipt_LOG: not logging via system console "
478 "since somebody else already registered for PF_INET\n");
479 /* we cannot make module load fail here, since otherwise
480 * iptables userspace would abort */
486 static void __exit
ipt_log_fini(void)
488 nf_log_unregister_logger(&ipt_log_logger
);
489 ipt_unregister_target(&ipt_log_reg
);
492 module_init(ipt_log_init
);
493 module_exit(ipt_log_fini
);