Skip to content

Commit 384317e

Browse files
hartkoppmarckleinebudde
authored andcommitted
can: network namespace support for CAN_BCM protocol
The CAN_BCM protocol and its procfs entries were not implemented as per-net in the initial network namespace support by Mario Kicherer (8e8cda6). This patch adds the missing per-net functionality for the CAN BCM. Signed-off-by: Oliver Hartkopp <[email protected]> Signed-off-by: Marc Kleine-Budde <[email protected]>
1 parent cb5635a commit 384317e

File tree

2 files changed

+58
-33
lines changed

2 files changed

+58
-33
lines changed

include/net/netns/can.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ struct netns_can {
2323
struct proc_dir_entry *pde_rcvlist_sff;
2424
struct proc_dir_entry *pde_rcvlist_eff;
2525
struct proc_dir_entry *pde_rcvlist_err;
26+
struct proc_dir_entry *bcmproc_dir;
2627
#endif
2728

2829
/* receive filters subscribed for 'all' CAN devices */

net/can/bcm.c

Lines changed: 57 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* bcm.c - Broadcast Manager to filter/send (cyclic) CAN content
33
*
4-
* Copyright (c) 2002-2016 Volkswagen Group Electronic Research
4+
* Copyright (c) 2002-2017 Volkswagen Group Electronic Research
55
* All rights reserved.
66
*
77
* Redistribution and use in source and binary forms, with or without
@@ -77,7 +77,7 @@
7777
(CAN_EFF_MASK | CAN_EFF_FLAG | CAN_RTR_FLAG) : \
7878
(CAN_SFF_MASK | CAN_EFF_FLAG | CAN_RTR_FLAG))
7979

80-
#define CAN_BCM_VERSION "20161123"
80+
#define CAN_BCM_VERSION "20170425"
8181

8282
MODULE_DESCRIPTION("PF_CAN broadcast manager protocol");
8383
MODULE_LICENSE("Dual BSD/GPL");
@@ -118,8 +118,6 @@ struct bcm_op {
118118
struct net_device *rx_reg_dev;
119119
};
120120

