1 /* (C) 1999-2001 Paul `Rusty' Russell
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 nf_log_buf *m, struct sock *sk)
137 if (!sk || !sk_fullsock(sk))
140 read_lock_bh(&sk->sk_callback_lock);
141 if (sk->sk_socket && sk->sk_socket->file) {
142 const struct cred *cred = sk->sk_socket->file->f_cred;
143 nf_log_buf_add(m, "UID=%u GID=%u ",
144 from_kuid_munged(&init_user_ns, cred->fsuid),
145 from_kgid_munged(&init_user_ns, cred->fsgid));
147 read_unlock_bh(&sk->sk_callback_lock);
149 EXPORT_SYMBOL_GPL(nf_log_dump_sk_uid_gid);
152 nf_log_dump_packet_common(struct nf_log_buf *m, u_int8_t pf,
153 unsigned int hooknum, const struct sk_buff *skb,
154 const struct net_device *in,
155 const struct net_device *out,
156 const struct nf_loginfo *loginfo, const char *prefix)
158 nf_log_buf_add(m, KERN_SOH "%c%sIN=%s OUT=%s ",
159 '0' + loginfo->u.log.level, prefix,
161 out ? out->name : "");
162 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
163 if (skb->nf_bridge) {
164 const struct net_device *physindev;
165 const struct net_device *physoutdev;
167 physindev = nf_bridge_get_physindev(skb);
168 if (physindev && in != physindev)
169 nf_log_buf_add(m, "PHYSIN=%s ", physindev->name);
170 physoutdev = nf_bridge_get_physoutdev(skb);
171 if (physoutdev && out != physoutdev)
172 nf_log_buf_add(m, "PHYSOUT=%s ", physoutdev->name);
176 EXPORT_SYMBOL_GPL(nf_log_dump_packet_common);
178 /* bridge and netdev logging families share this code. */
179 void nf_log_l2packet(struct net *net, u_int8_t pf,
181 unsigned int hooknum,
182 const struct sk_buff *skb,
183 const struct net_device *in,
184 const struct net_device *out,
185 const struct nf_loginfo *loginfo,
189 case htons(ETH_P_IP):
190 nf_log_packet(net, NFPROTO_IPV4, hooknum, skb, in, out,
191 loginfo, "%s", prefix);
193 case htons(ETH_P_IPV6):
194 nf_log_packet(net, NFPROTO_IPV6, hooknum, skb, in, out,
195 loginfo, "%s", prefix);
197 case htons(ETH_P_ARP):
198 case htons(ETH_P_RARP):
199 nf_log_packet(net, NFPROTO_ARP, hooknum, skb, in, out,
200 loginfo, "%s", prefix);
204 EXPORT_SYMBOL_GPL(nf_log_l2packet);
206 static int __init nf_log_common_init(void)
211 static void __exit nf_log_common_exit(void) {}
213 module_init(nf_log_common_init);
214 module_exit(nf_log_common_exit);
216 MODULE_LICENSE("GPL");