1 /* (C) 1999-2001 Paul `Rusty' Russell
2 * (C) 2002-2004 Netfilter Core Team <coreteam@netfilter.org>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10 #include <linux/module.h>
11 #include <linux/spinlock.h>
12 #include <linux/skbuff.h>
13 #include <linux/if_arp.h>
19 #include <net/route.h>
21 #include <linux/netfilter.h>
22 #include <linux/netfilter_ipv6/ip6_tables.h>
23 #include <linux/netfilter/xt_LOG.h>
24 #include <net/netfilter/nf_log.h>
26 static struct nf_loginfo default_loginfo
= {
27 .type
= NF_LOG_TYPE_LOG
,
31 .logflags
= NF_LOG_MASK
,
36 /* One level of recursion won't kill us */
37 static void dump_ipv6_packet(struct nf_log_buf
*m
,
38 const struct nf_loginfo
*info
,
39 const struct sk_buff
*skb
, unsigned int ip6hoff
,
45 const struct ipv6hdr
*ih
;
47 unsigned int hdrlen
= 0;
48 unsigned int logflags
;
50 if (info
->type
== NF_LOG_TYPE_LOG
)
51 logflags
= info
->u
.log
.logflags
;
53 logflags
= NF_LOG_MASK
;
55 ih
= skb_header_pointer(skb
, ip6hoff
, sizeof(_ip6h
), &_ip6h
);
57 nf_log_buf_add(m
, "TRUNCATED");
61 /* Max length: 88 "SRC=0000.0000.0000.0000.0000.0000.0000.0000 DST=0000.0000.0000.0000.0000.0000.0000.0000 " */
62 nf_log_buf_add(m
, "SRC=%pI6 DST=%pI6 ", &ih
->saddr
, &ih
->daddr
);
64 /* Max length: 44 "LEN=65535 TC=255 HOPLIMIT=255 FLOWLBL=FFFFF " */
65 nf_log_buf_add(m
, "LEN=%Zu TC=%u HOPLIMIT=%u FLOWLBL=%u ",
66 ntohs(ih
->payload_len
) + sizeof(struct ipv6hdr
),
67 (ntohl(*(__be32
*)ih
) & 0x0ff00000) >> 20,
69 (ntohl(*(__be32
*)ih
) & 0x000fffff));
72 ptr
= ip6hoff
+ sizeof(struct ipv6hdr
);
73 currenthdr
= ih
->nexthdr
;
74 while (currenthdr
!= NEXTHDR_NONE
&& ip6t_ext_hdr(currenthdr
)) {
75 struct ipv6_opt_hdr _hdr
;
76 const struct ipv6_opt_hdr
*hp
;
78 hp
= skb_header_pointer(skb
, ptr
, sizeof(_hdr
), &_hdr
);
80 nf_log_buf_add(m
, "TRUNCATED");
84 /* Max length: 48 "OPT (...) " */
85 if (logflags
& XT_LOG_IPOPT
)
86 nf_log_buf_add(m
, "OPT ( ");
89 case IPPROTO_FRAGMENT
: {
90 struct frag_hdr _fhdr
;
91 const struct frag_hdr
*fh
;
93 nf_log_buf_add(m
, "FRAG:");
94 fh
= skb_header_pointer(skb
, ptr
, sizeof(_fhdr
),
97 nf_log_buf_add(m
, "TRUNCATED ");
101 /* Max length: 6 "65535 " */
102 nf_log_buf_add(m
, "%u ", ntohs(fh
->frag_off
) & 0xFFF8);
104 /* Max length: 11 "INCOMPLETE " */
105 if (fh
->frag_off
& htons(0x0001))
106 nf_log_buf_add(m
, "INCOMPLETE ");
108 nf_log_buf_add(m
, "ID:%08x ",
109 ntohl(fh
->identification
));
111 if (ntohs(fh
->frag_off
) & 0xFFF8)
118 case IPPROTO_DSTOPTS
:
119 case IPPROTO_ROUTING
:
120 case IPPROTO_HOPOPTS
:
122 if (logflags
& XT_LOG_IPOPT
)
123 nf_log_buf_add(m
, ")");
126 hdrlen
= ipv6_optlen(hp
);
130 if (logflags
& XT_LOG_IPOPT
) {
131 struct ip_auth_hdr _ahdr
;
132 const struct ip_auth_hdr
*ah
;
134 /* Max length: 3 "AH " */
135 nf_log_buf_add(m
, "AH ");
138 nf_log_buf_add(m
, ")");
142 ah
= skb_header_pointer(skb
, ptr
, sizeof(_ahdr
),
146 * Max length: 26 "INCOMPLETE [65535
149 nf_log_buf_add(m
, "INCOMPLETE [%u bytes] )",
154 /* Length: 15 "SPI=0xF1234567 */
155 nf_log_buf_add(m
, "SPI=0x%x ", ntohl(ah
->spi
));
159 hdrlen
= (hp
->hdrlen
+2)<<2;
162 if (logflags
& XT_LOG_IPOPT
) {
163 struct ip_esp_hdr _esph
;
164 const struct ip_esp_hdr
*eh
;
166 /* Max length: 4 "ESP " */
167 nf_log_buf_add(m
, "ESP ");
170 nf_log_buf_add(m
, ")");
175 * Max length: 26 "INCOMPLETE [65535 bytes] )"
177 eh
= skb_header_pointer(skb
, ptr
, sizeof(_esph
),
180 nf_log_buf_add(m
, "INCOMPLETE [%u bytes] )",
185 /* Length: 16 "SPI=0xF1234567 )" */
186 nf_log_buf_add(m
, "SPI=0x%x )",
191 /* Max length: 20 "Unknown Ext Hdr 255" */
192 nf_log_buf_add(m
, "Unknown Ext Hdr %u", currenthdr
);
195 if (logflags
& XT_LOG_IPOPT
)
196 nf_log_buf_add(m
, ") ");
198 currenthdr
= hp
->nexthdr
;
202 switch (currenthdr
) {
204 if (nf_log_dump_tcp_header(m
, skb
, currenthdr
, fragment
,
209 case IPPROTO_UDPLITE
:
210 if (nf_log_dump_udp_header(m
, skb
, currenthdr
, fragment
, ptr
))
213 case IPPROTO_ICMPV6
: {
214 struct icmp6hdr _icmp6h
;
215 const struct icmp6hdr
*ic
;
217 /* Max length: 13 "PROTO=ICMPv6 " */
218 nf_log_buf_add(m
, "PROTO=ICMPv6 ");
223 /* Max length: 25 "INCOMPLETE [65535 bytes] " */
224 ic
= skb_header_pointer(skb
, ptr
, sizeof(_icmp6h
), &_icmp6h
);
226 nf_log_buf_add(m
, "INCOMPLETE [%u bytes] ",
231 /* Max length: 18 "TYPE=255 CODE=255 " */
232 nf_log_buf_add(m
, "TYPE=%u CODE=%u ",
233 ic
->icmp6_type
, ic
->icmp6_code
);
235 switch (ic
->icmp6_type
) {
236 case ICMPV6_ECHO_REQUEST
:
237 case ICMPV6_ECHO_REPLY
:
238 /* Max length: 19 "ID=65535 SEQ=65535 " */
239 nf_log_buf_add(m
, "ID=%u SEQ=%u ",
240 ntohs(ic
->icmp6_identifier
),
241 ntohs(ic
->icmp6_sequence
));
243 case ICMPV6_MGM_QUERY
:
244 case ICMPV6_MGM_REPORT
:
245 case ICMPV6_MGM_REDUCTION
:
248 case ICMPV6_PARAMPROB
:
249 /* Max length: 17 "POINTER=ffffffff " */
250 nf_log_buf_add(m
, "POINTER=%08x ",
251 ntohl(ic
->icmp6_pointer
));
253 case ICMPV6_DEST_UNREACH
:
254 case ICMPV6_PKT_TOOBIG
:
255 case ICMPV6_TIME_EXCEED
:
256 /* Max length: 3+maxlen */
258 nf_log_buf_add(m
, "[");
259 dump_ipv6_packet(m
, info
, skb
,
260 ptr
+ sizeof(_icmp6h
), 0);
261 nf_log_buf_add(m
, "] ");
264 /* Max length: 10 "MTU=65535 " */
265 if (ic
->icmp6_type
== ICMPV6_PKT_TOOBIG
) {
266 nf_log_buf_add(m
, "MTU=%u ",
267 ntohl(ic
->icmp6_mtu
));
272 /* Max length: 10 "PROTO=255 " */
274 nf_log_buf_add(m
, "PROTO=%u ", currenthdr
);
277 /* Max length: 15 "UID=4294967295 " */
278 if ((logflags
& XT_LOG_UID
) && recurse
)
279 nf_log_dump_sk_uid_gid(m
, skb
->sk
);
281 /* Max length: 16 "MARK=0xFFFFFFFF " */
282 if (recurse
&& skb
->mark
)
283 nf_log_buf_add(m
, "MARK=0x%x ", skb
->mark
);
286 static void dump_ipv6_mac_header(struct nf_log_buf
*m
,
287 const struct nf_loginfo
*info
,
288 const struct sk_buff
*skb
)
290 struct net_device
*dev
= skb
->dev
;
291 unsigned int logflags
= 0;
293 if (info
->type
== NF_LOG_TYPE_LOG
)
294 logflags
= info
->u
.log
.logflags
;
296 if (!(logflags
& XT_LOG_MACDECODE
))
301 nf_log_buf_add(m
, "MACSRC=%pM MACDST=%pM MACPROTO=%04x ",
302 eth_hdr(skb
)->h_source
, eth_hdr(skb
)->h_dest
,
303 ntohs(eth_hdr(skb
)->h_proto
));
310 nf_log_buf_add(m
, "MAC=");
311 if (dev
->hard_header_len
&&
312 skb
->mac_header
!= skb
->network_header
) {
313 const unsigned char *p
= skb_mac_header(skb
);
314 unsigned int len
= dev
->hard_header_len
;
317 if (dev
->type
== ARPHRD_SIT
) {
325 nf_log_buf_add(m
, "%02x", *p
++);
326 for (i
= 1; i
< len
; i
++)
327 nf_log_buf_add(m
, ":%02x", *p
++);
329 nf_log_buf_add(m
, " ");
331 if (dev
->type
== ARPHRD_SIT
) {
332 const struct iphdr
*iph
=
333 (struct iphdr
*)skb_mac_header(skb
);
334 nf_log_buf_add(m
, "TUNNEL=%pI4->%pI4 ", &iph
->saddr
,
338 nf_log_buf_add(m
, " ");
342 static void nf_log_ip6_packet(struct net
*net
, u_int8_t pf
,
343 unsigned int hooknum
, const struct sk_buff
*skb
,
344 const struct net_device
*in
,
345 const struct net_device
*out
,
346 const struct nf_loginfo
*loginfo
,
349 struct nf_log_buf
*m
;
351 /* FIXME: Disabled from containers until syslog ns is supported */
352 if (!net_eq(net
, &init_net
))
355 m
= nf_log_buf_open();
358 loginfo
= &default_loginfo
;
360 nf_log_dump_packet_common(m
, pf
, hooknum
, skb
, in
, out
,
364 dump_ipv6_mac_header(m
, loginfo
, skb
);
366 dump_ipv6_packet(m
, loginfo
, skb
, skb_network_offset(skb
), 1);
371 static struct nf_logger nf_ip6_logger __read_mostly
= {
372 .name
= "nf_log_ipv6",
373 .type
= NF_LOG_TYPE_LOG
,
374 .logfn
= nf_log_ip6_packet
,
378 static int __net_init
nf_log_ipv6_net_init(struct net
*net
)
380 nf_log_set(net
, NFPROTO_IPV6
, &nf_ip6_logger
);
384 static void __net_exit
nf_log_ipv6_net_exit(struct net
*net
)
386 nf_log_unset(net
, &nf_ip6_logger
);
389 static struct pernet_operations nf_log_ipv6_net_ops
= {
390 .init
= nf_log_ipv6_net_init
,
391 .exit
= nf_log_ipv6_net_exit
,
394 static int __init
nf_log_ipv6_init(void)
398 ret
= register_pernet_subsys(&nf_log_ipv6_net_ops
);
402 ret
= nf_log_register(NFPROTO_IPV6
, &nf_ip6_logger
);
404 pr_err("failed to register logger\n");
411 unregister_pernet_subsys(&nf_log_ipv6_net_ops
);
415 static void __exit
nf_log_ipv6_exit(void)
417 unregister_pernet_subsys(&nf_log_ipv6_net_ops
);
418 nf_log_unregister(&nf_ip6_logger
);
421 module_init(nf_log_ipv6_init
);
422 module_exit(nf_log_ipv6_exit
);
424 MODULE_AUTHOR("Netfilter Core Team <coreteam@netfilter.org>");
425 MODULE_DESCRIPTION("Netfilter IPv6 packet logging");
426 MODULE_LICENSE("GPL");
427 MODULE_ALIAS_NF_LOGGER(AF_INET6
, 0);