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/ip_fib.h> |
| 10 | |
Jiri Pirko | 7c550da | 2019-10-03 11:49:27 +0200 | [diff] [blame] | 11 | int call_fib4_notifier(struct notifier_block *nb, |
Ido Schimmel | 04b1d4e | 2017-08-03 13:28:11 +0200 | [diff] [blame] | 12 | enum fib_event_type event_type, |
Ido Schimmel | c024389 | 2017-03-10 08:56:18 +0100 | [diff] [blame] | 13 | struct fib_notifier_info *info) |
| 14 | { |
Ido Schimmel | 04b1d4e | 2017-08-03 13:28:11 +0200 | [diff] [blame] | 15 | info->family = AF_INET; |
Jiri Pirko | 7c550da | 2019-10-03 11:49:27 +0200 | [diff] [blame] | 16 | return call_fib_notifier(nb, event_type, info); |
Ido Schimmel | 04b1d4e | 2017-08-03 13:28:11 +0200 | [diff] [blame] | 17 | } |
| 18 | |
| 19 | int call_fib4_notifiers(struct net *net, enum fib_event_type event_type, |
| 20 | struct fib_notifier_info *info) |
| 21 | { |
| 22 | ASSERT_RTNL(); |
| 23 | |
| 24 | info->family = AF_INET; |
Ido Schimmel | c024389 | 2017-03-10 08:56:18 +0100 | [diff] [blame] | 25 | net->ipv4.fib_seq++; |
Ido Schimmel | 04b1d4e | 2017-08-03 13:28:11 +0200 | [diff] [blame] | 26 | return call_fib_notifiers(net, event_type, info); |
Ido Schimmel | c024389 | 2017-03-10 08:56:18 +0100 | [diff] [blame] | 27 | } |
| 28 | |
Ido Schimmel | 04b1d4e | 2017-08-03 13:28:11 +0200 | [diff] [blame] | 29 | static unsigned int fib4_seq_read(struct net *net) |
Ido Schimmel | c024389 | 2017-03-10 08:56:18 +0100 | [diff] [blame] | 30 | { |
Ido Schimmel | 04b1d4e | 2017-08-03 13:28:11 +0200 | [diff] [blame] | 31 | ASSERT_RTNL(); |
Ido Schimmel | c024389 | 2017-03-10 08:56:18 +0100 | [diff] [blame] | 32 | |
Ido Schimmel | 1b2a444 | 2017-08-03 13:28:14 +0200 | [diff] [blame] | 33 | return net->ipv4.fib_seq + fib4_rules_seq_read(net); |
Ido Schimmel | c024389 | 2017-03-10 08:56:18 +0100 | [diff] [blame] | 34 | } |
| 35 | |
Jiri Pirko | b7a5955 | 2019-10-03 11:49:30 +0200 | [diff] [blame] | 36 | static int fib4_dump(struct net *net, struct notifier_block *nb, |
| 37 | struct netlink_ext_ack *extack) |
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 | |
Jiri Pirko | b7a5955 | 2019-10-03 11:49:30 +0200 | [diff] [blame] | 41 | err = fib4_rules_dump(net, nb, extack); |
Ido Schimmel | 1b2a444 | 2017-08-03 13:28:14 +0200 | [diff] [blame] | 42 | if (err) |
| 43 | return err; |
| 44 | |
Jiri Pirko | b7a5955 | 2019-10-03 11:49:30 +0200 | [diff] [blame] | 45 | return fib_notify(net, nb, extack); |
Ido Schimmel | c024389 | 2017-03-10 08:56:18 +0100 | [diff] [blame] | 46 | } |
| 47 | |
Ido Schimmel | 04b1d4e | 2017-08-03 13:28:11 +0200 | [diff] [blame] | 48 | static const struct fib_notifier_ops fib4_notifier_ops_template = { |
| 49 | .family = AF_INET, |
| 50 | .fib_seq_read = fib4_seq_read, |
| 51 | .fib_dump = fib4_dump, |
Ido Schimmel | 864150d | 2017-09-01 12:15:17 +0300 | [diff] [blame] | 52 | .owner = THIS_MODULE, |
Ido Schimmel | 04b1d4e | 2017-08-03 13:28:11 +0200 | [diff] [blame] | 53 | }; |
| 54 | |
| 55 | int __net_init fib4_notifier_init(struct net *net) |
Ido Schimmel | c024389 | 2017-03-10 08:56:18 +0100 | [diff] [blame] | 56 | { |
Ido Schimmel | 04b1d4e | 2017-08-03 13:28:11 +0200 | [diff] [blame] | 57 | struct fib_notifier_ops *ops; |
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 | net->ipv4.fib_seq = 0; |
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 | ops = fib_notifier_ops_register(&fib4_notifier_ops_template, net); |
| 62 | if (IS_ERR(ops)) |
| 63 | return PTR_ERR(ops); |
| 64 | net->ipv4.notifier_ops = ops; |
Ido Schimmel | c024389 | 2017-03-10 08:56:18 +0100 | [diff] [blame] | 65 | |
Ido Schimmel | 04b1d4e | 2017-08-03 13:28:11 +0200 | [diff] [blame] | 66 | return 0; |
Ido Schimmel | c024389 | 2017-03-10 08:56:18 +0100 | [diff] [blame] | 67 | } |
Ido Schimmel | c024389 | 2017-03-10 08:56:18 +0100 | [diff] [blame] | 68 | |
Ido Schimmel | 04b1d4e | 2017-08-03 13:28:11 +0200 | [diff] [blame] | 69 | void __net_exit fib4_notifier_exit(struct net *net) |
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 | fib_notifier_ops_unregister(net->ipv4.notifier_ops); |
Ido Schimmel | c024389 | 2017-03-10 08:56:18 +0100 | [diff] [blame] | 72 | } |