blob: 0c28bd469a6810007ef66c6c224c6314aed56a6c [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/netns/ipv4.h>
10#include <net/ip_fib.h>
11
Jiri Pirko7c550da2019-10-03 11:49:27 +020012int call_fib4_notifier(struct notifier_block *nb,
Ido Schimmel04b1d4e2017-08-03 13:28:11 +020013 enum fib_event_type event_type,
Ido Schimmelc0243892017-03-10 08:56:18 +010014 struct fib_notifier_info *info)
15{
Ido Schimmel04b1d4e2017-08-03 13:28:11 +020016 info->family = AF_INET;
Jiri Pirko7c550da2019-10-03 11:49:27 +020017 return call_fib_notifier(nb, event_type, info);
Ido Schimmel04b1d4e2017-08-03 13:28:11 +020018}
19
20int 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 Schimmelc0243892017-03-10 08:56:18 +010026 net->ipv4.fib_seq++;
Ido Schimmel04b1d4e2017-08-03 13:28:11 +020027 return call_fib_notifiers(net, event_type, info);
Ido Schimmelc0243892017-03-10 08:56:18 +010028}
29
Ido Schimmel04b1d4e2017-08-03 13:28:11 +020030static unsigned int fib4_seq_read(struct net *net)
Ido Schimmelc0243892017-03-10 08:56:18 +010031{
Ido Schimmel04b1d4e2017-08-03 13:28:11 +020032 ASSERT_RTNL();
Ido Schimmelc0243892017-03-10 08:56:18 +010033
Ido Schimmel1b2a4442017-08-03 13:28:14 +020034 return net->ipv4.fib_seq + fib4_rules_seq_read(net);
Ido Schimmelc0243892017-03-10 08:56:18 +010035}
36
Jiri Pirkob7a59552019-10-03 11:49:30 +020037static int fib4_dump(struct net *net, struct notifier_block *nb,
38 struct netlink_ext_ack *extack)
Ido Schimmelc0243892017-03-10 08:56:18 +010039{
Ido Schimmel1b2a4442017-08-03 13:28:14 +020040 int err;
41
Jiri Pirkob7a59552019-10-03 11:49:30 +020042 err = fib4_rules_dump(net, nb, extack);
Ido Schimmel1b2a4442017-08-03 13:28:14 +020043 if (err)
44 return err;
45
Jiri Pirkob7a59552019-10-03 11:49:30 +020046 return fib_notify(net, nb, extack);
Ido Schimmelc0243892017-03-10 08:56:18 +010047}
48
Ido Schimmel04b1d4e2017-08-03 13:28:11 +020049static const struct fib_notifier_ops fib4_notifier_ops_template = {
50 .family = AF_INET,
51 .fib_seq_read = fib4_seq_read,
52 .fib_dump = fib4_dump,
Ido Schimmel864150d2017-09-01 12:15:17 +030053 .owner = THIS_MODULE,
Ido Schimmel04b1d4e2017-08-03 13:28:11 +020054};
55
56int __net_init fib4_notifier_init(struct net *net)
Ido Schimmelc0243892017-03-10 08:56:18 +010057{
Ido Schimmel04b1d4e2017-08-03 13:28:11 +020058 struct fib_notifier_ops *ops;
Ido Schimmelc0243892017-03-10 08:56:18 +010059
Ido Schimmel04b1d4e2017-08-03 13:28:11 +020060 net->ipv4.fib_seq = 0;
Ido Schimmelc0243892017-03-10 08:56:18 +010061
Ido Schimmel04b1d4e2017-08-03 13:28:11 +020062 ops = fib_notifier_ops_register(&fib4_notifier_ops_template, net);
63 if (IS_ERR(ops))
64 return PTR_ERR(ops);
65 net->ipv4.notifier_ops = ops;
Ido Schimmelc0243892017-03-10 08:56:18 +010066
Ido Schimmel04b1d4e2017-08-03 13:28:11 +020067 return 0;
Ido Schimmelc0243892017-03-10 08:56:18 +010068}
Ido Schimmelc0243892017-03-10 08:56:18 +010069
Ido Schimmel04b1d4e2017-08-03 13:28:11 +020070void __net_exit fib4_notifier_exit(struct net *net)
Ido Schimmelc0243892017-03-10 08:56:18 +010071{
Ido Schimmel04b1d4e2017-08-03 13:28:11 +020072 fib_notifier_ops_unregister(net->ipv4.notifier_ops);
Ido Schimmelc0243892017-03-10 08:56:18 +010073}