blob: 0e23ade74493ce10a3f2572c39e091f132684884 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Ido Schimmelc0243892017-03-10 08:56:18 +01002#include <linux/rtnetlink.h>
3#include <linux/notifier.h>
Ido Schimmel04b1d4e2017-08-03 13:28:11 +02004#include <linux/socket.h>
Ido Schimmelc0243892017-03-10 08:56:18 +01005#include <linux/kernel.h>
Ido Schimmel864150d2017-09-01 12:15:17 +03006#include <linux/export.h>
Ido Schimmelc0243892017-03-10 08:56:18 +01007#include <net/net_namespace.h>
Ido Schimmel04b1d4e2017-08-03 13:28:11 +02008#include <net/fib_notifier.h>
Ido Schimmelc0243892017-03-10 08:56:18 +01009#include <net/ip_fib.h>
10
Jiri Pirko7c550da2019-10-03 11:49:27 +020011int call_fib4_notifier(struct notifier_block *nb,
Ido Schimmel04b1d4e2017-08-03 13:28:11 +020012 enum fib_event_type event_type,
Ido Schimmelc0243892017-03-10 08:56:18 +010013 struct fib_notifier_info *info)
14{
Ido Schimmel04b1d4e2017-08-03 13:28:11 +020015 info->family = AF_INET;
Jiri Pirko7c550da2019-10-03 11:49:27 +020016 return call_fib_notifier(nb, event_type, info);
Ido Schimmel04b1d4e2017-08-03 13:28:11 +020017}
18
19int 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 Schimmelc0243892017-03-10 08:56:18 +010025 net->ipv4.fib_seq++;
Ido Schimmel04b1d4e2017-08-03 13:28:11 +020026 return call_fib_notifiers(net, event_type, info);
Ido Schimmelc0243892017-03-10 08:56:18 +010027}
28
Ido Schimmel04b1d4e2017-08-03 13:28:11 +020029static unsigned int fib4_seq_read(struct net *net)
Ido Schimmelc0243892017-03-10 08:56:18 +010030{
Ido Schimmel04b1d4e2017-08-03 13:28:11 +020031 ASSERT_RTNL();
Ido Schimmelc0243892017-03-10 08:56:18 +010032
Ido Schimmel1b2a4442017-08-03 13:28:14 +020033 return net->ipv4.fib_seq + fib4_rules_seq_read(net);
Ido Schimmelc0243892017-03-10 08:56:18 +010034}
35
Jiri Pirkob7a59552019-10-03 11:49:30 +020036static int fib4_dump(struct net *net, struct notifier_block *nb,
37 struct netlink_ext_ack *extack)
Ido Schimmelc0243892017-03-10 08:56:18 +010038{
Ido Schimmel1b2a4442017-08-03 13:28:14 +020039 int err;
40
Jiri Pirkob7a59552019-10-03 11:49:30 +020041 err = fib4_rules_dump(net, nb, extack);
Ido Schimmel1b2a4442017-08-03 13:28:14 +020042 if (err)
43 return err;
44
Jiri Pirkob7a59552019-10-03 11:49:30 +020045 return fib_notify(net, nb, extack);
Ido Schimmelc0243892017-03-10 08:56:18 +010046}
47
Ido Schimmel04b1d4e2017-08-03 13:28:11 +020048static 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 Schimmel864150d2017-09-01 12:15:17 +030052 .owner = THIS_MODULE,
Ido Schimmel04b1d4e2017-08-03 13:28:11 +020053};
54
55int __net_init fib4_notifier_init(struct net *net)
Ido Schimmelc0243892017-03-10 08:56:18 +010056{
Ido Schimmel04b1d4e2017-08-03 13:28:11 +020057 struct fib_notifier_ops *ops;
Ido Schimmelc0243892017-03-10 08:56:18 +010058
Ido Schimmel04b1d4e2017-08-03 13:28:11 +020059 net->ipv4.fib_seq = 0;
Ido Schimmelc0243892017-03-10 08:56:18 +010060
Ido Schimmel04b1d4e2017-08-03 13:28:11 +020061 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 Schimmelc0243892017-03-10 08:56:18 +010065
Ido Schimmel04b1d4e2017-08-03 13:28:11 +020066 return 0;
Ido Schimmelc0243892017-03-10 08:56:18 +010067}
Ido Schimmelc0243892017-03-10 08:56:18 +010068
Ido Schimmel04b1d4e2017-08-03 13:28:11 +020069void __net_exit fib4_notifier_exit(struct net *net)
Ido Schimmelc0243892017-03-10 08:56:18 +010070{
Ido Schimmel04b1d4e2017-08-03 13:28:11 +020071 fib_notifier_ops_unregister(net->ipv4.notifier_ops);
Ido Schimmelc0243892017-03-10 08:56:18 +010072}