1 // SPDX-License-Identifier: GPL-2.0-only
2 /* (C) 1999-2001 Paul `Rusty' Russell
3 * (C) 2002-2004 Netfilter Core Team <coreteam@netfilter.org>
6 #include <linux/module.h>
7 #include <linux/spinlock.h>
8 #include <linux/skbuff.h>
9 #include <linux/if_arp.h>
14 #include <net/route.h>
16 #include <linux/netfilter.h>
17 #include <linux/netfilter_bridge.h>
18 #include <linux/netfilter/xt_LOG.h>
19 #include <net/netfilter/nf_log.h>
21 int nf_log_dump_udp_header(struct nf_log_buf *m, const struct sk_buff *skb,
22 u8 proto, int fragment, unsigned int offset)
25 const struct udphdr *uh;
27 if (proto == IPPROTO_UDP)
28 /* Max length: 10 "PROTO=UDP " */
29 nf_log_buf_add(m, "PROTO=UDP ");
30 else /* Max length: 14 "PROTO=UDPLITE " */
31 nf_log_buf_add(m, "PROTO=UDPLITE ");
36 /* Max length: 25 "INCOMPLETE [65535 bytes] " */
37 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
39 nf_log_buf_add(m, "INCOMPLETE [%u bytes] ", skb->len - offset);
44 /* Max length: 20 "SPT=65535 DPT=65535 " */
45 nf_log_buf_add(m, "SPT=%u DPT=%u LEN=%u ",
46 ntohs(uh->source), ntohs(uh->dest), ntohs(uh->len));
51 EXPORT_SYMBOL_GPL(nf_log_dump_udp_header);
53 int nf_log_dump_tcp_header(struct nf_log_buf *m, const struct sk_buff *skb,
54 u8 proto, int fragment, unsigned int offset,
55 unsigned int logflags)
58 const struct tcphdr *th;
60 /* Max length: 10 "PROTO=TCP " */
61 nf_log_buf_add(m, "PROTO=TCP ");
66 /* Max length: 25 "INCOMPLETE [65535 bytes] " */
67 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
69 nf_log_buf_add(m, "INCOMPLETE [%u bytes] ", skb->len - offset);
73 /* Max length: 20 "SPT=65535 DPT=65535 " */
74 nf_log_buf_add(m, "SPT=%u DPT=%u ",
75 ntohs(th->source), ntohs(th->dest));
76 /* Max length: 30 "SEQ=4294967295 ACK=4294967295 " */
77 if (logflags & NF_LOG_TCPSEQ) {
78 nf_log_buf_add(m, "SEQ=%u ACK=%u ",
79 ntohl(th->seq), ntohl(th->ack_seq));
82 /* Max length: 13 "WINDOW=65535 " */
83 nf_log_buf_add(m, "WINDOW=%u ", ntohs(th->window));
84 /* Max length: 9 "RES=0x3C " */
85 nf_log_buf_add(m, "RES=0x%02x ", (u_int8_t)(ntohl(tcp_flag_word(th) &
86 TCP_RESERVED_BITS) >> 22));
87 /* Max length: 32 "CWR ECE URG ACK PSH RST SYN FIN " */
89 nf_log_buf_add(m, "CWR ");
91 nf_log_buf_add(m, "ECE ");
93 nf_log_buf_add(m, "URG ");
95 nf_log_buf_add(m, "ACK ");
97 nf_log_buf_add(m, "PSH ");
99 nf_log_buf_add(m, "RST ");
101 nf_log_buf_add(m, "SYN ");
103 nf_log_buf_add(m, "FIN ");
104 /* Max length: 11 "URGP=65535 " */
105 nf_log_buf_add(m, "URGP=%u ", ntohs(th->urg_ptr));
107 if ((logflags & NF_LOG_TCPOPT) && th->doff*4 > sizeof(struct tcphdr)) {
108 u_int8_t _opt[60 - sizeof(struct tcphdr)];
111 unsigned int optsize = th->doff*4 - sizeof(struct tcphdr);
113 op = skb_header_pointer(skb, offset + sizeof(struct tcphdr),
116 nf_log_buf_add(m, "OPT (TRUNCATED)");
120 /* Max length: 127 "OPT (" 15*4*2chars ") " */
121 nf_log_buf_add(m, "OPT (");
122 for (i = 0; i < optsize; i++)
123 nf_log_buf_add(m, "%02X", op[i]);
125 nf_log_buf_add(m, ") ");
130 EXPORT_SYMBOL_GPL(nf_log_dump_tcp_header);
132 void nf_log_dump_sk_uid_gid(struct net *net, struct nf_log_buf *m,
135 if (!sk || !sk_fullsock(sk) || !net_eq(net, sock_net(sk)))
138 read_lock_bh(&sk->sk_callback_lock);
139 if (sk->sk_socket && sk->sk_socket->file) {
140 const struct cred *cred = sk->sk_socket->file->f_cred;
141 nf_log_buf_add(m, "UID=%u GID=%u ",
142 from_kuid_munged(&init_user_ns, cred->fsuid),
143 from_kgid_munged(&init_user_ns, cred->fsgid));
145 read_unlock_bh(&sk->sk_callback_lock);
147 EXPORT_SYMBOL_GPL(nf_log_dump_sk_uid_gid);
150 nf_log_dump_packet_common(struct nf_log_buf *m, u_int8_t pf,
151 unsigned int hooknum, const struct sk_buff *skb,
152 const struct net_device *in,
153 const struct net_device *out,
154 const struct nf_loginfo *loginfo, const char *prefix)
156 const struct net_device *physoutdev __maybe_unused;
157 const struct net_device *physindev __maybe_unused;
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 physindev = nf_bridge_get_physindev(skb);
165 if (physindev && in != physindev)
166 nf_log_buf_add(m, "PHYSIN=%s ", physindev->name);
167 physoutdev = nf_bridge_get_physoutdev(skb);
168 if (physoutdev && out != physoutdev)
169 nf_log_buf_add(m, "PHYSOUT=%s ", physoutdev->name);
172 EXPORT_SYMBOL_GPL(nf_log_dump_packet_common);
174 /* bridge and netdev logging families share this code. */
175 void nf_log_l2packet(struct net *net, u_int8_t pf,
177 unsigned int hooknum,
178 const struct sk_buff *skb,
179 const struct net_device *in,
180 const struct net_device *out,
181 const struct nf_loginfo *loginfo,
185 case htons(ETH_P_IP):
186 nf_log_packet(net, NFPROTO_IPV4, hooknum, skb, in, out,
187 loginfo, "%s", prefix);
189 case htons(ETH_P_IPV6):
190 nf_log_packet(net, NFPROTO_IPV6, hooknum, skb, in, out,
191 loginfo, "%s", prefix);
193 case htons(ETH_P_ARP):
194 case htons(ETH_P_RARP):
195 nf_log_packet(net, NFPROTO_ARP, hooknum, skb, in, out,
196 loginfo, "%s", prefix);
200 EXPORT_SYMBOL_GPL(nf_log_l2packet);
202 static int __init nf_log_common_init(void)
207 static void __exit nf_log_common_exit(void) {}
209 module_init(nf_log_common_init);
210 module_exit(nf_log_common_exit);
212 MODULE_LICENSE("GPL");