Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

can: initial support for network namespaces

This patch adds initial support for network namespaces. The changes only
enable support in the CAN raw, proc and af_can code. GW and BCM still
have their checks that ensure that they are used only from the main
namespace.

The patch boils down to moving the global structures, i.e. the global
filter list and their /proc stats, into a per-namespace structure and passing
around the corresponding "struct net" in a lot of different places.

Changes since v1:
- rebased on current HEAD (2bfe01e)
- fixed overlong line

Signed-off-by: Mario Kicherer <dev@kicherer.org>
Signed-off-by: Marc Kleine-Budde <mkl@pengutronix.de>

authored by

Mario Kicherer and committed by
Marc Kleine-Budde
8e8cda6d dabf54dd

+242 -179
+4 -3
include/linux/can/core.h
··· 45 45 extern int can_proto_register(const struct can_proto *cp); 46 46 extern void can_proto_unregister(const struct can_proto *cp); 47 47 48 - int can_rx_register(struct net_device *dev, canid_t can_id, canid_t mask, 48 + int can_rx_register(struct net *net, struct net_device *dev, 49 + canid_t can_id, canid_t mask, 49 50 void (*func)(struct sk_buff *, void *), 50 51 void *data, char *ident, struct sock *sk); 51 52 52 - extern void can_rx_unregister(struct net_device *dev, canid_t can_id, 53 - canid_t mask, 53 + extern void can_rx_unregister(struct net *net, struct net_device *dev, 54 + canid_t can_id, canid_t mask, 54 55 void (*func)(struct sk_buff *, void *), 55 56 void *data); 56 57
+4
include/net/net_namespace.h
··· 27 27 #include <net/netns/nftables.h> 28 28 #include <net/netns/xfrm.h> 29 29 #include <net/netns/mpls.h> 30 + #include <net/netns/can.h> 30 31 #include <linux/ns_common.h> 31 32 #include <linux/idr.h> 32 33 #include <linux/skbuff.h> ··· 141 140 #endif 142 141 #if IS_ENABLED(CONFIG_MPLS) 143 142 struct netns_mpls mpls; 143 + #endif 144 + #if IS_ENABLED(CONFIG_CAN) 145 + struct netns_can can; 144 146 #endif 145 147 struct sock *diag_nlsk; 146 148 atomic_t fnhe_genid;
+31
include/net/netns/can.h
··· 1 + /* 2 + * can in net namespaces 3 + */ 4 + 5 + #ifndef __NETNS_CAN_H__ 6 + #define __NETNS_CAN_H__ 7 + 8 + #include <linux/spinlock.h> 9 + 10 + struct dev_rcv_lists; 11 + 12 + struct netns_can { 13 + #if IS_ENABLED(CONFIG_PROC_FS) 14 + struct proc_dir_entry *proc_dir; 15 + struct proc_dir_entry *pde_version; 16 + struct proc_dir_entry *pde_stats; 17 + struct proc_dir_entry *pde_reset_stats; 18 + struct proc_dir_entry *pde_rcvlist_all; 19 + struct proc_dir_entry *pde_rcvlist_fil; 20 + struct proc_dir_entry *pde_rcvlist_inv; 21 + struct proc_dir_entry *pde_rcvlist_sff; 22 + struct proc_dir_entry *pde_rcvlist_eff; 23 + struct proc_dir_entry *pde_rcvlist_err; 24 + #endif 25 + 26 + /* receive filters subscribed for 'all' CAN devices */ 27 + struct dev_rcv_lists *can_rx_alldev_list; 28 + spinlock_t can_rcvlists_lock; 29 + }; 30 + 31 + #endif /* __NETNS_CAN_H__ */
+70 -52
net/can/af_can.c
··· 75 75 module_param(stats_timer, int, S_IRUGO); 76 76 MODULE_PARM_DESC(stats_timer, "enable timer for statistics (default:on)"); 77 77 78 - /* receive filters subscribed for 'all' CAN devices */ 79 - struct dev_rcv_lists can_rx_alldev_list; 80 - static DEFINE_SPINLOCK(can_rcvlists_lock); 78 + static int can_net_id; 81 79 82 80 static struct kmem_cache *rcv_cache __read_mostly; 83 81 ··· 142 144 143 145 if (protocol < 0 || protocol >= CAN_NPROTO) 144 146 return -EINVAL; 145 - 146 - if (!net_eq(net, &init_net)) 147 - return -EAFNOSUPPORT; 148 147 149 148 cp = can_get_proto(protocol); 150 149 ··· 326 331 * af_can rx path 327 332 */ 328 333 329 - static struct dev_rcv_lists *find_dev_rcv_lists(struct net_device *dev) 334 + static struct dev_rcv_lists *find_dev_rcv_lists(struct net *net, 335 + struct net_device *dev) 330 336 { 331 337 if (!dev) 332 - return &can_rx_alldev_list; 338 + return net->can.can_rx_alldev_list; 333 339 else 334 340 return (struct dev_rcv_lists *)dev->ml_priv; 335 341 } ··· 463 467 * -ENOMEM on missing cache mem to create subscription entry 464 468 * -ENODEV unknown device 465 469 */ 466 - int can_rx_register(struct net_device *dev, canid_t can_id, canid_t mask, 467 - void (*func)(struct sk_buff *, void *), void *data, 468 - char *ident, struct sock *sk) 470 + int can_rx_register(struct net *net, struct net_device *dev, canid_t can_id, 471 + canid_t mask, void (*func)(struct sk_buff *, void *), 472 + void *data, char *ident, struct sock *sk) 469 473 { 470 474 struct receiver *r; 471 475 struct hlist_head *rl; ··· 477 481 if (dev && dev->type != ARPHRD_CAN) 478 482 return -ENODEV; 479 483 484 + if (dev && !net_eq(net, dev_net(dev))) 485 + return -ENODEV; 486 + 480 487 r = kmem_cache_alloc(rcv_cache, GFP_KERNEL); 481 488 if (!r) 482 489 return -ENOMEM; 483 490 484 - spin_lock(&can_rcvlists_lock); 491 + spin_lock(&net->can.can_rcvlists_lock); 485 492 486 - d = find_dev_rcv_lists(dev); 493 + d = find_dev_rcv_lists(net, dev); 487 494 if (d) { 488 495 rl = find_rcv_list(&can_id, &mask, d); 489 496 ··· 509 510 err = -ENODEV; 510 511 } 511 512 512 - spin_unlock(&can_rcvlists_lock); 513 + spin_unlock(&net->can.can_rcvlists_lock); 513 514 514 515 return err; 515 516 } ··· 539 540 * Description: 540 541 * Removes subscription entry depending on given (subscription) values. 541 542 */ 542 - void can_rx_unregister(struct net_device *dev, canid_t can_id, canid_t mask, 543 - void (*func)(struct sk_buff *, void *), void *data) 543 + void can_rx_unregister(struct net *net, struct net_device *dev, canid_t can_id, 544 + canid_t mask, void (*func)(struct sk_buff *, void *), 545 + void *data) 544 546 { 545 547 struct receiver *r = NULL; 546 548 struct hlist_head *rl; ··· 550 550 if (dev && dev->type != ARPHRD_CAN) 551 551 return; 552 552 553 - spin_lock(&can_rcvlists_lock); 553 + if (dev && !net_eq(net, dev_net(dev))) 554 + return; 554 555 555 - d = find_dev_rcv_lists(dev); 556 + spin_lock(&net->can.can_rcvlists_lock); 557 + 558 + d = find_dev_rcv_lists(net, dev); 556 559 if (!d) { 557 560 pr_err("BUG: receive list not found for " 558 561 "dev %s, id %03X, mask %03X\n", ··· 601 598 } 602 599 603 600 out: 604 - spin_unlock(&can_rcvlists_lock); 601 + spin_unlock(&net->can.can_rcvlists_lock); 605 602 606 603 /* schedule the receiver item for deletion */ 607 604 if (r) { ··· 699 696 rcu_read_lock(); 700 697 701 698 /* deliver the packet to sockets listening on all devices */ 702 - matches = can_rcv_filter(&can_rx_alldev_list, skb); 699 + matches = can_rcv_filter(dev_net(dev)->can.can_rx_alldev_list, skb); 703 700 704 701 /* find receive list for this device */ 705 - d = find_dev_rcv_lists(dev); 702 + d = find_dev_rcv_lists(dev_net(dev), dev); 706 703 if (d) 707 704 matches += can_rcv_filter(d, skb); 708 705 ··· 721 718 struct packet_type *pt, struct net_device *orig_dev) 722 719 { 723 720 struct canfd_frame *cfd = (struct canfd_frame *)skb->data; 724 - 725 - if (unlikely(!net_eq(dev_net(dev), &init_net))) 726 - goto drop; 727 721 728 722 if (WARN_ONCE(dev->type != ARPHRD_CAN || 729 723 skb->len != CAN_MTU || ··· 742 742 struct packet_type *pt, struct net_device *orig_dev) 743 743 { 744 744 struct canfd_frame *cfd = (struct canfd_frame *)skb->data; 745 - 746 - if (unlikely(!net_eq(dev_net(dev), &init_net))) 747 - goto drop; 748 745 749 746 if (WARN_ONCE(dev->type != ARPHRD_CAN || 750 747 skb->len != CANFD_MTU || ··· 832 835 struct net_device *dev = netdev_notifier_info_to_dev(ptr); 833 836 struct dev_rcv_lists *d; 834 837 835 - if (!net_eq(dev_net(dev), &init_net)) 836 - return NOTIFY_DONE; 837 - 838 838 if (dev->type != ARPHRD_CAN) 839 839 return NOTIFY_DONE; 840 840 ··· 849 855 break; 850 856 851 857 case NETDEV_UNREGISTER: 852 - spin_lock(&can_rcvlists_lock); 858 + spin_lock(&dev_net(dev)->can.can_rcvlists_lock); 853 859 854 860 d = dev->ml_priv; 855 861 if (d) { ··· 863 869 pr_err("can: notifier: receive list not found for dev " 864 870 "%s\n", dev->name); 865 871 866 - spin_unlock(&can_rcvlists_lock); 872 + spin_unlock(&dev_net(dev)->can.can_rcvlists_lock); 867 873 868 874 break; 869 875 } 870 876 871 877 return NOTIFY_DONE; 878 + } 879 + 880 + static int can_pernet_init(struct net *net) 881 + { 882 + net->can.can_rcvlists_lock = 883 + __SPIN_LOCK_UNLOCKED(net->can.can_rcvlists_lock); 884 + net->can.can_rx_alldev_list = 885 + kzalloc(sizeof(struct dev_rcv_lists), GFP_KERNEL); 886 + 887 + if (IS_ENABLED(CONFIG_PROC_FS)) 888 + can_init_proc(net); 889 + 890 + return 0; 891 + } 892 + 893 + static void can_pernet_exit(struct net *net) 894 + { 895 + struct net_device *dev; 896 + 897 + if (IS_ENABLED(CONFIG_PROC_FS)) 898 + can_remove_proc(net); 899 + 900 + /* remove created dev_rcv_lists from still registered CAN devices */ 901 + rcu_read_lock(); 902 + for_each_netdev_rcu(net, dev) { 903 + if (dev->type == ARPHRD_CAN && dev->ml_priv) { 904 + struct dev_rcv_lists *d = dev->ml_priv; 905 + 906 + BUG_ON(d->entries); 907 + kfree(d); 908 + dev->ml_priv = NULL; 909 + } 910 + } 911 + rcu_read_unlock(); 872 912 } 873 913 874 914 /* ··· 930 902 .notifier_call = can_notifier, 931 903 }; 932 904 905 + static struct pernet_operations can_pernet_ops __read_mostly = { 906 + .init = can_pernet_init, 907 + .exit = can_pernet_exit, 908 + .id = &can_net_id, 909 + .size = 0, 910 + }; 911 + 933 912 static __init int can_init(void) 934 913 { 935 914 /* check for correct padding to be able to use the structs similarly */ ··· 946 911 offsetof(struct canfd_frame, data)); 947 912 948 913 pr_info("can: controller area network core (" CAN_VERSION_STRING ")\n"); 949 - 950 - memset(&can_rx_alldev_list, 0, sizeof(can_rx_alldev_list)); 951 914 952 915 rcv_cache = kmem_cache_create("can_receiver", sizeof(struct receiver), 953 916 0, 0, NULL); ··· 958 925 setup_timer(&can_stattimer, can_stat_update, 0); 959 926 mod_timer(&can_stattimer, round_jiffies(jiffies + HZ)); 960 927 } 961 - can_init_proc(); 962 928 } 929 + 930 + register_pernet_subsys(&can_pernet_ops); 963 931 964 932 /* protocol register */ 965 933 sock_register(&can_family_ops); ··· 973 939 974 940 static __exit void can_exit(void) 975 941 { 976 - struct net_device *dev; 977 - 978 942 if (IS_ENABLED(CONFIG_PROC_FS)) { 979 943 if (stats_timer) 980 944 del_timer_sync(&can_stattimer); 981 - 982 - can_remove_proc(); 983 945 } 984 946 985 947 /* protocol unregister */ ··· 984 954 unregister_netdevice_notifier(&can_netdev_notifier); 985 955 sock_unregister(PF_CAN); 986 956 987 - /* remove created dev_rcv_lists from still registered CAN devices */ 988 - rcu_read_lock(); 989 - for_each_netdev_rcu(&init_net, dev) { 990 - if (dev->type == ARPHRD_CAN && dev->ml_priv) { 991 - 992 - struct dev_rcv_lists *d = dev->ml_priv; 993 - 994 - BUG_ON(d->entries); 995 - kfree(d); 996 - dev->ml_priv = NULL; 997 - } 998 - } 999 - rcu_read_unlock(); 957 + unregister_pernet_subsys(&can_pernet_ops); 1000 958 1001 959 rcu_barrier(); /* Wait for completion of call_rcu()'s */ 1002 960
+2 -2
net/can/af_can.h
··· 114 114 extern struct dev_rcv_lists can_rx_alldev_list; 115 115 116 116 /* function prototypes for the CAN networklayer procfs (proc.c) */ 117 - void can_init_proc(void); 118 - void can_remove_proc(void); 117 + void can_init_proc(struct net *net); 118 + void can_remove_proc(struct net *net); 119 119 void can_stat_update(unsigned long data); 120 120 121 121 /* structures and variables from af_can.c needed in proc.c for reading */
+7 -6
net/can/bcm.c
··· 764 764 static void bcm_rx_unreg(struct net_device *dev, struct bcm_op *op) 765 765 { 766 766 if (op->rx_reg_dev == dev) { 767 - can_rx_unregister(dev, op->can_id, REGMASK(op->can_id), 768 - bcm_rx_handler, op); 767 + can_rx_unregister(&init_net, dev, op->can_id, 768 + REGMASK(op->can_id), bcm_rx_handler, op); 769 769 770 770 /* mark as removed subscription */ 771 771 op->rx_reg_dev = NULL; ··· 808 808 } 809 809 } 810 810 } else 811 - can_rx_unregister(NULL, op->can_id, 811 + can_rx_unregister(&init_net, NULL, op->can_id, 812 812 REGMASK(op->can_id), 813 813 bcm_rx_handler, op); 814 814 ··· 1222 1222 1223 1223 dev = dev_get_by_index(&init_net, ifindex); 1224 1224 if (dev) { 1225 - err = can_rx_register(dev, op->can_id, 1225 + err = can_rx_register(&init_net, dev, 1226 + op->can_id, 1226 1227 REGMASK(op->can_id), 1227 1228 bcm_rx_handler, op, 1228 1229 "bcm", sk); ··· 1233 1232 } 1234 1233 1235 1234 } else 1236 - err = can_rx_register(NULL, op->can_id, 1235 + err = can_rx_register(&init_net, NULL, op->can_id, 1237 1236 REGMASK(op->can_id), 1238 1237 bcm_rx_handler, op, "bcm", sk); 1239 1238 if (err) { ··· 1529 1528 } 1530 1529 } 1531 1530 } else 1532 - can_rx_unregister(NULL, op->can_id, 1531 + can_rx_unregister(&init_net, NULL, op->can_id, 1533 1532 REGMASK(op->can_id), 1534 1533 bcm_rx_handler, op); 1535 1534
+2 -2
net/can/gw.c
··· 440 440 441 441 static inline int cgw_register_filter(struct cgw_job *gwj) 442 442 { 443 - return can_rx_register(gwj->src.dev, gwj->ccgw.filter.can_id, 443 + return can_rx_register(&init_net, gwj->src.dev, gwj->ccgw.filter.can_id, 444 444 gwj->ccgw.filter.can_mask, can_can_gw_rcv, 445 445 gwj, "gw", NULL); 446 446 } 447 447 448 448 static inline void cgw_unregister_filter(struct cgw_job *gwj) 449 449 { 450 - can_rx_unregister(gwj->src.dev, gwj->ccgw.filter.can_id, 450 + can_rx_unregister(&init_net, gwj->src.dev, gwj->ccgw.filter.can_id, 451 451 gwj->ccgw.filter.can_mask, can_can_gw_rcv, gwj); 452 452 } 453 453
+70 -74
net/can/proc.c
··· 62 62 #define CAN_PROC_RCVLIST_EFF "rcvlist_eff" 63 63 #define CAN_PROC_RCVLIST_ERR "rcvlist_err" 64 64 65 - static struct proc_dir_entry *can_dir; 66 - static struct proc_dir_entry *pde_version; 67 - static struct proc_dir_entry *pde_stats; 68 - static struct proc_dir_entry *pde_reset_stats; 69 - static struct proc_dir_entry *pde_rcvlist_all; 70 - static struct proc_dir_entry *pde_rcvlist_fil; 71 - static struct proc_dir_entry *pde_rcvlist_inv; 72 - static struct proc_dir_entry *pde_rcvlist_sff; 73 - static struct proc_dir_entry *pde_rcvlist_eff; 74 - static struct proc_dir_entry *pde_rcvlist_err; 75 - 76 65 static int user_reset; 77 66 78 67 static const char rx_list_name[][8] = { ··· 340 351 static int can_rcvlist_proc_show(struct seq_file *m, void *v) 341 352 { 342 353 /* double cast to prevent GCC warning */ 343 - int idx = (int)(long)m->private; 354 + int idx = (int)(long)PDE_DATA(m->file->f_inode); 344 355 struct net_device *dev; 345 356 struct dev_rcv_lists *d; 357 + struct net *net = m->private; 346 358 347 359 seq_printf(m, "\nreceive list '%s':\n", rx_list_name[idx]); 348 360 349 361 rcu_read_lock(); 350 362 351 363 /* receive list for 'all' CAN devices (dev == NULL) */ 352 - d = &can_rx_alldev_list; 364 + d = net->can.can_rx_alldev_list; 353 365 can_rcvlist_proc_show_one(m, idx, NULL, d); 354 366 355 367 /* receive list for registered CAN devices */ 356 - for_each_netdev_rcu(&init_net, dev) { 368 + for_each_netdev_rcu(net, dev) { 357 369 if (dev->type == ARPHRD_CAN && dev->ml_priv) 358 370 can_rcvlist_proc_show_one(m, idx, dev, dev->ml_priv); 359 371 } ··· 367 377 368 378 static int can_rcvlist_proc_open(struct inode *inode, struct file *file) 369 379 { 370 - return single_open(file, can_rcvlist_proc_show, PDE_DATA(inode)); 380 + return single_open_net(inode, file, can_rcvlist_proc_show); 371 381 } 372 382 373 383 static const struct file_operations can_rcvlist_proc_fops = { ··· 407 417 { 408 418 struct net_device *dev; 409 419 struct dev_rcv_lists *d; 420 + struct net *net = m->private; 410 421 411 422 /* RX_SFF */ 412 423 seq_puts(m, "\nreceive list 'rx_sff':\n"); ··· 415 424 rcu_read_lock(); 416 425 417 426 /* sff receive list for 'all' CAN devices (dev == NULL) */ 418 - d = &can_rx_alldev_list; 427 + d = net->can.can_rx_alldev_list; 419 428 can_rcvlist_proc_show_array(m, NULL, d->rx_sff, ARRAY_SIZE(d->rx_sff)); 420 429 421 430 /* sff receive list for registered CAN devices */ 422 - for_each_netdev_rcu(&init_net, dev) { 431 + for_each_netdev_rcu(net, dev) { 423 432 if (dev->type == ARPHRD_CAN && dev->ml_priv) { 424 433 d = dev->ml_priv; 425 434 can_rcvlist_proc_show_array(m, dev, d->rx_sff, ··· 435 444 436 445 static int can_rcvlist_sff_proc_open(struct inode *inode, struct file *file) 437 446 { 438 - return single_open(file, can_rcvlist_sff_proc_show, NULL); 447 + return single_open_net(inode, file, can_rcvlist_sff_proc_show); 439 448 } 440 449 441 450 static const struct file_operations can_rcvlist_sff_proc_fops = { ··· 451 460 { 452 461 struct net_device *dev; 453 462 struct dev_rcv_lists *d; 463 + struct net *net = m->private; 454 464 455 465 /* RX_EFF */ 456 466 seq_puts(m, "\nreceive list 'rx_eff':\n"); ··· 459 467 rcu_read_lock(); 460 468 461 469 /* eff receive list for 'all' CAN devices (dev == NULL) */ 462 - d = &can_rx_alldev_list; 470 + d = net->can.can_rx_alldev_list; 463 471 can_rcvlist_proc_show_array(m, NULL, d->rx_eff, ARRAY_SIZE(d->rx_eff)); 464 472 465 473 /* eff receive list for registered CAN devices */ 466 - for_each_netdev_rcu(&init_net, dev) { 474 + for_each_netdev_rcu(net, dev) { 467 475 if (dev->type == ARPHRD_CAN && dev->ml_priv) { 468 476 d = dev->ml_priv; 469 477 can_rcvlist_proc_show_array(m, dev, d->rx_eff, ··· 479 487 480 488 static int can_rcvlist_eff_proc_open(struct inode *inode, struct file *file) 481 489 { 482 - return single_open(file, can_rcvlist_eff_proc_show, NULL); 490 + return single_open_net(inode, file, can_rcvlist_eff_proc_show); 483 491 } 484 492 485 493 static const struct file_operations can_rcvlist_eff_proc_fops = { ··· 491 499 }; 492 500 493 501 /* 494 - * proc utility functions 495 - */ 496 - 497 - static void can_remove_proc_readentry(const char *name) 498 - { 499 - if (can_dir) 500 - remove_proc_entry(name, can_dir); 501 - } 502 - 503 - /* 504 502 * can_init_proc - create main CAN proc directory and procfs entries 505 503 */ 506 - void can_init_proc(void) 504 + void can_init_proc(struct net *net) 507 505 { 508 506 /* create /proc/net/can directory */ 509 - can_dir = proc_mkdir("can", init_net.proc_net); 507 + net->can.proc_dir = proc_net_mkdir(net, "can", net->proc_net); 510 508 511 - if (!can_dir) { 512 - pr_info("can: failed to create /proc/net/can.\n"); 509 + if (!net->can.proc_dir) { 510 + printk(KERN_INFO "can: failed to create /proc/net/can . " 511 + "CONFIG_PROC_FS missing?\n"); 513 512 return; 514 513 } 515 514 516 515 /* own procfs entries from the AF_CAN core */ 517 - pde_version = proc_create(CAN_PROC_VERSION, 0644, can_dir, 518 - &can_version_proc_fops); 519 - pde_stats = proc_create(CAN_PROC_STATS, 0644, can_dir, 520 - &can_stats_proc_fops); 521 - pde_reset_stats = proc_create(CAN_PROC_RESET_STATS, 0644, can_dir, 522 - &can_reset_stats_proc_fops); 523 - pde_rcvlist_err = proc_create_data(CAN_PROC_RCVLIST_ERR, 0644, can_dir, 524 - &can_rcvlist_proc_fops, (void *)RX_ERR); 525 - pde_rcvlist_all = proc_create_data(CAN_PROC_RCVLIST_ALL, 0644, can_dir, 526 - &can_rcvlist_proc_fops, (void *)RX_ALL); 527 - pde_rcvlist_fil = proc_create_data(CAN_PROC_RCVLIST_FIL, 0644, can_dir, 528 - &can_rcvlist_proc_fops, (void *)RX_FIL); 529 - pde_rcvlist_inv = proc_create_data(CAN_PROC_RCVLIST_INV, 0644, can_dir, 530 - &can_rcvlist_proc_fops, (void *)RX_INV); 531 - pde_rcvlist_eff = proc_create(CAN_PROC_RCVLIST_EFF, 0644, can_dir, 532 - &can_rcvlist_eff_proc_fops); 533 - pde_rcvlist_sff = proc_create(CAN_PROC_RCVLIST_SFF, 0644, can_dir, 534 - &can_rcvlist_sff_proc_fops); 516 + net->can.pde_version = proc_create(CAN_PROC_VERSION, 0644, 517 + net->can.proc_dir, 518 + &can_version_proc_fops); 519 + net->can.pde_stats = proc_create(CAN_PROC_STATS, 0644, 520 + net->can.proc_dir, 521 + &can_stats_proc_fops); 522 + net->can.pde_reset_stats = proc_create(CAN_PROC_RESET_STATS, 0644, 523 + net->can.proc_dir, 524 + &can_reset_stats_proc_fops); 525 + net->can.pde_rcvlist_err = proc_create_data(CAN_PROC_RCVLIST_ERR, 0644, 526 + net->can.proc_dir, 527 + &can_rcvlist_proc_fops, 528 + (void *)RX_ERR); 529 + net->can.pde_rcvlist_all = proc_create_data(CAN_PROC_RCVLIST_ALL, 0644, 530 + net->can.proc_dir, 531 + &can_rcvlist_proc_fops, 532 + (void *)RX_ALL); 533 + net->can.pde_rcvlist_fil = proc_create_data(CAN_PROC_RCVLIST_FIL, 0644, 534 + net->can.proc_dir, 535 + &can_rcvlist_proc_fops, 536 + (void *)RX_FIL); 537 + net->can.pde_rcvlist_inv = proc_create_data(CAN_PROC_RCVLIST_INV, 0644, 538 + net->can.proc_dir, 539 + &can_rcvlist_proc_fops, 540 + (void *)RX_INV); 541 + net->can.pde_rcvlist_eff = proc_create(CAN_PROC_RCVLIST_EFF, 0644, 542 + net->can.proc_dir, 543 + &can_rcvlist_eff_proc_fops); 544 + net->can.pde_rcvlist_sff = proc_create(CAN_PROC_RCVLIST_SFF, 0644, 545 + net->can.proc_dir, 546 + &can_rcvlist_sff_proc_fops); 535 547 } 536 548 537 549 /* 538 550 * can_remove_proc - remove procfs entries and main CAN proc directory 539 551 */ 540 - void can_remove_proc(void) 552 + void can_remove_proc(struct net *net) 541 553 { 542 - if (pde_version) 543 - can_remove_proc_readentry(CAN_PROC_VERSION); 554 + if (net->can.pde_version) 555 + remove_proc_entry(CAN_PROC_VERSION, net->can.proc_dir); 544 556 545 - if (pde_stats) 546 - can_remove_proc_readentry(CAN_PROC_STATS); 557 + if (net->can.pde_stats) 558 + remove_proc_entry(CAN_PROC_STATS, net->can.proc_dir); 547 559 548 - if (pde_reset_stats) 549 - can_remove_proc_readentry(CAN_PROC_RESET_STATS); 560 + if (net->can.pde_reset_stats) 561 + remove_proc_entry(CAN_PROC_RESET_STATS, net->can.proc_dir); 550 562 551 - if (pde_rcvlist_err) 552 - can_remove_proc_readentry(CAN_PROC_RCVLIST_ERR); 563 + if (net->can.pde_rcvlist_err) 564 + remove_proc_entry(CAN_PROC_RCVLIST_ERR, net->can.proc_dir); 553 565 554 - if (pde_rcvlist_all) 555 - can_remove_proc_readentry(CAN_PROC_RCVLIST_ALL); 566 + if (net->can.pde_rcvlist_all) 567 + remove_proc_entry(CAN_PROC_RCVLIST_ALL, net->can.proc_dir); 556 568 557 - if (pde_rcvlist_fil) 558 - can_remove_proc_readentry(CAN_PROC_RCVLIST_FIL); 569 + if (net->can.pde_rcvlist_fil) 570 + remove_proc_entry(CAN_PROC_RCVLIST_FIL, net->can.proc_dir); 559 571 560 - if (pde_rcvlist_inv) 561 - can_remove_proc_readentry(CAN_PROC_RCVLIST_INV); 572 + if (net->can.pde_rcvlist_inv) 573 + remove_proc_entry(CAN_PROC_RCVLIST_INV, net->can.proc_dir); 562 574 563 - if (pde_rcvlist_eff) 564 - can_remove_proc_readentry(CAN_PROC_RCVLIST_EFF); 575 + if (net->can.pde_rcvlist_eff) 576 + remove_proc_entry(CAN_PROC_RCVLIST_EFF, net->can.proc_dir); 565 577 566 - if (pde_rcvlist_sff) 567 - can_remove_proc_readentry(CAN_PROC_RCVLIST_SFF); 578 + if (net->can.pde_rcvlist_sff) 579 + remove_proc_entry(CAN_PROC_RCVLIST_SFF, net->can.proc_dir); 568 580 569 - if (can_dir) 570 - remove_proc_entry("can", init_net.proc_net); 581 + if (net->can.proc_dir) 582 + remove_proc_entry("can", net->proc_net); 571 583 }
+52 -40
net/can/raw.c
··· 181 181 kfree_skb(skb); 182 182 } 183 183 184 - static int raw_enable_filters(struct net_device *dev, struct sock *sk, 185 - struct can_filter *filter, int count) 184 + static int raw_enable_filters(struct net *net, struct net_device *dev, 185 + struct sock *sk, struct can_filter *filter, 186 + int count) 186 187 { 187 188 int err = 0; 188 189 int i; 189 190 190 191 for (i = 0; i < count; i++) { 191 - err = can_rx_register(dev, filter[i].can_id, 192 + err = can_rx_register(net, dev, filter[i].can_id, 192 193 filter[i].can_mask, 193 194 raw_rcv, sk, "raw", sk); 194 195 if (err) { 195 196 /* clean up successfully registered filters */ 196 197 while (--i >= 0) 197 - can_rx_unregister(dev, filter[i].can_id, 198 + can_rx_unregister(net, dev, filter[i].can_id, 198 199 filter[i].can_mask, 199 200 raw_rcv, sk); 200 201 break; ··· 205 204 return err; 206 205 } 207 206 208 - static int raw_enable_errfilter(struct net_device *dev, struct sock *sk, 209 - can_err_mask_t err_mask) 207 + static int raw_enable_errfilter(struct net *net, struct net_device *dev, 208 + struct sock *sk, can_err_mask_t err_mask) 210 209 { 211 210 int err = 0; 212 211 213 212 if (err_mask) 214 - err = can_rx_register(dev, 0, err_mask | CAN_ERR_FLAG, 213 + err = can_rx_register(net, dev, 0, err_mask | CAN_ERR_FLAG, 215 214 raw_rcv, sk, "raw", sk); 216 215 217 216 return err; 218 217 } 219 218 220 - static void raw_disable_filters(struct net_device *dev, struct sock *sk, 221 - struct can_filter *filter, int count) 219 + static void raw_disable_filters(struct net *net, struct net_device *dev, 220 + struct sock *sk, struct can_filter *filter, 221 + int count) 222 222 { 223 223 int i; 224 224 225 225 for (i = 0; i < count; i++) 226 - can_rx_unregister(dev, filter[i].can_id, filter[i].can_mask, 227 - raw_rcv, sk); 226 + can_rx_unregister(net, dev, filter[i].can_id, 227 + filter[i].can_mask, raw_rcv, sk); 228 228 } 229 229 230 - static inline void raw_disable_errfilter(struct net_device *dev, 230 + static inline void raw_disable_errfilter(struct net *net, 231 + struct net_device *dev, 231 232 struct sock *sk, 232 233 can_err_mask_t err_mask) 233 234 234 235 { 235 236 if (err_mask) 236 - can_rx_unregister(dev, 0, err_mask | CAN_ERR_FLAG, 237 + can_rx_unregister(net, dev, 0, err_mask | CAN_ERR_FLAG, 237 238 raw_rcv, sk); 238 239 } 239 240 240 - static inline void raw_disable_allfilters(struct net_device *dev, 241 + static inline void raw_disable_allfilters(struct net *net, 242 + struct net_device *dev, 241 243 struct sock *sk) 242 244 { 243 245 struct raw_sock *ro = raw_sk(sk); 244 246 245 - raw_disable_filters(dev, sk, ro->filter, ro->count); 246 - raw_disable_errfilter(dev, sk, ro->err_mask); 247 + raw_disable_filters(net, dev, sk, ro->filter, ro->count); 248 + raw_disable_errfilter(net, dev, sk, ro->err_mask); 247 249 } 248 250 249 - static int raw_enable_allfilters(struct net_device *dev, struct sock *sk) 251 + static int raw_enable_allfilters(struct net *net, struct net_device *dev, 252 + struct sock *sk) 250 253 { 251 254 struct raw_sock *ro = raw_sk(sk); 252 255 int err; 253 256 254 - err = raw_enable_filters(dev, sk, ro->filter, ro->count); 257 + err = raw_enable_filters(net, dev, sk, ro->filter, ro->count); 255 258 if (!err) { 256 - err = raw_enable_errfilter(dev, sk, ro->err_mask); 259 + err = raw_enable_errfilter(net, dev, sk, ro->err_mask); 257 260 if (err) 258 - raw_disable_filters(dev, sk, ro->filter, ro->count); 261 + raw_disable_filters(net, dev, sk, ro->filter, 262 + ro->count); 259 263 } 260 264 261 265 return err; ··· 273 267 struct raw_sock *ro = container_of(nb, struct raw_sock, notifier); 274 268 struct sock *sk = &ro->sk; 275 269 276 - if (!net_eq(dev_net(dev), &init_net)) 270 + if (!net_eq(dev_net(dev), sock_net(sk))) 277 271 return NOTIFY_DONE; 278 272 279 273 if (dev->type != ARPHRD_CAN) ··· 288 282 lock_sock(sk); 289 283 /* remove current filters & unregister */ 290 284 if (ro->bound) 291 - raw_disable_allfilters(dev, sk); 285 + raw_disable_allfilters(dev_net(dev), dev, sk); 292 286 293 287 if (ro->count > 1) 294 288 kfree(ro->filter); ··· 364 358 if (ro->ifindex) { 365 359 struct net_device *dev; 366 360 367 - dev = dev_get_by_index(&init_net, ro->ifindex); 361 + dev = dev_get_by_index(sock_net(sk), ro->ifindex); 368 362 if (dev) { 369 - raw_disable_allfilters(dev, sk); 363 + raw_disable_allfilters(dev_net(dev), dev, sk); 370 364 dev_put(dev); 371 365 } 372 366 } else 373 - raw_disable_allfilters(NULL, sk); 367 + raw_disable_allfilters(sock_net(sk), NULL, sk); 374 368 } 375 369 376 370 if (ro->count > 1) ··· 410 404 if (addr->can_ifindex) { 411 405 struct net_device *dev; 412 406 413 - dev = dev_get_by_index(&init_net, addr->can_ifindex); 407 + dev = dev_get_by_index(sock_net(sk), addr->can_ifindex); 414 408 if (!dev) { 415 409 err = -ENODEV; 416 410 goto out; ··· 426 420 ifindex = dev->ifindex; 427 421 428 422 /* filters set by default/setsockopt */ 429 - err = raw_enable_allfilters(dev, sk); 423 + err = raw_enable_allfilters(sock_net(sk), dev, sk); 430 424 dev_put(dev); 431 425 } else { 432 426 ifindex = 0; 433 427 434 428 /* filters set by default/setsockopt */ 435 - err = raw_enable_allfilters(NULL, sk); 429 + err = raw_enable_allfilters(sock_net(sk), NULL, sk); 436 430 } 437 431 438 432 if (!err) { ··· 441 435 if (ro->ifindex) { 442 436 struct net_device *dev; 443 437 444 - dev = dev_get_by_index(&init_net, ro->ifindex); 438 + dev = dev_get_by_index(sock_net(sk), 439 + ro->ifindex); 445 440 if (dev) { 446 - raw_disable_allfilters(dev, sk); 441 + raw_disable_allfilters(dev_net(dev), 442 + dev, sk); 447 443 dev_put(dev); 448 444 } 449 445 } else 450 - raw_disable_allfilters(NULL, sk); 446 + raw_disable_allfilters(sock_net(sk), NULL, sk); 451 447 } 452 448 ro->ifindex = ifindex; 453 449 ro->bound = 1; ··· 525 517 lock_sock(sk); 526 518 527 519 if (ro->bound && ro->ifindex) 528 - dev = dev_get_by_index(&init_net, ro->ifindex); 520 + dev = dev_get_by_index(sock_net(sk), ro->ifindex); 529 521 530 522 if (ro->bound) { 531 523 /* (try to) register the new filters */ 532 524 if (count == 1) 533 - err = raw_enable_filters(dev, sk, &sfilter, 1); 525 + err = raw_enable_filters(sock_net(sk), dev, sk, 526 + &sfilter, 1); 534 527 else 535 - err = raw_enable_filters(dev, sk, filter, 536 - count); 528 + err = raw_enable_filters(sock_net(sk), dev, sk, 529 + filter, count); 537 530 if (err) { 538 531 if (count > 1) 539 532 kfree(filter); ··· 542 533 } 543 534 544 535 /* remove old filter registrations */ 545 - raw_disable_filters(dev, sk, ro->filter, ro->count); 536 + raw_disable_filters(sock_net(sk), dev, sk, ro->filter, 537 + ro->count); 546 538 } 547 539 548 540 /* remove old filter space */ ··· 579 569 lock_sock(sk); 580 570 581 571 if (ro->bound && ro->ifindex) 582 - dev = dev_get_by_index(&init_net, ro->ifindex); 572 + dev = dev_get_by_index(sock_net(sk), ro->ifindex); 583 573 584 574 /* remove current error mask */ 585 575 if (ro->bound) { 586 576 /* (try to) register the new err_mask */ 587 - err = raw_enable_errfilter(dev, sk, err_mask); 577 + err = raw_enable_errfilter(sock_net(sk), dev, sk, 578 + err_mask); 588 579 589 580 if (err) 590 581 goto out_err; 591 582 592 583 /* remove old err_mask registration */ 593 - raw_disable_errfilter(dev, sk, ro->err_mask); 584 + raw_disable_errfilter(sock_net(sk), dev, sk, 585 + ro->err_mask); 594 586 } 595 587 596 588 /* link new err_mask to the socket */ ··· 753 741 return -EINVAL; 754 742 } 755 743 756 - dev = dev_get_by_index(&init_net, ifindex); 744 + dev = dev_get_by_index(sock_net(sk), ifindex); 757 745 if (!dev) 758 746 return -ENXIO; 759 747