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.
9 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <linux/spinlock.h>
14 #include <linux/skbuff.h>
15 #include <linux/if_arp.h>
21 #include <net/route.h>
23 #include <linux/netfilter.h>
24 #include <linux/netfilter_ipv6/ip6_tables.h>
25 #include <linux/netfilter/xt_LOG.h>
26 #include <net/netfilter/nf_log.h>
28 static struct nf_loginfo default_loginfo
= {
29 .type
= NF_LOG_TYPE_LOG
,
32 .level
= LOGLEVEL_NOTICE
,
33 .logflags
= NF_LOG_MASK
,
38 /* One level of recursion won't kill us */
39 static void dump_ipv6_packet(struct nf_log_buf
*m
,
40 const struct nf_loginfo
*info
,
41 const struct sk_buff
*skb
, unsigned int ip6hoff
,
47 const struct ipv6hdr
*ih
;
49 unsigned int hdrlen
= 0;
50 unsigned int logflags
;
52 if (info
->type
== NF_LOG_TYPE_LOG
)
53 logflags
= info
->u
.log
.logflags
;
55 logflags
= NF_LOG_MASK
;
57 ih
= skb_header_pointer(skb
, ip6hoff
, sizeof(_ip6h
), &_ip6h
);
59 nf_log_buf_add(m
, "TRUNCATED");
63 /* Max length: 88 "SRC=0000.0000.0000.0000.0000.0000.0000.0000 DST=0000.0000.0000.0000.0000.0000.0000.0000 " */
64 nf_log_buf_add(m
, "SRC=%pI6 DST=%pI6 ", &ih
->saddr
, &ih
->daddr
);
66 /* Max length: 44 "LEN=65535 TC=255 HOPLIMIT=255 FLOWLBL=FFFFF " */
67 nf_log_buf_add(m
, "LEN=%Zu TC=%u HOPLIMIT=%u FLOWLBL=%u ",
68 ntohs(ih
->payload_len
) + sizeof(struct ipv6hdr
),
69 (ntohl(*(__be32
*)ih
) & 0x0ff00000) >> 20,
71 (ntohl(*(__be32
*)ih
) & 0x000fffff));
74 ptr
= ip6hoff
+ sizeof(struct ipv6hdr
);
75 currenthdr
= ih
->nexthdr
;
76 while (currenthdr
!= NEXTHDR_NONE
&& ip6t_ext_hdr(currenthdr
)) {
77 struct ipv6_opt_hdr _hdr
;
78 const struct ipv6_opt_hdr
*hp
;
80 hp
= skb_header_pointer(skb
, ptr
, sizeof(_hdr
), &_hdr
);
82 nf_log_buf_add(m
, "TRUNCATED");
86 /* Max length: 48 "OPT (...) " */
87 if (logflags
& XT_LOG_IPOPT
)
88 nf_log_buf_add(m
, "OPT ( ");
91 case IPPROTO_FRAGMENT
: {
92 struct frag_hdr _fhdr
;
93 const struct frag_hdr
*fh
;
95 nf_log_buf_add(m
, "FRAG:");
96 fh
= skb_header_pointer(skb
, ptr
, sizeof(_fhdr
),
99 nf_log_buf_add(m
, "TRUNCATED ");
103 /* Max length: 6 "65535 " */
104 nf_log_buf_add(m
, "%u ", ntohs(fh
->frag_off
) & 0xFFF8);
106 /* Max length: 11 "INCOMPLETE " */
107 if (fh
->frag_off
& htons(0x0001))
108 nf_log_buf_add(m
, "INCOMPLETE ");
110 nf_log_buf_add(m
, "ID:%08x ",
111 ntohl(fh
->identification
));
113 if (ntohs(fh
->frag_off
) & 0xFFF8)
120 case IPPROTO_DSTOPTS
:
121 case IPPROTO_ROUTING
:
122 case IPPROTO_HOPOPTS
:
124 if (logflags
& XT_LOG_IPOPT
)
125 nf_log_buf_add(m
, ")");
128 hdrlen
= ipv6_optlen(hp
);
132 if (logflags
& XT_LOG_IPOPT
) {
133 struct ip_auth_hdr _ahdr
;
134 const struct ip_auth_hdr
*ah
;
136 /* Max length: 3 "AH " */
137 nf_log_buf_add(m
, "AH ");
140 nf_log_buf_add(m
, ")");
144 ah
= skb_header_pointer(skb
, ptr
, sizeof(_ahdr
),
148 * Max length: 26 "INCOMPLETE [65535
151 nf_log_buf_add(m
, "INCOMPLETE [%u bytes] )",
156 /* Length: 15 "SPI=0xF1234567 */
157 nf_log_buf_add(m
, "SPI=0x%x ", ntohl(ah
->spi
));
161 hdrlen
= (hp
->hdrlen
+2)<<2;
164 if (logflags
& XT_LOG_IPOPT
) {
165 struct ip_esp_hdr _esph
;
166 const struct ip_esp_hdr
*eh
;
168 /* Max length: 4 "ESP " */
169 nf_log_buf_add(m
, "ESP ");
172 nf_log_buf_add(m
, ")");
177 * Max length: 26 "INCOMPLETE [65535 bytes] )"
179 eh
= skb_header_pointer(skb
, ptr
, sizeof(_esph
),
182 nf_log_buf_add(m
, "INCOMPLETE [%u bytes] )",
187 /* Length: 16 "SPI=0xF1234567 )" */
188 nf_log_buf_add(m
, "SPI=0x%x )",
193 /* Max length: 20 "Unknown Ext Hdr 255" */
194 nf_log_buf_add(m
, "Unknown Ext Hdr %u", currenthdr
);
197 if (logflags
& XT_LOG_IPOPT
)
198 nf_log_buf_add(m
, ") ");
200 currenthdr
= hp
->nexthdr
;
204 switch (currenthdr
) {
206 if (nf_log_dump_tcp_header(m
, skb
, currenthdr
, fragment
,
211 case IPPROTO_UDPLITE
:
212 if (nf_log_dump_udp_header(m
, skb
, currenthdr
, fragment
, ptr
))
215 case IPPROTO_ICMPV6
: {
216 struct icmp6hdr _icmp6h
;
217 const struct icmp6hdr
*ic
;
219 /* Max length: 13 "PROTO=ICMPv6 " */
220 nf_log_buf_add(m
, "PROTO=ICMPv6 ");
225 /* Max length: 25 "INCOMPLETE [65535 bytes] " */
226 ic
= skb_header_pointer(skb
, ptr
, sizeof(_icmp6h
), &_icmp6h
);
228 nf_log_buf_add(m
, "INCOMPLETE [%u bytes] ",
233 /* Max length: 18 "TYPE=255 CODE=255 " */
234 nf_log_buf_add(m
, "TYPE=%u CODE=%u ",
235 ic
->icmp6_type
, ic
->icmp6_code
);
237 switch (ic
->icmp6_type
) {
238 case ICMPV6_ECHO_REQUEST
:
239 case ICMPV6_ECHO_REPLY
:
240 /* Max length: 19 "ID=65535 SEQ=65535 " */
241 nf_log_buf_add(m
, "ID=%u SEQ=%u ",
242 ntohs(ic
->icmp6_identifier
),
243 ntohs(ic
->icmp6_sequence
));
245 case ICMPV6_MGM_QUERY
:
246 case ICMPV6_MGM_REPORT
:
247 case ICMPV6_MGM_REDUCTION
:
250 case ICMPV6_PARAMPROB
:
251 /* Max length: 17 "POINTER=ffffffff " */
252 nf_log_buf_add(m
, "POINTER=%08x ",
253 ntohl(ic
->icmp6_pointer
));
255 case ICMPV6_DEST_UNREACH
:
256 case ICMPV6_PKT_TOOBIG
:
257 case ICMPV6_TIME_EXCEED
:
258 /* Max length: 3+maxlen */
260 nf_log_buf_add(m
, "[");
261 dump_ipv6_packet(m
, info
, skb
,
262 ptr
+ sizeof(_icmp6h
), 0);
263 nf_log_buf_add(m
, "] ");
266 /* Max length: 10 "MTU=65535 " */
267 if (ic
->icmp6_type
== ICMPV6_PKT_TOOBIG
) {
268 nf_log_buf_add(m
, "MTU=%u ",
269 ntohl(ic
->icmp6_mtu
));
274 /* Max length: 10 "PROTO=255 " */
276 nf_log_buf_add(m
, "PROTO=%u ", currenthdr
);
279 /* Max length: 15 "UID=4294967295 " */
280 if ((logflags
& XT_LOG_UID
) && recurse
)
281 nf_log_dump_sk_uid_gid(m
, skb
->sk
);
283 /* Max length: 16 "MARK=0xFFFFFFFF " */
284 if (recurse
&& skb
->mark
)
285 nf_log_buf_add(m
, "MARK=0x%x ", skb
->mark
);
288 static void dump_ipv6_mac_header(struct nf_log_buf
*m
,
289 const struct nf_loginfo
*info
,
290 const struct sk_buff
*skb
)
292 struct net_device
*dev
= skb
->dev
;
293 unsigned int logflags
= 0;
295 if (info
->type
== NF_LOG_TYPE_LOG
)
296 logflags
= info
->u
.log
.logflags
;
298 if (!(logflags
& XT_LOG_MACDECODE
))
303 nf_log_buf_add(m
, "MACSRC=%pM MACDST=%pM MACPROTO=%04x ",
304 eth_hdr(skb
)->h_source
, eth_hdr(skb
)->h_dest
,
305 ntohs(eth_hdr(skb
)->h_proto
));
312 nf_log_buf_add(m
, "MAC=");
313 if (dev
->hard_header_len
&&
314 skb
->mac_header
!= skb
->network_header
) {
315 const unsigned char *p
= skb_mac_header(skb
);
316 unsigned int len
= dev
->hard_header_len
;
319 if (dev
->type
== ARPHRD_SIT
) {
327 nf_log_buf_add(m
, "%02x", *p
++);
328 for (i
= 1; i
< len
; i
++)
329 nf_log_buf_add(m
, ":%02x", *p
++);
331 nf_log_buf_add(m
, " ");
333 if (dev
->type
== ARPHRD_SIT
) {
334 const struct iphdr
*iph
=
335 (struct iphdr
*)skb_mac_header(skb
);
336 nf_log_buf_add(m
, "TUNNEL=%pI4->%pI4 ", &iph
->saddr
,
340 nf_log_buf_add(m
, " ");
344 static void nf_log_ip6_packet(struct net
*net
, u_int8_t pf
,
345 unsigned int hooknum
, const struct sk_buff
*skb
,
346 const struct net_device
*in
,
347 const struct net_device
*out
,
348 const struct nf_loginfo
*loginfo
,
351 struct nf_log_buf
*m
;
353 /* FIXME: Disabled from containers until syslog ns is supported */
354 if (!net_eq(net
, &init_net
))
357 m
= nf_log_buf_open();
360 loginfo
= &default_loginfo
;
362 nf_log_dump_packet_common(m
, pf
, hooknum
, skb
, in
, out
,
366 dump_ipv6_mac_header(m
, loginfo
, skb
);
368 dump_ipv6_packet(m
, loginfo
, skb
, skb_network_offset(skb
), 1);
373 static struct nf_logger nf_ip6_logger __read_mostly
= {
374 .name
= "nf_log_ipv6",
375 .type
= NF_LOG_TYPE_LOG
,
376 .logfn
= nf_log_ip6_packet
,
380 static int __net_init
nf_log_ipv6_net_init(struct net
*net
)
382 nf_log_set(net
, NFPROTO_IPV6
, &nf_ip6_logger
);
386 static void __net_exit
nf_log_ipv6_net_exit(struct net
*net
)
388 nf_log_unset(net
, &nf_ip6_logger
);
391 static struct pernet_operations nf_log_ipv6_net_ops
= {
392 .init
= nf_log_ipv6_net_init
,
393 .exit
= nf_log_ipv6_net_exit
,
396 static int __init
nf_log_ipv6_init(void)
400 ret
= register_pernet_subsys(&nf_log_ipv6_net_ops
);
404 ret
= nf_log_register(NFPROTO_IPV6
, &nf_ip6_logger
);
406 pr_err("failed to register logger\n");
413 unregister_pernet_subsys(&nf_log_ipv6_net_ops
);
417 static void __exit
nf_log_ipv6_exit(void)
419 unregister_pernet_subsys(&nf_log_ipv6_net_ops
);
420 nf_log_unregister(&nf_ip6_logger
);
423 module_init(nf_log_ipv6_init
);
424 module_exit(nf_log_ipv6_exit
);
426 MODULE_AUTHOR("Netfilter Core Team <coreteam@netfilter.org>");
427 MODULE_DESCRIPTION("Netfilter IPv6 packet logging");
428 MODULE_LICENSE("GPL");
429 MODULE_ALIAS_NF_LOGGER(AF_INET6
, 0);