libnl  3.3.0
log_msg.c
1 /*
2  * lib/netfilter/log_msg.c Netfilter Log Message
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation version 2.1
7  * of the License.
8  *
9  * Copyright (c) 2003-2008 Thomas Graf <tgraf@suug.ch>
10  * Copyright (c) 2007 Philip Craig <philipc@snapgear.com>
11  * Copyright (c) 2007 Secure Computing Corporation
12  * Copyright (c) 2008 Patrick McHardy <kaber@trash.net>
13  */
14 
15 /**
16  * @ingroup nfnl
17  * @defgroup log Log
18  * @brief
19  * @{
20  */
21 
22 #include <sys/types.h>
23 #include <linux/netfilter/nfnetlink_log.h>
24 
25 #include <netlink-private/netlink.h>
26 #include <netlink/attr.h>
27 #include <netlink/netfilter/nfnl.h>
28 #include <netlink/netfilter/log_msg.h>
29 #include <netlink-private/utils.h>
30 
31 static struct nla_policy log_msg_policy[NFULA_MAX+1] = {
32  [NFULA_PACKET_HDR] = {
33  .minlen = sizeof(struct nfulnl_msg_packet_hdr)
34  },
35  [NFULA_MARK] = { .type = NLA_U32 },
36  [NFULA_TIMESTAMP] = {
37  .minlen = sizeof(struct nfulnl_msg_packet_timestamp)
38  },
39  [NFULA_IFINDEX_INDEV] = { .type = NLA_U32 },
40  [NFULA_IFINDEX_OUTDEV] = { .type = NLA_U32 },
41  [NFULA_IFINDEX_PHYSINDEV] = { .type = NLA_U32 },
42  [NFULA_IFINDEX_PHYSOUTDEV] = { .type = NLA_U32 },
43  [NFULA_HWADDR] = {
44  .minlen = sizeof(struct nfulnl_msg_packet_hw)
45  },
46  //[NFULA_PAYLOAD]
47  [NFULA_PREFIX] = { .type = NLA_STRING, },
48  [NFULA_UID] = { .type = NLA_U32 },
49  [NFULA_GID] = { .type = NLA_U32 },
50  [NFULA_SEQ] = { .type = NLA_U32 },
51  [NFULA_SEQ_GLOBAL] = { .type = NLA_U32 },
52 };
53 
54 int nfnlmsg_log_msg_parse(struct nlmsghdr *nlh, struct nfnl_log_msg **result)
55 {
56  struct nfnl_log_msg *msg;
57  struct nlattr *tb[NFULA_MAX+1];
58  struct nlattr *attr;
59  int err;
60 
61  msg = nfnl_log_msg_alloc();
62  if (!msg)
63  return -NLE_NOMEM;
64 
65  msg->ce_msgtype = nlh->nlmsg_type;
66 
67  err = nlmsg_parse(nlh, sizeof(struct nfgenmsg), tb, NFULA_MAX,
68  log_msg_policy);
69  if (err < 0)
70  goto errout;
71 
72  nfnl_log_msg_set_family(msg, nfnlmsg_family(nlh));
73 
74  attr = tb[NFULA_PACKET_HDR];
75  if (attr) {
76  struct nfulnl_msg_packet_hdr *hdr = nla_data(attr);
77 
78  if (hdr->hw_protocol)
79  nfnl_log_msg_set_hwproto(msg, hdr->hw_protocol);
80  nfnl_log_msg_set_hook(msg, hdr->hook);
81  }
82 
83  attr = tb[NFULA_MARK];
84  if (attr)
85  nfnl_log_msg_set_mark(msg, ntohl(nla_get_u32(attr)));
86 
87  attr = tb[NFULA_TIMESTAMP];
88  if (attr) {
89  struct nfulnl_msg_packet_timestamp *timestamp = nla_data(attr);
90  struct timeval tv;
91 
92  tv.tv_sec = ntohll(timestamp->sec);
93  tv.tv_usec = ntohll(timestamp->usec);
94  nfnl_log_msg_set_timestamp(msg, &tv);
95  }
96 
97  attr = tb[NFULA_IFINDEX_INDEV];
98  if (attr)
99  nfnl_log_msg_set_indev(msg, ntohl(nla_get_u32(attr)));
100 
101  attr = tb[NFULA_IFINDEX_OUTDEV];
102  if (attr)
103  nfnl_log_msg_set_outdev(msg, ntohl(nla_get_u32(attr)));
104 
105  attr = tb[NFULA_IFINDEX_PHYSINDEV];
106  if (attr)
107  nfnl_log_msg_set_physindev(msg, ntohl(nla_get_u32(attr)));
108 
109  attr = tb[NFULA_IFINDEX_PHYSOUTDEV];
110  if (attr)
111  nfnl_log_msg_set_physoutdev(msg, ntohl(nla_get_u32(attr)));
112 
113  attr = tb[NFULA_HWADDR];
114  if (attr) {
115  struct nfulnl_msg_packet_hw *hw = nla_data(attr);
116 
117  nfnl_log_msg_set_hwaddr(msg, hw->hw_addr, ntohs(hw->hw_addrlen));
118  }
119 
120  attr = tb[NFULA_PAYLOAD];
121  if (attr) {
122  err = nfnl_log_msg_set_payload(msg, nla_data(attr), nla_len(attr));
123  if (err < 0)
124  goto errout;
125  }
126 
127  attr = tb[NFULA_PREFIX];
128  if (attr) {
129  err = nfnl_log_msg_set_prefix(msg, nla_data(attr));
130  if (err < 0)
131  goto errout;
132  }
133 
134  attr = tb[NFULA_UID];
135  if (attr)
136  nfnl_log_msg_set_uid(msg, ntohl(nla_get_u32(attr)));
137 
138  attr = tb[NFULA_GID];
139  if (attr)
140  nfnl_log_msg_set_gid(msg, ntohl(nla_get_u32(attr)));
141 
142  attr = tb[NFULA_SEQ];
143  if (attr)
144  nfnl_log_msg_set_seq(msg, ntohl(nla_get_u32(attr)));
145 
146  attr = tb[NFULA_SEQ_GLOBAL];
147  if (attr)
148  nfnl_log_msg_set_seq_global(msg, ntohl(nla_get_u32(attr)));
149 
150  *result = msg;
151  return 0;
152 
153 errout:
154  nfnl_log_msg_put(msg);
155  return err;
156 }
157 
158 static int log_msg_parser(struct nl_cache_ops *ops, struct sockaddr_nl *who,
159  struct nlmsghdr *nlh, struct nl_parser_param *pp)
160 {
161  struct nfnl_log_msg *msg;
162  int err;
163 
164  if ((err = nfnlmsg_log_msg_parse(nlh, &msg)) < 0)
165  return err;
166 
167  err = pp->pp_cb((struct nl_object *) msg, pp);
168  nfnl_log_msg_put(msg);
169  return err;
170 }
171 
172 /** @} */
173 
174 #define NFNLMSG_LOG_TYPE(type) NFNLMSG_TYPE(NFNL_SUBSYS_ULOG, (type))
175 static struct nl_cache_ops nfnl_log_msg_ops = {
176  .co_name = "netfilter/log_msg",
177  .co_hdrsize = NFNL_HDRLEN,
178  .co_msgtypes = {
179  { NFNLMSG_LOG_TYPE(NFULNL_MSG_PACKET), NL_ACT_NEW, "new" },
180  END_OF_MSGTYPES_LIST,
181  },
182  .co_protocol = NETLINK_NETFILTER,
183  .co_msg_parser = log_msg_parser,
184  .co_obj_ops = &log_msg_obj_ops,
185 };
186 
187 static void __init log_msg_init(void)
188 {
189  nl_cache_mngt_register(&nfnl_log_msg_ops);
190 }
191 
192 static void __exit log_msg_exit(void)
193 {
194  nl_cache_mngt_unregister(&nfnl_log_msg_ops);
195 }
196 
197 /** @} */
int nl_cache_mngt_unregister(struct nl_cache_ops *ops)
Unregister a set of cache operations.
Definition: cache_mngt.c:287
Attribute validation policy.
Definition: attr.h:69
uint32_t nla_get_u32(const struct nlattr *nla)
Return payload of 32 bit integer attribute.
Definition: attr.c:706
int nlmsg_parse(struct nlmsghdr *nlh, int hdrlen, struct nlattr *tb[], int maxtype, struct nla_policy *policy)
parse attributes of a netlink message
Definition: msg.c:214
NUL terminated character string.
Definition: attr.h:45
int nl_cache_mngt_register(struct nl_cache_ops *ops)
Register a set of cache operations.
Definition: cache_mngt.c:252
void * nla_data(const struct nlattr *nla)
Return pointer to the payload section.
Definition: attr.c:120
int nla_len(const struct nlattr *nla)
Return length of the payload .
Definition: attr.c:131
uint8_t nfnlmsg_family(struct nlmsghdr *nlh)
Get netfilter family from message.
Definition: nfnl.c:152
uint16_t minlen
Minimal length of payload required.
Definition: attr.h:74
32 bit integer
Definition: attr.h:43