121-
static struct proc_dir_entry *proc_dir;
122-
123121
struct bcm_sock {
124122
struct sock sk;
125123
int bound;
@@ -149,15 +147,15 @@ static inline ktime_t bcm_timeval_to_ktime(struct bcm_timeval tv)
149147
/*
150148
* procfs functions
151149
*/
152-
static char *bcm_proc_getifname(char *result, int ifindex)
150+
static char *bcm_proc_getifname(struct net *net, char *result, int ifindex)
153151
{
154152
struct net_device *dev;
155153

156154
if (!ifindex)
157155
return "any";
158156

159157
rcu_read_lock();
160-
dev = dev_get_by_index_rcu(&init_net, ifindex);
158+
dev = dev_get_by_index_rcu(net, ifindex);
161159
if (dev)
162160
strcpy(result, dev->name);
163161
else
@@ -170,15 +168,16 @@ static char *bcm_proc_getifname(char *result, int ifindex)
170168
static int bcm_proc_show(struct seq_file *m, void *v)
171169
{
172170
char ifname[IFNAMSIZ];
173-
struct sock *sk = (struct sock *)m->private;
171+
struct net *net = m->private;
172+
struct sock *sk = (struct sock *)PDE_DATA(m->file->f_inode);
174173
struct bcm_sock *bo = bcm_sk(sk);
175174
struct bcm_op *op;
176175

177176
seq_printf(m, ">>> socket %pK", sk->sk_socket);
178177
seq_printf(m, " / sk %pK", sk);
179178
seq_printf(m, " / bo %pK", bo);
180179
seq_printf(m, " / dropped %lu", bo->dropped_usr_msgs);
181-
seq_printf(m, " / bound %s", bcm_proc_getifname(ifname, bo->ifindex));
180+
seq_printf(m, " / bound %s", bcm_proc_getifname(net, ifname, bo->ifindex));
182181
seq_printf(m, " <<<\n");
183182

184183
list_for_each_entry(op, &bo->rx_ops, list) {
@@ -190,7 +189,7 @@ static int bcm_proc_show(struct seq_file *m, void *v)
190189
continue;
191190

192191
seq_printf(m, "rx_op: %03X %-5s ", op->can_id,
193-
bcm_proc_getifname(ifname, op->ifindex));
192+
bcm_proc_getifname(net, ifname, op->ifindex));
194193

195194
if (op->flags & CAN_FD_FRAME)
196195
seq_printf(m, "(%u)", op->nframes);
@@ -219,7 +218,7 @@ static int bcm_proc_show(struct seq_file *m, void *v)
219218
list_for_each_entry(op, &bo->tx_ops, list) {
220219

221220
seq_printf(m, "tx_op: %03X %s ", op->can_id,
222-
bcm_proc_getifname(ifname, op->ifindex));
221+
bcm_proc_getifname(net, ifname, op->ifindex));
223222

224223
if (op->flags & CAN_FD_FRAME)
225224
seq_printf(m, "(%u) ", op->nframes);
@@ -242,7 +241,7 @@ static int bcm_proc_show(struct seq_file *m, void *v)
242241

243242
static int bcm_proc_open(struct inode *inode, struct file *file)
244243
{
245-
return single_open(file, bcm_proc_show, PDE_DATA(inode));
244+
return single_open_net(inode, file, bcm_proc_show);
246245
}
247246

248247
static const struct file_operations bcm_proc_fops = {
@@ -267,7 +266,7 @@ static void bcm_can_tx(struct bcm_op *op)
267266
if (!op->ifindex)
268267
return;
269268

270-
dev = dev_get_by_index(&init_net, op->ifindex);
269+
dev = dev_get_by_index(sock_net(op->sk), op->ifindex);
271270
if (!dev) {
272271
/* RFC: should this bcm_op remove itself here? */
273272
return;
@@ -764,7 +763,7 @@ static void bcm_remove_op(struct bcm_op *op)
764763
static void bcm_rx_unreg(struct net_device *dev, struct bcm_op *op)
765764
{
766765
if (op->rx_reg_dev == dev) {
767-
can_rx_unregister(&init_net, dev, op->can_id,
766+
can_rx_unregister(dev_net(dev), dev, op->can_id,
768767
REGMASK(op->can_id), bcm_rx_handler, op);
769768

770769
/* mark as removed subscription */
@@ -800,15 +799,16 @@ static int bcm_delete_rx_op(struct list_head *ops, struct bcm_msg_head *mh,
800799
if (op->rx_reg_dev) {
801800
struct net_device *dev;
802801

803-
dev = dev_get_by_index(&init_net,
802+
dev = dev_get_by_index(sock_net(op->sk),
804803
op->ifindex);
805804
if (dev) {
806805
bcm_rx_unreg(dev, op);
807806
dev_put(dev);
808807
}
809808
}
810809
} else
811-
can_rx_unregister(&init_net, NULL, op->can_id,
810+
can_rx_unregister(sock_net(op->sk), NULL,
811+
op->can_id,
812812
REGMASK(op->can_id),
813813
bcm_rx_handler, op);
814814

@@ -1220,9 +1220,9 @@ static int bcm_rx_setup(struct bcm_msg_head *msg_head, struct msghdr *msg,
12201220
if (ifindex) {
12211221
struct net_device *dev;
12221222

1223-
dev = dev_get_by_index(&init_net, ifindex);
1223+
dev = dev_get_by_index(sock_net(sk), ifindex);
12241224
if (dev) {
1225-
err = can_rx_register(&init_net, dev,
1225+
err = can_rx_register(sock_net(sk), dev,
12261226
op->can_id,
12271227
REGMASK(op->can_id),
12281228
bcm_rx_handler, op,
@@ -1233,7 +1233,7 @@ static int bcm_rx_setup(struct bcm_msg_head *msg_head, struct msghdr *msg,
12331233
}
12341234

12351235
} else
1236-
err = can_rx_register(&init_net, NULL, op->can_id,
1236+
err = can_rx_register(sock_net(sk), NULL, op->can_id,
12371237
REGMASK(op->can_id),
12381238
bcm_rx_handler, op, "bcm", sk);
12391239
if (err) {
@@ -1273,7 +1273,7 @@ static int bcm_tx_send(struct msghdr *msg, int ifindex, struct sock *sk,
12731273
return err;
12741274
}
12751275

1276-
dev = dev_get_by_index(&init_net, ifindex);
1276+
dev = dev_get_by_index(sock_net(sk), ifindex);
12771277
if (!dev) {
12781278
kfree_skb(skb);
12791279
return -ENODEV;
@@ -1338,7 +1338,7 @@ static int bcm_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)
13381338
if (ifindex) {
13391339
struct net_device *dev;
13401340

1341-
dev = dev_get_by_index(&init_net, ifindex);
1341+
dev = dev_get_by_index(sock_net(sk), ifindex);
13421342
if (!dev)
13431343
return -ENODEV;
13441344

@@ -1419,7 +1419,7 @@ static int bcm_notifier(struct notifier_block *nb, unsigned long msg,
14191419
struct bcm_op *op;
14201420
int notify_enodev = 0;
14211421

1422-
if (!net_eq(dev_net(dev), &init_net))
1422+
if (!net_eq(dev_net(dev), sock_net(sk)))
14231423
return NOTIFY_DONE;
14241424

14251425
if (dev->type != ARPHRD_CAN)
@@ -1491,6 +1491,7 @@ static int bcm_init(struct sock *sk)
14911491
static int bcm_release(struct socket *sock)
14921492
{
14931493
struct sock *sk = sock->sk;
1494+
struct net *net = sock_net(sk);
14941495
struct bcm_sock *bo;
14951496
struct bcm_op *op, *next;
14961497

@@ -1522,23 +1523,23 @@ static int bcm_release(struct socket *sock)
15221523
if (op->rx_reg_dev) {
15231524
struct net_device *dev;
15241525

1525-
dev = dev_get_by_index(&init_net, op->ifindex);
1526+
dev = dev_get_by_index(net, op->ifindex);
15261527
if (dev) {
15271528
bcm_rx_unreg(dev, op);
15281529
dev_put(dev);
15291530
}
15301531
}
15311532
} else
1532-
can_rx_unregister(&init_net, NULL, op->can_id,
1533+
can_rx_unregister(net, NULL, op->can_id,
15331534
REGMASK(op->can_id),
15341535
bcm_rx_handler, op);
15351536

15361537
bcm_remove_op(op);
15371538
}
15381539

15391540
/* remove procfs entry */
1540-
if (proc_dir && bo->bcm_proc_read)
1541-
remove_proc_entry(bo->procname, proc_dir);
1541+
if (net->can.bcmproc_dir && bo->bcm_proc_read)
1542+
remove_proc_entry(bo->procname, net->can.bcmproc_dir);
15421543

15431544
/* remove device reference */
15441545
if (bo->bound) {
@@ -1561,6 +1562,7 @@ static int bcm_connect(struct socket *sock, struct sockaddr *uaddr, int len,
15611562
struct sockaddr_can *addr = (struct sockaddr_can *)uaddr;
15621563
struct sock *sk = sock->sk;
15631564
struct bcm_sock *bo = bcm_sk(sk);
1565+
struct net *net = sock_net(sk);
15641566
int ret = 0;
15651567

15661568
if (len < sizeof(*addr))
@@ -1577,7 +1579,7 @@ static int bcm_connect(struct socket *sock, struct sockaddr *uaddr, int len,
15771579
if (addr->can_ifindex) {
15781580
struct net_device *dev;
15791581

1580-
dev = dev_get_by_index(&init_net, addr->can_ifindex);
1582+
dev = dev_get_by_index(net, addr->can_ifindex);
15811583
if (!dev) {
15821584
ret = -ENODEV;
15831585
goto fail;
@@ -1596,11 +1598,11 @@ static int bcm_connect(struct socket *sock, struct sockaddr *uaddr, int len,
15961598
bo->ifindex = 0;
15971599
}
15981600

1599-
if (proc_dir) {
1601+
if (net->can.bcmproc_dir) {
16001602
/* unique socket address as filename */
16011603
sprintf(bo->procname, "%lu", sock_i_ino(sk));
16021604
bo->bcm_proc_read = proc_create_data(bo->procname, 0644,
1603-
proc_dir,
1605+
net->can.bcmproc_dir,
16041606
&bcm_proc_fops, sk);
16051607
if (!bo->bcm_proc_read) {
16061608
ret = -ENOMEM;
@@ -1687,6 +1689,31 @@ static const struct can_proto bcm_can_proto = {
16871689
.prot = &bcm_proto,
16881690
};
16891691

1692+
static int canbcm_pernet_init(struct net *net)
1693+
{
1694+
/* create /proc/net/can-bcm directory */
1695+
if (IS_ENABLED(CONFIG_PROC_FS)) {
1696+
net->can.bcmproc_dir =
1697+
proc_net_mkdir(net, "can-bcm", net->proc_net);
1698+
}
1699+
1700+
return 0;
1701+
}
1702+
1703+
static void canbcm_pernet_exit(struct net *net)
1704+
{
1705+
/* remove /proc/net/can-bcm directory */
1706+
if (IS_ENABLED(CONFIG_PROC_FS)) {
1707+
if (net->can.bcmproc_dir)
1708+
remove_proc_entry("can-bcm", net->proc_net);
1709+
}
1710+
}
1711+
1712+
static struct pernet_operations canbcm_pernet_ops __read_mostly = {
1713+
.init = canbcm_pernet_init,
1714+
.exit = canbcm_pernet_exit,
1715+
};
1716+
16901717
static int __init bcm_module_init(void)
16911718
{
16921719
int err;
@@ -1699,17 +1726,14 @@ static int __init bcm_module_init(void)
16991726
return err;
17001727
}
17011728

1702-
/* create /proc/net/can-bcm directory */
1703-
proc_dir = proc_mkdir("can-bcm", init_net.proc_net);
1729+
register_pernet_subsys(&canbcm_pernet_ops);
17041730
return 0;
17051731
}
17061732

17071733
static void __exit bcm_module_exit(void)
17081734
{
17091735
can_proto_unregister(&bcm_can_proto);
1710-
1711-
if (proc_dir)
1712-
remove_proc_entry("can-bcm", init_net.proc_net);
1736+
unregister_pernet_subsys(&canbcm_pernet_ops);
17131737
}
17141738

17151739
module_init(bcm_module_init);

0 commit comments

Comments
 (0)