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 #include <linux/module.h>
10 #include <linux/spinlock.h>
11 #include <linux/skbuff.h>
12 #include <linux/if_arp.h>
17 #include <net/route.h>
19 #include <linux/netfilter.h>
20 #include <linux/netfilter_bridge.h>
21 #include <linux/netfilter/xt_LOG.h>
22 #include <net/netfilter/nf_log.h>
24 int nf_log_dump_udp_header(struct nf_log_buf *m, const struct sk_buff *skb,
25 u8 proto, int fragment, unsigned int offset)
28 const struct udphdr *uh;
30 if (proto == IPPROTO_UDP)
31 /* Max length: 10 "PROTO=UDP " */
32 nf_log_buf_add(m, "PROTO=UDP ");
33 else /* Max length: 14 "PROTO=UDPLITE " */
34 nf_log_buf_add(m, "PROTO=UDPLITE ");
39 /* Max length: 25 "INCOMPLETE [65535 bytes] " */
40 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
42 nf_log_buf_add(m, "INCOMPLETE [%u bytes] ", skb->len - offset);
47 /* Max length: 20 "SPT=65535 DPT=65535 " */
48 nf_log_buf_add(m, "SPT=%u DPT=%u LEN=%u ",
49 ntohs(uh->source), ntohs(uh->dest), ntohs(uh->len));
54 EXPORT_SYMBOL_GPL(nf_log_dump_udp_header);
56 int nf_log_dump_tcp_header(struct nf_log_buf *m, const struct sk_buff *skb,
57 u8 proto, int fragment, unsigned int offset,
58 unsigned int logflags)
61 const struct tcphdr *th;
63 /* Max length: 10 "PROTO=TCP " */
64 nf_log_buf_add(m, "PROTO=TCP ");
69 /* Max length: 25 "INCOMPLETE [65535 bytes] " */
70 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
72 nf_log_buf_add(m, "INCOMPLETE [%u bytes] ", skb->len - offset);
76 /* Max length: 20 "SPT=65535 DPT=65535 " */
77 nf_log_buf_add(m, "SPT=%u DPT=%u ",
78 ntohs(th->source), ntohs(th->dest));
79 /* Max length: 30 "SEQ=4294967295 ACK=4294967295 " */
80 if (logflags & NF_LOG_TCPSEQ) {
81 nf_log_buf_add(m, "SEQ=%u ACK=%u ",
82 ntohl(th->seq), ntohl(th->ack_seq));
85 /* Max length: 13 "WINDOW=65535 " */
86 nf_log_buf_add(m, "WINDOW=%u ", ntohs(th->window));
87 /* Max length: 9 "RES=0x3C " */
88 nf_log_buf_add(m, "RES=0x%02x ", (u_int8_t)(ntohl(tcp_flag_word(th) &
89 TCP_RESERVED_BITS) >> 22));
90 /* Max length: 32 "CWR ECE URG ACK PSH RST SYN FIN " */
92 nf_log_buf_add(m, "CWR ");
94 nf_log_buf_add(m, "ECE ");
96 nf_log_buf_add(m, "URG ");
98 nf_log_buf_add(m, "ACK ");
100 nf_log_buf_add(m, "PSH ");
102 nf_log_buf_add(m, "RST ");
104 nf_log_buf_add(m, "SYN ");
106 nf_log_buf_add(m, "FIN ");
107 /* Max length: 11 "URGP=65535 " */
108 nf_log_buf_add(m, "URGP=%u ", ntohs(th->urg_ptr));
110 if ((logflags & NF_LOG_TCPOPT) && th->doff*4 > sizeof(struct tcphdr)) {
111 u_int8_t _opt[60 - sizeof(struct tcphdr)];
114 unsigned int optsize = th->doff*4 - sizeof(struct tcphdr);
116 op = skb_header_pointer(skb, offset + sizeof(struct tcphdr),
119 nf_log_buf_add(m, "OPT (TRUNCATED)");
123 /* Max length: 127 "OPT (" 15*4*2chars ") " */
124 nf_log_buf_add(m, "OPT (");
125 for (i = 0; i < optsize; i++)
126 nf_log_buf_add(m, "%02X", op[i]);
128 nf_log_buf_add(m, ") ");
133 EXPORT_SYMBOL_GPL(nf_log_dump_tcp_header);
135 void nf_log_dump_sk_uid_gid(struct net *net, struct nf_log_buf *m,
138 if (!sk || !sk_fullsock(sk) || !net_eq(net, sock_net(sk)))
141 read_lock_bh(&sk->sk_callback_lock);
142 if (sk->sk_socket && sk->sk_socket->file) {
143 const struct cred *cred = sk->sk_socket->file->f_cred;
144 nf_log_buf_add(m, "UID=%u GID=%u ",
145 from_kuid_munged(&init_user_ns, cred->fsuid),
146 from_kgid_munged(&init_user_ns, cred->fsgid));
148 read_unlock_bh(&sk->sk_callback_lock);
150 EXPORT_SYMBOL_GPL(nf_log_dump_sk_uid_gid);
153 nf_log_dump_packet_common(struct nf_log_buf *m, u_int8_t pf,
154 unsigned int hooknum, const struct sk_buff *skb,
155 const struct net_device *in,
156 const struct net_device *out,
157 const struct nf_loginfo *loginfo, const char *prefix)
159 nf_log_buf_add(m, KERN_SOH "%c%sIN=%s OUT=%s ",
160 '0' + loginfo->u.log.level, prefix,
162 out ? out->name : "");
163 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
164 if (skb->nf_bridge) {
165 const struct net_device *physindev;
166 const struct net_device *physoutdev;
168 physindev = nf_bridge_get_physindev(skb);
169 if (physindev && in != physindev)
170 nf_log_buf_add(m, "PHYSIN=%s ", physindev->name);
171 physoutdev = nf_bridge_get_physoutdev(skb);
172 if (physoutdev && out != physoutdev)
173 nf_log_buf_add(m, "PHYSOUT=%s ", physoutdev->name);
177 EXPORT_SYMBOL_GPL(nf_log_dump_packet_common);
179 /* bridge and netdev logging families share this code. */
180 void nf_log_l2packet(struct net *net, u_int8_t pf,
182 unsigned int hooknum,
183 const struct sk_buff *skb,
184 const struct net_device *in,
185 const struct net_device *out,
186 const struct nf_loginfo *loginfo,
190 case htons(ETH_P_IP):
191 nf_log_packet(net, NFPROTO_IPV4, hooknum, skb, in, out,
192 loginfo, "%s", prefix);
194 case htons(ETH_P_IPV6):
195 nf_log_packet(net, NFPROTO_IPV6, hooknum, skb, in, out,
196 loginfo, "%s", prefix);
198 case htons(ETH_P_ARP):
199 case htons(ETH_P_RARP):
200 nf_log_packet(net, NFPROTO_ARP, hooknum, skb, in, out,
201 loginfo, "%s", prefix);
205 EXPORT_SYMBOL_GPL(nf_log_l2packet);
207 static int __init nf_log_common_init(void)
212 static void __exit nf_log_common_exit(void) {}
214 module_init(nf_log_common_init);
215 module_exit(nf_log_common_exit);
217 MODULE_LICENSE("GPL");