staging: erofs: integrate decompression inplace
[linux/fpc-iii.git] / net / l3mdev / l3mdev.c
blobcfc9fcb97465bcee2db27fc12359d414984c6b54
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * net/l3mdev/l3mdev.c - L3 master device implementation
4 * Copyright (c) 2015 Cumulus Networks
5 * Copyright (c) 2015 David Ahern <dsa@cumulusnetworks.com>
6 */
8 #include <linux/netdevice.h>
9 #include <net/fib_rules.h>
10 #include <net/l3mdev.h>
12 /**
13 * l3mdev_master_ifindex - get index of L3 master device
14 * @dev: targeted interface
17 int l3mdev_master_ifindex_rcu(const struct net_device *dev)
19 int ifindex = 0;
21 if (!dev)
22 return 0;
24 if (netif_is_l3_master(dev)) {
25 ifindex = dev->ifindex;
26 } else if (netif_is_l3_slave(dev)) {
27 struct net_device *master;
28 struct net_device *_dev = (struct net_device *)dev;
30 /* netdev_master_upper_dev_get_rcu calls
31 * list_first_or_null_rcu to walk the upper dev list.
32 * list_first_or_null_rcu does not handle a const arg. We aren't
33 * making changes, just want the master device from that list so
34 * typecast to remove the const
36 master = netdev_master_upper_dev_get_rcu(_dev);
37 if (master)
38 ifindex = master->ifindex;
41 return ifindex;
43 EXPORT_SYMBOL_GPL(l3mdev_master_ifindex_rcu);
45 /**
46 * l3mdev_master_upper_ifindex_by_index - get index of upper l3 master
47 * device
48 * @net: network namespace for device index lookup
49 * @ifindex: targeted interface
51 int l3mdev_master_upper_ifindex_by_index_rcu(struct net *net, int ifindex)
53 struct net_device *dev;
55 dev = dev_get_by_index_rcu(net, ifindex);
56 while (dev && !netif_is_l3_master(dev))
57 dev = netdev_master_upper_dev_get(dev);
59 return dev ? dev->ifindex : 0;
61 EXPORT_SYMBOL_GPL(l3mdev_master_upper_ifindex_by_index_rcu);
63 /**
64 * l3mdev_fib_table - get FIB table id associated with an L3
65 * master interface
66 * @dev: targeted interface
69 u32 l3mdev_fib_table_rcu(const struct net_device *dev)
71 u32 tb_id = 0;
73 if (!dev)
74 return 0;
76 if (netif_is_l3_master(dev)) {
77 if (dev->l3mdev_ops->l3mdev_fib_table)
78 tb_id = dev->l3mdev_ops->l3mdev_fib_table(dev);
79 } else if (netif_is_l3_slave(dev)) {
80 /* Users of netdev_master_upper_dev_get_rcu need non-const,
81 * but current inet_*type functions take a const
83 struct net_device *_dev = (struct net_device *) dev;
84 const struct net_device *master;
86 master = netdev_master_upper_dev_get_rcu(_dev);
87 if (master &&
88 master->l3mdev_ops->l3mdev_fib_table)
89 tb_id = master->l3mdev_ops->l3mdev_fib_table(master);
92 return tb_id;
94 EXPORT_SYMBOL_GPL(l3mdev_fib_table_rcu);
96 u32 l3mdev_fib_table_by_index(struct net *net, int ifindex)
98 struct net_device *dev;
99 u32 tb_id = 0;
101 if (!ifindex)
102 return 0;
104 rcu_read_lock();
106 dev = dev_get_by_index_rcu(net, ifindex);
107 if (dev)
108 tb_id = l3mdev_fib_table_rcu(dev);
110 rcu_read_unlock();
112 return tb_id;
114 EXPORT_SYMBOL_GPL(l3mdev_fib_table_by_index);
117 * l3mdev_link_scope_lookup - IPv6 route lookup based on flow for link
118 * local and multicast addresses
119 * @net: network namespace for device index lookup
120 * @fl6: IPv6 flow struct for lookup
123 struct dst_entry *l3mdev_link_scope_lookup(struct net *net,
124 struct flowi6 *fl6)
126 struct dst_entry *dst = NULL;
127 struct net_device *dev;
129 if (fl6->flowi6_oif) {
130 rcu_read_lock();
132 dev = dev_get_by_index_rcu(net, fl6->flowi6_oif);
133 if (dev && netif_is_l3_slave(dev))
134 dev = netdev_master_upper_dev_get_rcu(dev);
136 if (dev && netif_is_l3_master(dev) &&
137 dev->l3mdev_ops->l3mdev_link_scope_lookup)
138 dst = dev->l3mdev_ops->l3mdev_link_scope_lookup(dev, fl6);
140 rcu_read_unlock();
143 return dst;
145 EXPORT_SYMBOL_GPL(l3mdev_link_scope_lookup);
148 * l3mdev_fib_rule_match - Determine if flowi references an
149 * L3 master device
150 * @net: network namespace for device index lookup
151 * @fl: flow struct
154 int l3mdev_fib_rule_match(struct net *net, struct flowi *fl,
155 struct fib_lookup_arg *arg)
157 struct net_device *dev;
158 int rc = 0;
160 rcu_read_lock();
162 dev = dev_get_by_index_rcu(net, fl->flowi_oif);
163 if (dev && netif_is_l3_master(dev) &&
164 dev->l3mdev_ops->l3mdev_fib_table) {
165 arg->table = dev->l3mdev_ops->l3mdev_fib_table(dev);
166 rc = 1;
167 goto out;
170 dev = dev_get_by_index_rcu(net, fl->flowi_iif);
171 if (dev && netif_is_l3_master(dev) &&
172 dev->l3mdev_ops->l3mdev_fib_table) {
173 arg->table = dev->l3mdev_ops->l3mdev_fib_table(dev);
174 rc = 1;
175 goto out;
178 out:
179 rcu_read_unlock();
181 return rc;
184 void l3mdev_update_flow(struct net *net, struct flowi *fl)
186 struct net_device *dev;
187 int ifindex;
189 rcu_read_lock();
191 if (fl->flowi_oif) {
192 dev = dev_get_by_index_rcu(net, fl->flowi_oif);
193 if (dev) {
194 ifindex = l3mdev_master_ifindex_rcu(dev);
195 if (ifindex) {
196 fl->flowi_oif = ifindex;
197 fl->flowi_flags |= FLOWI_FLAG_SKIP_NH_OIF;
198 goto out;
203 if (fl->flowi_iif) {
204 dev = dev_get_by_index_rcu(net, fl->flowi_iif);
205 if (dev) {
206 ifindex = l3mdev_master_ifindex_rcu(dev);
207 if (ifindex) {
208 fl->flowi_iif = ifindex;
209 fl->flowi_flags |= FLOWI_FLAG_SKIP_NH_OIF;
214 out:
215 rcu_read_unlock();
217 EXPORT_SYMBOL_GPL(l3mdev_update_flow);