Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Ido Schimmel | c024389 | 2017-03-10 08:56:18 +0100 | [diff] [blame] | 2 | #include <linux/rtnetlink.h> |
| 3 | #include <linux/notifier.h> |
Ido Schimmel | 04b1d4e | 2017-08-03 13:28:11 +0200 | [diff] [blame] | 4 | #include <linux/socket.h> |
Ido Schimmel | c024389 | 2017-03-10 08:56:18 +0100 | [diff] [blame] | 5 | #include <linux/kernel.h> |
Ido Schimmel | 864150d | 2017-09-01 12:15:17 +0300 | [diff] [blame] | 6 | #include <linux/export.h> |
Ido Schimmel | c024389 | 2017-03-10 08:56:18 +0100 | [diff] [blame] | 7 | #include <net/net_namespace.h> |
Ido Schimmel | 04b1d4e | 2017-08-03 13:28:11 +0200 | [diff] [blame] | 8 | #include <net/fib_notifier.h> |
Ido Schimmel | c024389 | 2017-03-10 08:56:18 +0100 | [diff] [blame] | 9 | #include <net/netns/ipv4.h> |
| 10 | #include <net/ip_fib.h> |
| 11 | |
Ido Schimmel | 04b1d4e | 2017-08-03 13:28:11 +0200 | [diff] [blame] | 12 | int call_fib4_notifier(struct notifier_block *nb, struct net *net, |
| 13 | enum fib_event_type event_type, |
Ido Schimmel | c024389 | 2017-03-10 08:56:18 +0100 | [diff] [blame] | 14 | struct fib_notifier_info *info) |
| 15 | { |
Ido Schimmel | 04b1d4e | 2017-08-03 13:28:11 +0200 | [diff] [blame] | 16 | info->family = AF_INET; |
| 17 | return call_fib_notifier(nb, net, event_type, info); |
| 18 | } |
| 19 | |
| 20 | int call_fib4_notifiers(struct net *net, enum fib_event_type event_type, |
| 21 | struct fib_notifier_info *info) |
| 22 | { |
| 23 | ASSERT_RTNL(); |
| 24 | |
| 25 | info->family = AF_INET; |
Ido Schimmel | c024389 | 2017-03-10 08:56:18 +0100 | [diff] [blame] | 26 | net->ipv4.fib_seq++; |
Ido Schimmel | 04b1d4e | 2017-08-03 13:28:11 +0200 | [diff] [blame] | 27 | return call_fib_notifiers(net, event_type, info); |
Ido Schimmel | c024389 | 2017-03-10 08:56:18 +0100 | [diff] [blame] | 28 | } |
| 29 | |
Ido Schimmel | 04b1d4e | 2017-08-03 13:28:11 +0200 | [diff] [blame] | 30 | static unsigned int fib4_seq_read(struct net *net) |
Ido Schimmel | c024389 | 2017-03-10 08:56:18 +0100 | [diff] [blame] | 31 | { |
Ido Schimmel | 04b1d4e | 2017-08-03 13:28:11 +0200 | [diff] [blame] | 32 | ASSERT_RTNL(); |
Ido Schimmel | c024389 | 2017-03-10 08:56:18 +0100 | [diff] [blame] | 33 | |
Ido Schimmel | 1b2a444 | 2017-08-03 13:28:14 +0200 | [diff] [blame] | 34 | return net->ipv4.fib_seq + fib4_rules_seq_read(net); |
Ido Schimmel | c024389 | 2017-03-10 08:56:18 +0100 | [diff] [blame] | 35 | } |
| 36 | |
Ido Schimmel | 04b1d4e | 2017-08-03 13:28:11 +0200 | [diff] [blame] | 37 | static int fib4_dump(struct net *net, struct notifier_block *nb) |
Ido Schimmel | c024389 | 2017-03-10 08:56:18 +0100 | [diff] [blame] | 38 | { |
Ido Schimmel | 1b2a444 | 2017-08-03 13:28:14 +0200 | [diff] [blame] | 39 | int err; |
| 40 | |
| 41 | err = fib4_rules_dump(net, nb); |
| 42 | if (err) |
| 43 | return err; |
| 44 | |
Ido Schimmel | 04b1d4e | 2017-08-03 13:28:11 +0200 | [diff] [blame] | 45 | fib_notify(net, nb); |
| 46 | |
| 47 | return 0; |
Ido Schimmel | c024389 | 2017-03-10 08:56:18 +0100 | [diff] [blame] | 48 | } |
| 49 | |
Ido Schimmel | 04b1d4e | 2017-08-03 13:28:11 +0200 | [diff] [blame] | 50 | static const struct fib_notifier_ops fib4_notifier_ops_template = { |
| 51 | .family = AF_INET, |
| 52 | .fib_seq_read = fib4_seq_read, |
| 53 | .fib_dump = fib4_dump, |
Ido Schimmel | 864150d | 2017-09-01 12:15:17 +0300 | [diff] [blame] | 54 | .owner = THIS_MODULE, |
Ido Schimmel | 04b1d4e | 2017-08-03 13:28:11 +0200 | [diff] [blame] | 55 | }; |
| 56 | |
| 57 | int __net_init fib4_notifier_init(struct net *net) |
Ido Schimmel | c024389 | 2017-03-10 08:56:18 +0100 | [diff] [blame] | 58 | { |
Ido Schimmel | 04b1d4e | 2017-08-03 13:28:11 +0200 | [diff] [blame] | 59 | struct fib_notifier_ops *ops; |
Ido Schimmel | c024389 | 2017-03-10 08:56:18 +0100 | [diff] [blame] | 60 | |
Ido Schimmel | 04b1d4e | 2017-08-03 13:28:11 +0200 | [diff] [blame] | 61 | net->ipv4.fib_seq = 0; |
Ido Schimmel | c024389 | 2017-03-10 08:56:18 +0100 | [diff] [blame] | 62 | |
Ido Schimmel | 04b1d4e | 2017-08-03 13:28:11 +0200 | [diff] [blame] | 63 | ops = fib_notifier_ops_register(&fib4_notifier_ops_template, net); |
| 64 | if (IS_ERR(ops)) |
| 65 | return PTR_ERR(ops); |
| 66 | net->ipv4.notifier_ops = ops; |
Ido Schimmel | c024389 | 2017-03-10 08:56:18 +0100 | [diff] [blame] | 67 | |
Ido Schimmel | 04b1d4e | 2017-08-03 13:28:11 +0200 | [diff] [blame] | 68 | return 0; |
Ido Schimmel | c024389 | 2017-03-10 08:56:18 +0100 | [diff] [blame] | 69 | } |
Ido Schimmel | c024389 | 2017-03-10 08:56:18 +0100 | [diff] [blame] | 70 | |
Ido Schimmel | 04b1d4e | 2017-08-03 13:28:11 +0200 | [diff] [blame] | 71 | void __net_exit fib4_notifier_exit(struct net *net) |
Ido Schimmel | c024389 | 2017-03-10 08:56:18 +0100 | [diff] [blame] | 72 | { |
Ido Schimmel | 04b1d4e | 2017-08-03 13:28:11 +0200 | [diff] [blame] | 73 | fib_notifier_ops_unregister(net->ipv4.notifier_ops); |
Ido Schimmel | c024389 | 2017-03-10 08:56:18 +0100 | [diff] [blame] | 74 | } |