Skip to content

Commit bcbde0d

Browse files
Daniel Borkmanndavem330
authored andcommitted
net: netlink: virtual tap device management
Similarly to the networking receive path with ptype_all taps, we add the possibility to register netdevices that are for ARPHRD_NETLINK to the netlink subsystem, so that those can be used for netlink analyzers resp. debuggers. We do not offer a direct callback function as out-of-tree modules could do crap with it. Instead, a netdevice must be registered properly and only receives a clone, managed by the netlink layer. Symbols are exported as GPL-only. Signed-off-by: Daniel Borkmann <dborkman@redhat.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 77e2af0 commit bcbde0d

2 files changed

Lines changed: 117 additions & 0 deletions

File tree

‎include/linux/netlink.h‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,4 +145,14 @@ static inline int netlink_dump_start(struct sock *ssk, struct sk_buff *skb,
145145
return __netlink_dump_start(ssk, skb, nlh, control);
146146
}
147147

148+
struct netlink_tap {
149+
struct net_device *dev;
150+
struct module *module;
151+
struct list_head list;
152+
};
153+
154+
extern int netlink_add_tap(struct netlink_tap *nt);
155+
extern int __netlink_remove_tap(struct netlink_tap *nt);
156+
extern int netlink_remove_tap(struct netlink_tap *nt);
157+
148158
#endif /* __LINUX_NETLINK_H */

‎net/netlink/af_netlink.c‎

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
#include <linux/audit.h>
5858
#include <linux/mutex.h>
5959
#include <linux/vmalloc.h>
60+
#include <linux/if_arp.h>
6061
#include <asm/cacheflush.h>
6162

6263
#include <net/net_namespace.h>
@@ -101,6 +102,9 @@ static atomic_t nl_table_users = ATOMIC_INIT(0);
101102

102103
static ATOMIC_NOTIFIER_HEAD(netlink_chain);
103104

105+
static DEFINE_SPINLOCK(netlink_tap_lock);
106+
static struct list_head netlink_tap_all __read_mostly;
107+
104108
static inline u32 netlink_group_mask(u32 group)
105109
{
106110
return group ? 1 << (group - 1) : 0;
@@ -111,6 +115,100 @@ static inline struct hlist_head *nl_portid_hashfn(struct nl_portid_hash *hash, u
111115
return &hash->table[jhash_1word(portid, hash->rnd) & hash->mask];
112116
}
113117

118+
int netlink_add_tap(struct netlink_tap *nt)
119+
{
120+
if (unlikely(nt->dev->type != ARPHRD_NETLINK))
121+
return -EINVAL;
122+
123+
spin_lock(&netlink_tap_lock);
124+
list_add_rcu(&nt->list, &netlink_tap_all);
125+
spin_unlock(&netlink_tap_lock);
126+
127+
if (nt->module)
128+
__module_get(nt->module);
129+
130+
return 0;
131+
}
132+
EXPORT_SYMBOL_GPL(netlink_add_tap);
133+
134+
int __netlink_remove_tap(struct netlink_tap *nt)
135+
{
136+
bool found = false;
137+
struct netlink_tap *tmp;
138+
139+
spin_lock(&netlink_tap_lock);
140+
141+
list_for_each_entry(tmp, &netlink_tap_all, list) {
142+
if (nt == tmp) {
143+
list_del_rcu(&nt->list);
144+
found = true;
145+
goto out;
146+
}
147+
}
148+
149+
pr_warn("__netlink_remove_tap: %p not found\n", nt);
150+
out:
151+
spin_unlock(&netlink_tap_lock);
152+
153+
if (found && nt->module)
154+
module_put(nt->module);
155+
156+
return found ? 0 : -ENODEV;
157+
}
158+
EXPORT_SYMBOL_GPL(__netlink_remove_tap);
159+
160+
int netlink_remove_tap(struct netlink_tap *nt)
161+
{
162+
int ret;
163+
164+
ret = __netlink_remove_tap(nt);
165+
synchronize_net();
166+
167+
return ret;
168+
}
169+
EXPORT_SYMBOL_GPL(netlink_remove_tap);
170+
171+
static int __netlink_deliver_tap_skb(struct sk_buff *skb,
172+
struct net_device *dev)
173+
{
174+
struct sk_buff *nskb;
175+
int ret = -ENOMEM;
176+
177+
dev_hold(dev);
178+
nskb = skb_clone(skb, GFP_ATOMIC);
179+
if (nskb) {
180+
nskb->dev = dev;
181+
ret = dev_queue_xmit(nskb);
182+
if (unlikely(ret > 0))
183+
ret = net_xmit_errno(ret);
184+
}
185+
186+
dev_put(dev);
187+
return ret;
188+
}
189+
190+
static void __netlink_deliver_tap(struct sk_buff *skb)
191+
{
192+
int ret;
193+
struct netlink_tap *tmp;
194+
195+
list_for_each_entry_rcu(tmp, &netlink_tap_all, list) {
196+
ret = __netlink_deliver_tap_skb(skb, tmp->dev);
197+
if (unlikely(ret))
198+
break;
199+
}
200+
}
201+
202+
static void netlink_deliver_tap(struct sk_buff *skb)
203+
{
204+
rcu_read_lock();
205+
206+
if (unlikely(!list_empty(&netlink_tap_all)))
207+
__netlink_deliver_tap(skb);
208+
209+
rcu_read_unlock();
210+
}
211+
114212
static void netlink_overrun(struct sock *sk)
115213
{
116214
struct netlink_sock *nlk = nlk_sk(sk);
@@ -1518,6 +1616,8 @@ static int __netlink_sendskb(struct sock *sk, struct sk_buff *skb)
15181616
{
15191617
int len = skb->len;
15201618

1619+
netlink_deliver_tap(skb);
1620+
15211621
#ifdef CONFIG_NETLINK_MMAP
15221622
if (netlink_skb_is_mmaped(skb))
15231623
netlink_queue_mmaped_skb(sk, skb);
@@ -1578,6 +1678,11 @@ static int netlink_unicast_kernel(struct sock *sk, struct sk_buff *skb,
15781678

15791679
ret = -ECONNREFUSED;
15801680
if (nlk->netlink_rcv != NULL) {
1681+
/* We could do a netlink_deliver_tap(skb) here as well
1682+
* but since this is intended for the kernel only, we
1683+
* should rather let it stay under the hood.
1684+
*/
1685+
15811686
ret = skb->len;
15821687
netlink_skb_set_owner_r(skb, sk);
15831688
NETLINK_CB(skb).sk = ssk;
@@ -2975,6 +3080,8 @@ static int __init netlink_proto_init(void)
29753080
nl_table[i].compare = netlink_compare;
29763081
}
29773082

3083+
INIT_LIST_HEAD(&netlink_tap_all);
3084+
29783085
netlink_add_usersock_entry();
29793086

29803087
sock_register(&netlink_family_ops);

0 commit comments

Comments
 (0)