blob: e6af8d72f26be3cfb509b45bffb9198a151ae395 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * This is a module which is used for logging packets.
3 */
4
5/* (C) 2001 Jan Rekorajski <baggins@pld.org.pl>
6 * (C) 2002-2004 Netfilter Core Team <coreteam@netfilter.org>
7 *
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.
11 */
Jan Engelhardtff67e4e2010-03-19 21:08:16 +010012#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/skbuff.h>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020015#include <linux/if_arp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/ip.h>
17#include <linux/spinlock.h>
18#include <linux/icmpv6.h>
19#include <net/udp.h>
20#include <net/tcp.h>
21#include <net/ipv6.h>
22#include <linux/netfilter.h>
Jan Engelhardt6709dbb2007-02-07 15:11:19 -080023#include <linux/netfilter/x_tables.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/netfilter_ipv6/ip6_tables.h>
Patrick McHardyf01ffbd2007-12-17 22:38:49 -080025#include <net/netfilter/nf_log.h>
Eric Dumazeta8defca2010-10-04 20:56:05 +020026#include <net/netfilter/xt_log.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
28MODULE_AUTHOR("Jan Rekorajski <baggins@pld.org.pl>");
Jan Engelhardt2ae15b62008-01-14 23:42:28 -080029MODULE_DESCRIPTION("Xtables: IPv6 packet logging to syslog");
Linus Torvalds1da177e2005-04-16 15:20:36 -070030MODULE_LICENSE("GPL");
31
Linus Torvalds1da177e2005-04-16 15:20:36 -070032struct in_device;
33#include <net/route.h>
34#include <linux/netfilter_ipv6/ip6t_LOG.h>
35
Linus Torvalds1da177e2005-04-16 15:20:36 -070036/* One level of recursion won't kill us */
Eric Dumazeta8defca2010-10-04 20:56:05 +020037static void dump_packet(struct sbuff *m,
38 const struct nf_loginfo *info,
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 const struct sk_buff *skb, unsigned int ip6hoff,
40 int recurse)
41{
42 u_int8_t currenthdr;
43 int fragment;
Jan Engelhardta47362a2007-07-07 22:16:55 -070044 struct ipv6hdr _ip6h;
45 const struct ipv6hdr *ih;
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 unsigned int ptr;
47 unsigned int hdrlen = 0;
Harald Welte608c8e42005-08-09 19:58:27 -070048 unsigned int logflags;
49
50 if (info->type == NF_LOG_TYPE_LOG)
51 logflags = info->u.log.logflags;
52 else
53 logflags = NF_LOG_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
55 ih = skb_header_pointer(skb, ip6hoff, sizeof(_ip6h), &_ip6h);
56 if (ih == NULL) {
Eric Dumazeta8defca2010-10-04 20:56:05 +020057 sb_add(m, "TRUNCATED");
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 return;
59 }
60
Joe Perches46b86a22006-01-13 14:29:07 -080061 /* Max length: 88 "SRC=0000.0000.0000.0000.0000.0000.0000.0000 DST=0000.0000.0000.0000.0000.0000.0000.0000 " */
Eric Dumazeta8defca2010-10-04 20:56:05 +020062 sb_add(m, "SRC=%pI6 DST=%pI6 ", &ih->saddr, &ih->daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
64 /* Max length: 44 "LEN=65535 TC=255 HOPLIMIT=255 FLOWLBL=FFFFF " */
Eric Dumazeta8defca2010-10-04 20:56:05 +020065 sb_add(m, "LEN=%Zu TC=%u HOPLIMIT=%u FLOWLBL=%u ",
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 ntohs(ih->payload_len) + sizeof(struct ipv6hdr),
Al Viroe69a4adc2006-11-14 20:56:00 -080067 (ntohl(*(__be32 *)ih) & 0x0ff00000) >> 20,
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 ih->hop_limit,
Al Viroe69a4adc2006-11-14 20:56:00 -080069 (ntohl(*(__be32 *)ih) & 0x000fffff));
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
71 fragment = 0;
72 ptr = ip6hoff + sizeof(struct ipv6hdr);
73 currenthdr = ih->nexthdr;
74 while (currenthdr != NEXTHDR_NONE && ip6t_ext_hdr(currenthdr)) {
Jan Engelhardta47362a2007-07-07 22:16:55 -070075 struct ipv6_opt_hdr _hdr;
76 const struct ipv6_opt_hdr *hp;
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
78 hp = skb_header_pointer(skb, ptr, sizeof(_hdr), &_hdr);
79 if (hp == NULL) {
Eric Dumazeta8defca2010-10-04 20:56:05 +020080 sb_add(m, "TRUNCATED");
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 return;
82 }
83
84 /* Max length: 48 "OPT (...) " */
Harald Welte608c8e42005-08-09 19:58:27 -070085 if (logflags & IP6T_LOG_IPOPT)
Eric Dumazeta8defca2010-10-04 20:56:05 +020086 sb_add(m, "OPT ( ");
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
88 switch (currenthdr) {
89 case IPPROTO_FRAGMENT: {
Jan Engelhardta47362a2007-07-07 22:16:55 -070090 struct frag_hdr _fhdr;
91 const struct frag_hdr *fh;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
Eric Dumazeta8defca2010-10-04 20:56:05 +020093 sb_add(m, "FRAG:");
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 fh = skb_header_pointer(skb, ptr, sizeof(_fhdr),
95 &_fhdr);
96 if (fh == NULL) {
Eric Dumazeta8defca2010-10-04 20:56:05 +020097 sb_add(m, "TRUNCATED ");
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 return;
99 }
100
101 /* Max length: 6 "65535 " */
Eric Dumazeta8defca2010-10-04 20:56:05 +0200102 sb_add(m, "%u ", ntohs(fh->frag_off) & 0xFFF8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
104 /* Max length: 11 "INCOMPLETE " */
105 if (fh->frag_off & htons(0x0001))
Eric Dumazeta8defca2010-10-04 20:56:05 +0200106 sb_add(m, "INCOMPLETE ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
Eric Dumazeta8defca2010-10-04 20:56:05 +0200108 sb_add(m, "ID:%08x ", ntohl(fh->identification));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
110 if (ntohs(fh->frag_off) & 0xFFF8)
111 fragment = 1;
112
113 hdrlen = 8;
114
115 break;
116 }
117 case IPPROTO_DSTOPTS:
118 case IPPROTO_ROUTING:
119 case IPPROTO_HOPOPTS:
120 if (fragment) {
Harald Welte608c8e42005-08-09 19:58:27 -0700121 if (logflags & IP6T_LOG_IPOPT)
Eric Dumazeta8defca2010-10-04 20:56:05 +0200122 sb_add(m, ")");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 return;
124 }
125 hdrlen = ipv6_optlen(hp);
126 break;
127 /* Max Length */
128 case IPPROTO_AH:
Harald Welte608c8e42005-08-09 19:58:27 -0700129 if (logflags & IP6T_LOG_IPOPT) {
Jan Engelhardta47362a2007-07-07 22:16:55 -0700130 struct ip_auth_hdr _ahdr;
131 const struct ip_auth_hdr *ah;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
133 /* Max length: 3 "AH " */
Eric Dumazeta8defca2010-10-04 20:56:05 +0200134 sb_add(m, "AH ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
136 if (fragment) {
Eric Dumazeta8defca2010-10-04 20:56:05 +0200137 sb_add(m, ")");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 return;
139 }
140
141 ah = skb_header_pointer(skb, ptr, sizeof(_ahdr),
142 &_ahdr);
143 if (ah == NULL) {
144 /*
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900145 * Max length: 26 "INCOMPLETE [65535
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 * bytes] )"
147 */
Eric Dumazeta8defca2010-10-04 20:56:05 +0200148 sb_add(m, "INCOMPLETE [%u bytes] )",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 skb->len - ptr);
150 return;
151 }
152
153 /* Length: 15 "SPI=0xF1234567 */
Eric Dumazeta8defca2010-10-04 20:56:05 +0200154 sb_add(m, "SPI=0x%x ", ntohl(ah->spi));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
156 }
157
158 hdrlen = (hp->hdrlen+2)<<2;
159 break;
160 case IPPROTO_ESP:
Harald Welte608c8e42005-08-09 19:58:27 -0700161 if (logflags & IP6T_LOG_IPOPT) {
Jan Engelhardta47362a2007-07-07 22:16:55 -0700162 struct ip_esp_hdr _esph;
163 const struct ip_esp_hdr *eh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
165 /* Max length: 4 "ESP " */
Eric Dumazeta8defca2010-10-04 20:56:05 +0200166 sb_add(m, "ESP ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
168 if (fragment) {
Eric Dumazeta8defca2010-10-04 20:56:05 +0200169 sb_add(m, ")");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 return;
171 }
172
173 /*
174 * Max length: 26 "INCOMPLETE [65535 bytes] )"
175 */
176 eh = skb_header_pointer(skb, ptr, sizeof(_esph),
177 &_esph);
178 if (eh == NULL) {
Eric Dumazeta8defca2010-10-04 20:56:05 +0200179 sb_add(m, "INCOMPLETE [%u bytes] )",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 skb->len - ptr);
181 return;
182 }
183
184 /* Length: 16 "SPI=0xF1234567 )" */
Eric Dumazeta8defca2010-10-04 20:56:05 +0200185 sb_add(m, "SPI=0x%x )", ntohl(eh->spi) );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
187 }
188 return;
189 default:
190 /* Max length: 20 "Unknown Ext Hdr 255" */
Eric Dumazeta8defca2010-10-04 20:56:05 +0200191 sb_add(m, "Unknown Ext Hdr %u", currenthdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 return;
193 }
Harald Welte608c8e42005-08-09 19:58:27 -0700194 if (logflags & IP6T_LOG_IPOPT)
Eric Dumazeta8defca2010-10-04 20:56:05 +0200195 sb_add(m, ") ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
197 currenthdr = hp->nexthdr;
198 ptr += hdrlen;
199 }
200
201 switch (currenthdr) {
202 case IPPROTO_TCP: {
Jan Engelhardta47362a2007-07-07 22:16:55 -0700203 struct tcphdr _tcph;
204 const struct tcphdr *th;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
206 /* Max length: 10 "PROTO=TCP " */
Eric Dumazeta8defca2010-10-04 20:56:05 +0200207 sb_add(m, "PROTO=TCP ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
209 if (fragment)
210 break;
211
212 /* Max length: 25 "INCOMPLETE [65535 bytes] " */
213 th = skb_header_pointer(skb, ptr, sizeof(_tcph), &_tcph);
214 if (th == NULL) {
Eric Dumazeta8defca2010-10-04 20:56:05 +0200215 sb_add(m, "INCOMPLETE [%u bytes] ", skb->len - ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 return;
217 }
218
219 /* Max length: 20 "SPT=65535 DPT=65535 " */
Eric Dumazeta8defca2010-10-04 20:56:05 +0200220 sb_add(m, "SPT=%u DPT=%u ",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 ntohs(th->source), ntohs(th->dest));
222 /* Max length: 30 "SEQ=4294967295 ACK=4294967295 " */
Harald Welte608c8e42005-08-09 19:58:27 -0700223 if (logflags & IP6T_LOG_TCPSEQ)
Eric Dumazeta8defca2010-10-04 20:56:05 +0200224 sb_add(m, "SEQ=%u ACK=%u ",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 ntohl(th->seq), ntohl(th->ack_seq));
226 /* Max length: 13 "WINDOW=65535 " */
Eric Dumazeta8defca2010-10-04 20:56:05 +0200227 sb_add(m, "WINDOW=%u ", ntohs(th->window));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 /* Max length: 9 "RES=0x3C " */
Eric Dumazeta8defca2010-10-04 20:56:05 +0200229 sb_add(m, "RES=0x%02x ", (u_int8_t)(ntohl(tcp_flag_word(th) & TCP_RESERVED_BITS) >> 22));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 /* Max length: 32 "CWR ECE URG ACK PSH RST SYN FIN " */
231 if (th->cwr)
Eric Dumazeta8defca2010-10-04 20:56:05 +0200232 sb_add(m, "CWR ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 if (th->ece)
Eric Dumazeta8defca2010-10-04 20:56:05 +0200234 sb_add(m, "ECE ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 if (th->urg)
Eric Dumazeta8defca2010-10-04 20:56:05 +0200236 sb_add(m, "URG ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 if (th->ack)
Eric Dumazeta8defca2010-10-04 20:56:05 +0200238 sb_add(m, "ACK ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 if (th->psh)
Eric Dumazeta8defca2010-10-04 20:56:05 +0200240 sb_add(m, "PSH ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 if (th->rst)
Eric Dumazeta8defca2010-10-04 20:56:05 +0200242 sb_add(m, "RST ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 if (th->syn)
Eric Dumazeta8defca2010-10-04 20:56:05 +0200244 sb_add(m, "SYN ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 if (th->fin)
Eric Dumazeta8defca2010-10-04 20:56:05 +0200246 sb_add(m, "FIN ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 /* Max length: 11 "URGP=65535 " */
Eric Dumazeta8defca2010-10-04 20:56:05 +0200248 sb_add(m, "URGP=%u ", ntohs(th->urg_ptr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
Joe Perches3666ed12009-11-23 23:17:06 +0100250 if ((logflags & IP6T_LOG_TCPOPT) &&
251 th->doff * 4 > sizeof(struct tcphdr)) {
Jan Engelhardta47362a2007-07-07 22:16:55 -0700252 u_int8_t _opt[60 - sizeof(struct tcphdr)];
253 const u_int8_t *op;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 unsigned int i;
255 unsigned int optsize = th->doff * 4
256 - sizeof(struct tcphdr);
257
258 op = skb_header_pointer(skb,
259 ptr + sizeof(struct tcphdr),
260 optsize, _opt);
261 if (op == NULL) {
Eric Dumazeta8defca2010-10-04 20:56:05 +0200262 sb_add(m, "OPT (TRUNCATED)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 return;
264 }
265
266 /* Max length: 127 "OPT (" 15*4*2chars ") " */
Eric Dumazeta8defca2010-10-04 20:56:05 +0200267 sb_add(m, "OPT (");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 for (i =0; i < optsize; i++)
Eric Dumazeta8defca2010-10-04 20:56:05 +0200269 sb_add(m, "%02X", op[i]);
270 sb_add(m, ") ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 }
272 break;
273 }
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800274 case IPPROTO_UDP:
275 case IPPROTO_UDPLITE: {
Jan Engelhardta47362a2007-07-07 22:16:55 -0700276 struct udphdr _udph;
277 const struct udphdr *uh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800279 if (currenthdr == IPPROTO_UDP)
280 /* Max length: 10 "PROTO=UDP " */
Eric Dumazeta8defca2010-10-04 20:56:05 +0200281 sb_add(m, "PROTO=UDP " );
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800282 else /* Max length: 14 "PROTO=UDPLITE " */
Eric Dumazeta8defca2010-10-04 20:56:05 +0200283 sb_add(m, "PROTO=UDPLITE ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
285 if (fragment)
286 break;
287
288 /* Max length: 25 "INCOMPLETE [65535 bytes] " */
289 uh = skb_header_pointer(skb, ptr, sizeof(_udph), &_udph);
290 if (uh == NULL) {
Eric Dumazeta8defca2010-10-04 20:56:05 +0200291 sb_add(m, "INCOMPLETE [%u bytes] ", skb->len - ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 return;
293 }
294
295 /* Max length: 20 "SPT=65535 DPT=65535 " */
Eric Dumazeta8defca2010-10-04 20:56:05 +0200296 sb_add(m, "SPT=%u DPT=%u LEN=%u ",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 ntohs(uh->source), ntohs(uh->dest),
298 ntohs(uh->len));
299 break;
300 }
301 case IPPROTO_ICMPV6: {
Jan Engelhardta47362a2007-07-07 22:16:55 -0700302 struct icmp6hdr _icmp6h;
303 const struct icmp6hdr *ic;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
305 /* Max length: 13 "PROTO=ICMPv6 " */
Eric Dumazeta8defca2010-10-04 20:56:05 +0200306 sb_add(m, "PROTO=ICMPv6 ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
308 if (fragment)
309 break;
310
311 /* Max length: 25 "INCOMPLETE [65535 bytes] " */
312 ic = skb_header_pointer(skb, ptr, sizeof(_icmp6h), &_icmp6h);
313 if (ic == NULL) {
Eric Dumazeta8defca2010-10-04 20:56:05 +0200314 sb_add(m, "INCOMPLETE [%u bytes] ", skb->len - ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 return;
316 }
317
318 /* Max length: 18 "TYPE=255 CODE=255 " */
Eric Dumazeta8defca2010-10-04 20:56:05 +0200319 sb_add(m, "TYPE=%u CODE=%u ", ic->icmp6_type, ic->icmp6_code);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320
321 switch (ic->icmp6_type) {
322 case ICMPV6_ECHO_REQUEST:
323 case ICMPV6_ECHO_REPLY:
324 /* Max length: 19 "ID=65535 SEQ=65535 " */
Eric Dumazeta8defca2010-10-04 20:56:05 +0200325 sb_add(m, "ID=%u SEQ=%u ",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 ntohs(ic->icmp6_identifier),
327 ntohs(ic->icmp6_sequence));
328 break;
329 case ICMPV6_MGM_QUERY:
330 case ICMPV6_MGM_REPORT:
331 case ICMPV6_MGM_REDUCTION:
332 break;
333
334 case ICMPV6_PARAMPROB:
335 /* Max length: 17 "POINTER=ffffffff " */
Eric Dumazeta8defca2010-10-04 20:56:05 +0200336 sb_add(m, "POINTER=%08x ", ntohl(ic->icmp6_pointer));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 /* Fall through */
338 case ICMPV6_DEST_UNREACH:
339 case ICMPV6_PKT_TOOBIG:
340 case ICMPV6_TIME_EXCEED:
341 /* Max length: 3+maxlen */
342 if (recurse) {
Eric Dumazeta8defca2010-10-04 20:56:05 +0200343 sb_add(m, "[");
344 dump_packet(m, info, skb,
345 ptr + sizeof(_icmp6h), 0);
346 sb_add(m, "] ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 }
348
349 /* Max length: 10 "MTU=65535 " */
350 if (ic->icmp6_type == ICMPV6_PKT_TOOBIG)
Eric Dumazeta8defca2010-10-04 20:56:05 +0200351 sb_add(m, "MTU=%u ", ntohl(ic->icmp6_mtu));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 }
353 break;
354 }
355 /* Max length: 10 "PROTO=255 " */
356 default:
Eric Dumazeta8defca2010-10-04 20:56:05 +0200357 sb_add(m, "PROTO=%u ", currenthdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 }
359
360 /* Max length: 15 "UID=4294967295 " */
Harald Welte608c8e42005-08-09 19:58:27 -0700361 if ((logflags & IP6T_LOG_UID) && recurse && skb->sk) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 read_lock_bh(&skb->sk->sk_callback_lock);
363 if (skb->sk->sk_socket && skb->sk->sk_socket->file)
Eric Dumazeta8defca2010-10-04 20:56:05 +0200364 sb_add(m, "UID=%u GID=%u ",
David Howellsd76b0d92008-11-14 10:39:25 +1100365 skb->sk->sk_socket->file->f_cred->fsuid,
366 skb->sk->sk_socket->file->f_cred->fsgid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 read_unlock_bh(&skb->sk->sk_callback_lock);
368 }
Patrick McHardy36e2a1b2008-03-20 15:15:45 +0100369
370 /* Max length: 16 "MARK=0xFFFFFFFF " */
371 if (!recurse && skb->mark)
Eric Dumazeta8defca2010-10-04 20:56:05 +0200372 sb_add(m, "MARK=0x%x ", skb->mark);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373}
374
Eric Dumazeta8defca2010-10-04 20:56:05 +0200375static void dump_mac_header(struct sbuff *m,
376 const struct nf_loginfo *info,
Patrick McHardy7eb92822010-06-28 14:16:08 +0200377 const struct sk_buff *skb)
378{
379 struct net_device *dev = skb->dev;
380 unsigned int logflags = 0;
381
382 if (info->type == NF_LOG_TYPE_LOG)
383 logflags = info->u.log.logflags;
384
385 if (!(logflags & IP6T_LOG_MACDECODE))
386 goto fallback;
387
388 switch (dev->type) {
389 case ARPHRD_ETHER:
Eric Dumazeta8defca2010-10-04 20:56:05 +0200390 sb_add(m, "MACSRC=%pM MACDST=%pM MACPROTO=%04x ",
Patrick McHardy7eb92822010-06-28 14:16:08 +0200391 eth_hdr(skb)->h_source, eth_hdr(skb)->h_dest,
392 ntohs(eth_hdr(skb)->h_proto));
393 return;
394 default:
395 break;
396 }
397
398fallback:
Eric Dumazeta8defca2010-10-04 20:56:05 +0200399 sb_add(m, "MAC=");
Patrick McHardy7eb92822010-06-28 14:16:08 +0200400 if (dev->hard_header_len &&
401 skb->mac_header != skb->network_header) {
402 const unsigned char *p = skb_mac_header(skb);
403 unsigned int len = dev->hard_header_len;
404 unsigned int i;
405
406 if (dev->type == ARPHRD_SIT &&
407 (p -= ETH_HLEN) < skb->head)
408 p = NULL;
409
410 if (p != NULL) {
Eric Dumazeta8defca2010-10-04 20:56:05 +0200411 sb_add(m, "%02x", *p++);
Patrick McHardy7eb92822010-06-28 14:16:08 +0200412 for (i = 1; i < len; i++)
Joerg Marx0af320f2011-02-17 16:23:40 +0100413 sb_add(m, ":%02x", *p++);
Patrick McHardy7eb92822010-06-28 14:16:08 +0200414 }
Eric Dumazeta8defca2010-10-04 20:56:05 +0200415 sb_add(m, " ");
Patrick McHardy7eb92822010-06-28 14:16:08 +0200416
417 if (dev->type == ARPHRD_SIT) {
418 const struct iphdr *iph =
419 (struct iphdr *)skb_mac_header(skb);
Eric Dumazeta8defca2010-10-04 20:56:05 +0200420 sb_add(m, "TUNNEL=%pI4->%pI4 ", &iph->saddr, &iph->daddr);
Patrick McHardy7eb92822010-06-28 14:16:08 +0200421 }
422 } else
Eric Dumazeta8defca2010-10-04 20:56:05 +0200423 sb_add(m, " ");
Patrick McHardy7eb92822010-06-28 14:16:08 +0200424}
425
Harald Welte608c8e42005-08-09 19:58:27 -0700426static struct nf_loginfo default_loginfo = {
427 .type = NF_LOG_TYPE_LOG,
428 .u = {
429 .log = {
Patrick McHardyf0d57a52010-04-15 19:09:01 +0200430 .level = 5,
Harald Welte608c8e42005-08-09 19:58:27 -0700431 .logflags = NF_LOG_MASK,
432 },
433 },
434};
435
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436static void
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200437ip6t_log_packet(u_int8_t pf,
Harald Welte608c8e42005-08-09 19:58:27 -0700438 unsigned int hooknum,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 const struct sk_buff *skb,
440 const struct net_device *in,
441 const struct net_device *out,
Harald Welte608c8e42005-08-09 19:58:27 -0700442 const struct nf_loginfo *loginfo,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 const char *prefix)
444{
Eric Dumazeta8defca2010-10-04 20:56:05 +0200445 struct sbuff *m = sb_open();
446
Harald Welte608c8e42005-08-09 19:58:27 -0700447 if (!loginfo)
448 loginfo = &default_loginfo;
449
Eric Dumazeta8defca2010-10-04 20:56:05 +0200450 sb_add(m, "<%d>%sIN=%s OUT=%s ", loginfo->u.log.level,
451 prefix,
452 in ? in->name : "",
453 out ? out->name : "");
Patrick McHardy047601b2005-06-21 14:07:13 -0700454
Jan Engelhardtb4686452010-11-15 11:23:06 +0100455 if (in != NULL)
Eric Dumazeta8defca2010-10-04 20:56:05 +0200456 dump_mac_header(m, loginfo, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457
Eric Dumazeta8defca2010-10-04 20:56:05 +0200458 dump_packet(m, loginfo, skb, skb_network_offset(skb), 1);
459
460 sb_close(m);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461}
462
463static unsigned int
Jan Engelhardt4b560b42009-07-05 19:43:26 +0200464log_tg6(struct sk_buff *skb, const struct xt_action_param *par)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465{
Jan Engelhardt7eb35582008-10-08 11:35:19 +0200466 const struct ip6t_log_info *loginfo = par->targinfo;
Harald Welte608c8e42005-08-09 19:58:27 -0700467 struct nf_loginfo li;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468
Harald Welte608c8e42005-08-09 19:58:27 -0700469 li.type = NF_LOG_TYPE_LOG;
470 li.u.log.level = loginfo->level;
471 li.u.log.logflags = loginfo->logflags;
472
Jan Engelhardt7eb35582008-10-08 11:35:19 +0200473 ip6t_log_packet(NFPROTO_IPV6, par->hooknum, skb, par->in, par->out,
Jan Engelhardtee999d82008-10-08 11:35:01 +0200474 &li, loginfo->prefix);
Jan Engelhardt6709dbb2007-02-07 15:11:19 -0800475 return XT_CONTINUE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476}
477
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478
Jan Engelhardt135367b2010-03-19 17:16:42 +0100479static int log_tg6_check(const struct xt_tgchk_param *par)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480{
Jan Engelhardtaf5d6dc2008-10-08 11:35:19 +0200481 const struct ip6t_log_info *loginfo = par->targinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 if (loginfo->level >= 8) {
Jan Engelhardtff67e4e2010-03-19 21:08:16 +0100484 pr_debug("level %u >= 8\n", loginfo->level);
Jan Engelhardtd6b00a52010-03-25 16:34:45 +0100485 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 if (loginfo->prefix[sizeof(loginfo->prefix)-1] != '\0') {
Jan Engelhardtff67e4e2010-03-19 21:08:16 +0100488 pr_debug("prefix not null-terminated\n");
Jan Engelhardtd6b00a52010-03-25 16:34:45 +0100489 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 }
Jan Engelhardtd6b00a52010-03-25 16:34:45 +0100491 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492}
493
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800494static struct xt_target log_tg6_reg __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 .name = "LOG",
Jan Engelhardtee999d82008-10-08 11:35:01 +0200496 .family = NFPROTO_IPV6,
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800497 .target = log_tg6,
Patrick McHardy7f939712006-03-20 18:01:43 -0800498 .targetsize = sizeof(struct ip6t_log_info),
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800499 .checkentry = log_tg6_check,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 .me = THIS_MODULE,
501};
502
Eric Leblondca735b32009-03-16 14:54:21 +0100503static struct nf_logger ip6t_logger __read_mostly = {
Harald Welte608c8e42005-08-09 19:58:27 -0700504 .name = "ip6t_LOG",
505 .logfn = &ip6t_log_packet,
506 .me = THIS_MODULE,
507};
508
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800509static int __init log_tg6_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510{
Jan Engelhardte1fd0582007-02-07 15:10:34 -0800511 int ret;
512
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800513 ret = xt_register_target(&log_tg6_reg);
Jan Engelhardte1fd0582007-02-07 15:10:34 -0800514 if (ret < 0)
515 return ret;
Jan Engelhardtee999d82008-10-08 11:35:01 +0200516 nf_log_register(NFPROTO_IPV6, &ip6t_logger);
Patrick McHardy7e2acc72007-07-24 15:29:55 -0700517 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518}
519
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800520static void __exit log_tg6_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521{
Patrick McHardye92ad992007-02-12 11:11:55 -0800522 nf_log_unregister(&ip6t_logger);
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800523 xt_unregister_target(&log_tg6_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524}
525
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800526module_init(log_tg6_init);
527module_exit(log_tg6_exit);