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>
16 #include <linux/if_arp.h>
21 #include <net/route.h>
23 #include <linux/netfilter.h>
24 #include <linux/netfilter/x_tables.h>
25 #include <linux/netfilter_ipv4/ipt_LOG.h>
26 #include <net/netfilter/xt_log.h>
28 MODULE_LICENSE("GPL");
29 MODULE_AUTHOR("Netfilter Core Team <coreteam@netfilter.org>");
30 MODULE_DESCRIPTION("Xtables: IPv4 packet logging to syslog");
35 #define DEBUGP(format, args...)
38 /* One level of recursion won't kill us */
39 static void dump_packet(struct sbuff
*m
,
40 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
);
54 sb_add(m
, "TRUNCATED");
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 sb_add(m
, "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 sb_add(m
, "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 sb_add(m
, "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
),
90 sb_add(m
, "TRUNCATED");
94 /* Max length: 127 "OPT (" 15*4*2chars ") " */
96 for (i
= 0; i
< optsize
; i
++)
97 sb_add(m
, "%02X", op
[i
]);
101 switch (ih
->protocol
) {
103 struct tcphdr _tcph
, *th
;
105 /* Max length: 10 "PROTO=TCP " */
106 sb_add(m
, "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 sb_add(m
, "INCOMPLETE [%u bytes] ",
116 skb
->len
- iphoff
- ih
->ihl
*4);
120 /* Max length: 20 "SPT=65535 DPT=65535 " */
121 sb_add(m
, "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 sb_add(m
, "SEQ=%u ACK=%u ",
126 ntohl(th
->seq
), ntohl(th
->ack_seq
));
127 /* Max length: 13 "WINDOW=65535 " */
128 sb_add(m
, "WINDOW=%u ", ntohs(th
->window
));
129 /* Max length: 9 "RES=0x3F " */
130 sb_add(m
, "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 sb_add(m
, "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
),
162 sb_add(m
, "TRUNCATED");
166 /* Max length: 127 "OPT (" 15*4*2chars ") " */
168 for (i
= 0; i
< optsize
; i
++)
169 sb_add(m
, "%02X", op
[i
]);
175 case IPPROTO_UDPLITE
: {
176 struct udphdr _udph
, *uh
;
178 if (ih
->protocol
== IPPROTO_UDP
)
179 /* Max length: 10 "PROTO=UDP " */
180 sb_add(m
, "PROTO=UDP " );
181 else /* Max length: 14 "PROTO=UDPLITE " */
182 sb_add(m
, "PROTO=UDPLITE ");
184 if (ntohs(ih
->frag_off
) & IP_OFFSET
)
187 /* Max length: 25 "INCOMPLETE [65535 bytes] " */
188 uh
= skb_header_pointer(skb
, iphoff
+ih
->ihl
*4,
189 sizeof(_udph
), &_udph
);
191 sb_add(m
, "INCOMPLETE [%u bytes] ",
192 skb
->len
- iphoff
- ih
->ihl
*4);
196 /* Max length: 20 "SPT=65535 DPT=65535 " */
197 sb_add(m
, "SPT=%u DPT=%u LEN=%u ",
198 ntohs(uh
->source
), ntohs(uh
->dest
),
203 struct icmphdr _icmph
, *ich
;
204 static const size_t required_len
[NR_ICMP_TYPES
+1]
205 = { [ICMP_ECHOREPLY
] = 4,
207 = 8 + sizeof(struct iphdr
),
209 = 8 + sizeof(struct iphdr
),
211 = 8 + sizeof(struct iphdr
),
214 = 8 + sizeof(struct iphdr
),
216 = 8 + sizeof(struct iphdr
),
217 [ICMP_TIMESTAMP
] = 20,
218 [ICMP_TIMESTAMPREPLY
] = 20,
220 [ICMP_ADDRESSREPLY
] = 12 };
222 /* Max length: 11 "PROTO=ICMP " */
223 sb_add(m
, "PROTO=ICMP ");
225 if (ntohs(ih
->frag_off
) & IP_OFFSET
)
228 /* Max length: 25 "INCOMPLETE [65535 bytes] " */
229 ich
= skb_header_pointer(skb
, iphoff
+ ih
->ihl
* 4,
230 sizeof(_icmph
), &_icmph
);
232 sb_add(m
, "INCOMPLETE [%u bytes] ",
233 skb
->len
- iphoff
- ih
->ihl
*4);
237 /* Max length: 18 "TYPE=255 CODE=255 " */
238 sb_add(m
, "TYPE=%u CODE=%u ", ich
->type
, ich
->code
);
240 /* Max length: 25 "INCOMPLETE [65535 bytes] " */
241 if (ich
->type
<= NR_ICMP_TYPES
242 && required_len
[ich
->type
]
243 && skb
->len
-iphoff
-ih
->ihl
*4 < required_len
[ich
->type
]) {
244 sb_add(m
, "INCOMPLETE [%u bytes] ",
245 skb
->len
- iphoff
- ih
->ihl
*4);
252 /* Max length: 19 "ID=65535 SEQ=65535 " */
253 sb_add(m
, "ID=%u SEQ=%u ",
254 ntohs(ich
->un
.echo
.id
),
255 ntohs(ich
->un
.echo
.sequence
));
258 case ICMP_PARAMETERPROB
:
259 /* Max length: 14 "PARAMETER=255 " */
260 sb_add(m
, "PARAMETER=%u ",
261 ntohl(ich
->un
.gateway
) >> 24);
264 /* Max length: 24 "GATEWAY=255.255.255.255 " */
265 sb_add(m
, "GATEWAY=%u.%u.%u.%u ",
266 NIPQUAD(ich
->un
.gateway
));
268 case ICMP_DEST_UNREACH
:
269 case ICMP_SOURCE_QUENCH
:
270 case ICMP_TIME_EXCEEDED
:
271 /* Max length: 3+maxlen */
272 if (!iphoff
) { /* Only recurse once. */
274 dump_packet(m
, info
, skb
,
275 iphoff
+ ih
->ihl
*4+sizeof(_icmph
));
279 /* Max length: 10 "MTU=65535 " */
280 if (ich
->type
== ICMP_DEST_UNREACH
281 && ich
->code
== ICMP_FRAG_NEEDED
)
282 sb_add(m
, "MTU=%u ", ntohs(ich
->un
.frag
.mtu
));
288 struct ip_auth_hdr _ahdr
, *ah
;
290 if (ntohs(ih
->frag_off
) & IP_OFFSET
)
293 /* Max length: 9 "PROTO=AH " */
294 sb_add(m
, "PROTO=AH ");
296 /* Max length: 25 "INCOMPLETE [65535 bytes] " */
297 ah
= skb_header_pointer(skb
, iphoff
+ih
->ihl
*4,
298 sizeof(_ahdr
), &_ahdr
);
300 sb_add(m
, "INCOMPLETE [%u bytes] ",
301 skb
->len
- iphoff
- ih
->ihl
*4);
305 /* Length: 15 "SPI=0xF1234567 " */
306 sb_add(m
, "SPI=0x%x ", ntohl(ah
->spi
));
310 struct ip_esp_hdr _esph
, *eh
;
312 /* Max length: 10 "PROTO=ESP " */
313 sb_add(m
, "PROTO=ESP ");
315 if (ntohs(ih
->frag_off
) & IP_OFFSET
)
318 /* Max length: 25 "INCOMPLETE [65535 bytes] " */
319 eh
= skb_header_pointer(skb
, iphoff
+ih
->ihl
*4,
320 sizeof(_esph
), &_esph
);
322 sb_add(m
, "INCOMPLETE [%u bytes] ",
323 skb
->len
- iphoff
- ih
->ihl
*4);
327 /* Length: 15 "SPI=0xF1234567 " */
328 sb_add(m
, "SPI=0x%x ", ntohl(eh
->spi
));
331 /* Max length: 10 "PROTO 255 " */
333 sb_add(m
, "PROTO=%u ", ih
->protocol
);
336 /* Max length: 15 "UID=4294967295 " */
337 if ((logflags
& IPT_LOG_UID
) && !iphoff
&& skb
->sk
) {
338 read_lock_bh(&skb
->sk
->sk_callback_lock
);
339 if (skb
->sk
->sk_socket
&& skb
->sk
->sk_socket
->file
)
340 sb_add(m
, "UID=%u GID=%u ",
341 skb
->sk
->sk_socket
->file
->f_uid
,
342 skb
->sk
->sk_socket
->file
->f_gid
);
343 read_unlock_bh(&skb
->sk
->sk_callback_lock
);
346 /* Max length: 16 "MARK=0xFFFFFFFF " */
347 if (!iphoff
&& skb
->mark
)
348 sb_add(m
, "MARK=0x%x ", skb
->mark
);
350 /* Proto Max log string length */
351 /* IP: 40+46+6+11+127 = 230 */
352 /* TCP: 10+max(25,20+30+13+9+32+11+127) = 252 */
353 /* UDP: 10+max(25,20) = 35 */
354 /* UDPLITE: 14+max(25,20) = 39 */
355 /* ICMP: 11+max(25, 18+25+max(19,14,24+3+n+10,3+n+10)) = 91+n */
356 /* ESP: 10+max(25)+15 = 50 */
357 /* AH: 9+max(25)+15 = 49 */
360 /* (ICMP allows recursion one level deep) */
361 /* maxlen = IP + ICMP + IP + max(TCP,UDP,ICMP,unknown) */
362 /* maxlen = 230+ 91 + 230 + 252 = 803 */
365 static void dump_mac_header(struct sbuff
*m
,
366 const struct nf_loginfo
*info
,
367 const struct sk_buff
*skb
)
369 struct net_device
*dev
= skb
->dev
;
370 unsigned int logflags
= 0;
372 if (info
->type
== NF_LOG_TYPE_LOG
)
373 logflags
= info
->u
.log
.logflags
;
375 if (!(logflags
& IPT_LOG_MACDECODE
))
380 sb_add(m
, "MACSRC=%pM MACDST=%pM MACPROTO=%04x ",
381 eth_hdr(skb
)->h_source
, eth_hdr(skb
)->h_dest
,
382 ntohs(eth_hdr(skb
)->h_proto
));
390 if (dev
->hard_header_len
&&
391 skb
->mac_header
!= skb
->network_header
) {
392 const unsigned char *p
= skb_mac_header(skb
);
395 sb_add(m
, "%02x", *p
++);
396 for (i
= 1; i
< dev
->hard_header_len
; i
++, p
++)
397 sb_add(m
, ":%02x", *p
);
402 static struct nf_loginfo default_loginfo
= {
403 .type
= NF_LOG_TYPE_LOG
,
407 .logflags
= NF_LOG_MASK
,
413 ipt_log_packet(unsigned int pf
,
414 unsigned int hooknum
,
415 const struct sk_buff
*skb
,
416 const struct net_device
*in
,
417 const struct net_device
*out
,
418 const struct nf_loginfo
*loginfo
,
421 struct sbuff
*m
= sb_open();
424 loginfo
= &default_loginfo
;
426 sb_add(m
, "<%d>%sIN=%s OUT=%s ", loginfo
->u
.log
.level
,
429 out
? out
->name
: "");
430 #ifdef CONFIG_BRIDGE_NETFILTER
431 if (skb
->nf_bridge
) {
432 struct net_device
*physindev
= skb
->nf_bridge
->physindev
;
433 struct net_device
*physoutdev
= skb
->nf_bridge
->physoutdev
;
435 if (physindev
&& in
!= physindev
)
436 sb_add(m
, "PHYSIN=%s ", physindev
->name
);
437 if (physoutdev
&& out
!= physoutdev
)
438 sb_add(m
, "PHYSOUT=%s ", physoutdev
->name
);
442 /* MAC logging for input path only. */
444 dump_mac_header(m
, loginfo
, skb
);
446 dump_packet(m
, loginfo
, skb
, 0);
452 ipt_log_target(struct sk_buff
*skb
,
453 const struct net_device
*in
,
454 const struct net_device
*out
,
455 unsigned int hooknum
,
456 const struct xt_target
*target
,
457 const void *targinfo
)
459 const struct ipt_log_info
*loginfo
= targinfo
;
460 struct nf_loginfo li
;
462 li
.type
= NF_LOG_TYPE_LOG
;
463 li
.u
.log
.level
= loginfo
->level
;
464 li
.u
.log
.logflags
= loginfo
->logflags
;
466 ipt_log_packet(PF_INET
, hooknum
, skb
, in
, out
, &li
,
471 static int ipt_log_checkentry(const char *tablename
,
473 const struct xt_target
*target
,
475 unsigned int hook_mask
)
477 const struct ipt_log_info
*loginfo
= targinfo
;
479 if (loginfo
->level
>= 8) {
480 DEBUGP("LOG: level %u >= 8\n", loginfo
->level
);
483 if (loginfo
->prefix
[sizeof(loginfo
->prefix
)-1] != '\0') {
484 DEBUGP("LOG: prefix term %i\n",
485 loginfo
->prefix
[sizeof(loginfo
->prefix
)-1]);
491 static struct xt_target ipt_log_reg
= {
494 .target
= ipt_log_target
,
495 .targetsize
= sizeof(struct ipt_log_info
),
496 .checkentry
= ipt_log_checkentry
,
500 static struct nf_logger ipt_log_logger
={
502 .logfn
= &ipt_log_packet
,
506 static int __init
ipt_log_init(void)
510 ret
= xt_register_target(&ipt_log_reg
);
513 nf_log_register(PF_INET
, &ipt_log_logger
);
517 static void __exit
ipt_log_fini(void)
519 nf_log_unregister(&ipt_log_logger
);
520 xt_unregister_target(&ipt_log_reg
);
523 module_init(ipt_log_init
);
524 module_exit(ipt_log_fini
);