• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef NFLOG_LISTENER_H
18 #define NFLOG_LISTENER_H
19 
20 #include <netdutils/Netfilter.h>
21 
22 #include "NetlinkListener.h"
23 
24 namespace android {
25 namespace net {
26 
27 class NFLogListenerInterface {
28   public:
29     using DispatchFn =
30         std::function<void(const nlmsghdr& nlmsg, const nfgenmsg& nfmsg,
31                            const netdutils::Slice msg)>;
32 
33     virtual ~NFLogListenerInterface() = default;
34 
35     // Similar to NetlinkListener::subscribe() but performs an additional
36     // level of deserialization and dispatch.
37     //
38     // Threadsafe.
39     // All dispatch functions invoked on a single service thread.
40     // subscribe() and join() must not be called from the stack of fn().
41     virtual netdutils::Status subscribe(uint16_t nfLogGroup, const DispatchFn& fn) = 0;
42 
43     // Overloaded version of subscribe which allows to specify a copyRange for obtaining packet
44     // payloads.
45     virtual netdutils::Status subscribe(
46             uint16_t nfLogGroup, uint32_t copyRange, const DispatchFn& fn) = 0;
47 
48     // Halt delivery of messages from a nfLogGroup previously subscribed to above.
49     //
50     // Threadsafe.
51     virtual netdutils::Status unsubscribe(uint16_t nfLogGroup) = 0;
52 };
53 
54 // NFLogListener manages a single netlink socket with specialized
55 // settings required for processing of NFLOG messages.
56 //
57 // NFLogListener currently assumes that it is ok to drop messages
58 // generated by the kernel when under heavy load. This makes the
59 // class most suitable for advisory tasks and statistics.
60 class NFLogListener : public NFLogListenerInterface {
61   public:
62     using DispatchFn = NFLogListenerInterface::DispatchFn;
63 
64     // Do not invoke this constructor directly outside of tests. Use
65     // makeNFLogListener() instead.
66     NFLogListener(std::shared_ptr<NetlinkListenerInterface> listener);
67 
68     ~NFLogListener() override;
69 
70     netdutils::Status subscribe(uint16_t nfLogGroup, const DispatchFn& fn) override;
71 
72     netdutils::Status subscribe(
73             uint16_t nfLogGroup, uint32_t copyRange, const DispatchFn& fn) override;
74 
75     netdutils::Status unsubscribe(uint16_t nfLogGroup) override;
76 
77   private:
78     std::shared_ptr<NetlinkListenerInterface> mListener;
79     std::mutex mMutex;
80     std::map<uint16_t, DispatchFn> mDispatchMap;  // guarded by mMutex
81 };
82 
83 // Allocate and return a new NFLogListener. On success, the returned
84 // listener is ready to use with a running service thread.
85 netdutils::StatusOr<std::unique_ptr<NFLogListener>> makeNFLogListener();
86 
87 }  // namespace net
88 }  // namespace android
89 
90 #endif /* NFLOG_LISTENER_H */
91