1 /* SPDX-License-Identifier: GPL-2.0-or-later */
3 * include/net/l3mdev.h - L3 master device API
4 * Copyright (c) 2015 Cumulus Networks
5 * Copyright (c) 2015 David Ahern <dsa@cumulusnetworks.com>
11 #include <net/fib_rules.h>
14 * struct l3mdev_ops - l3mdev operations
16 * @l3mdev_fib_table: Get FIB table id to use for lookups
18 * @l3mdev_l3_rcv: Hook in L3 receive path
20 * @l3mdev_l3_out: Hook in L3 output path
22 * @l3mdev_link_scope_lookup: IPv6 lookup for linklocal and mcast destinations
26 u32 (*l3mdev_fib_table)(const struct net_device *dev);
27 struct sk_buff * (*l3mdev_l3_rcv)(struct net_device *dev,
28 struct sk_buff *skb, u16 proto);
29 struct sk_buff * (*l3mdev_l3_out)(struct net_device *dev,
30 struct sock *sk, struct sk_buff *skb,
34 struct dst_entry * (*l3mdev_link_scope_lookup)(const struct net_device *dev,
38 #ifdef CONFIG_NET_L3_MASTER_DEV
40 int l3mdev_fib_rule_match(struct net *net, struct flowi *fl,
41 struct fib_lookup_arg *arg);
43 void l3mdev_update_flow(struct net *net, struct flowi *fl);
45 int l3mdev_master_ifindex_rcu(const struct net_device *dev);
46 static inline int l3mdev_master_ifindex(struct net_device *dev)
51 ifindex = l3mdev_master_ifindex_rcu(dev);
57 static inline int l3mdev_master_ifindex_by_index(struct net *net, int ifindex)
59 struct net_device *dev;
62 if (likely(ifindex)) {
65 dev = dev_get_by_index_rcu(net, ifindex);
67 rc = l3mdev_master_ifindex_rcu(dev);
76 struct net_device *l3mdev_master_dev_rcu(const struct net_device *_dev)
78 /* netdev_master_upper_dev_get_rcu calls
79 * list_first_or_null_rcu to walk the upper dev list.
80 * list_first_or_null_rcu does not handle a const arg. We aren't
81 * making changes, just want the master device from that list so
82 * typecast to remove the const
84 struct net_device *dev = (struct net_device *)_dev;
85 struct net_device *master;
90 if (netif_is_l3_master(dev))
92 else if (netif_is_l3_slave(dev))
93 master = netdev_master_upper_dev_get_rcu(dev);
100 int l3mdev_master_upper_ifindex_by_index_rcu(struct net *net, int ifindex);
102 int l3mdev_master_upper_ifindex_by_index(struct net *net, int ifindex)
105 ifindex = l3mdev_master_upper_ifindex_by_index_rcu(net, ifindex);
111 u32 l3mdev_fib_table_rcu(const struct net_device *dev);
112 u32 l3mdev_fib_table_by_index(struct net *net, int ifindex);
113 static inline u32 l3mdev_fib_table(const struct net_device *dev)
118 tb_id = l3mdev_fib_table_rcu(dev);
124 static inline bool netif_index_is_l3_master(struct net *net, int ifindex)
126 struct net_device *dev;
134 dev = dev_get_by_index_rcu(net, ifindex);
136 rc = netif_is_l3_master(dev);
143 struct dst_entry *l3mdev_link_scope_lookup(struct net *net, struct flowi6 *fl6);
146 struct sk_buff *l3mdev_l3_rcv(struct sk_buff *skb, u16 proto)
148 struct net_device *master = NULL;
150 if (netif_is_l3_slave(skb->dev))
151 master = netdev_master_upper_dev_get_rcu(skb->dev);
152 else if (netif_is_l3_master(skb->dev) ||
153 netif_has_l3_rx_handler(skb->dev))
156 if (master && master->l3mdev_ops->l3mdev_l3_rcv)
157 skb = master->l3mdev_ops->l3mdev_l3_rcv(master, skb, proto);
163 struct sk_buff *l3mdev_ip_rcv(struct sk_buff *skb)
165 return l3mdev_l3_rcv(skb, AF_INET);
169 struct sk_buff *l3mdev_ip6_rcv(struct sk_buff *skb)
171 return l3mdev_l3_rcv(skb, AF_INET6);
175 struct sk_buff *l3mdev_l3_out(struct sock *sk, struct sk_buff *skb, u16 proto)
177 struct net_device *dev = skb_dst(skb)->dev;
179 if (netif_is_l3_slave(dev)) {
180 struct net_device *master;
182 master = netdev_master_upper_dev_get_rcu(dev);
183 if (master && master->l3mdev_ops->l3mdev_l3_out)
184 skb = master->l3mdev_ops->l3mdev_l3_out(master, sk,
192 struct sk_buff *l3mdev_ip_out(struct sock *sk, struct sk_buff *skb)
194 return l3mdev_l3_out(sk, skb, AF_INET);
198 struct sk_buff *l3mdev_ip6_out(struct sock *sk, struct sk_buff *skb)
200 return l3mdev_l3_out(sk, skb, AF_INET6);
204 static inline int l3mdev_master_ifindex_rcu(const struct net_device *dev)
208 static inline int l3mdev_master_ifindex(struct net_device *dev)
213 static inline int l3mdev_master_ifindex_by_index(struct net *net, int ifindex)
219 int l3mdev_master_upper_ifindex_by_index_rcu(struct net *net, int ifindex)
224 int l3mdev_master_upper_ifindex_by_index(struct net *net, int ifindex)
230 struct net_device *l3mdev_master_dev_rcu(const struct net_device *dev)
235 static inline u32 l3mdev_fib_table_rcu(const struct net_device *dev)
239 static inline u32 l3mdev_fib_table(const struct net_device *dev)
243 static inline u32 l3mdev_fib_table_by_index(struct net *net, int ifindex)
248 static inline bool netif_index_is_l3_master(struct net *net, int ifindex)
254 struct dst_entry *l3mdev_link_scope_lookup(struct net *net, struct flowi6 *fl6)
260 struct sk_buff *l3mdev_ip_rcv(struct sk_buff *skb)
266 struct sk_buff *l3mdev_ip6_rcv(struct sk_buff *skb)
272 struct sk_buff *l3mdev_ip_out(struct sock *sk, struct sk_buff *skb)
278 struct sk_buff *l3mdev_ip6_out(struct sock *sk, struct sk_buff *skb)
284 int l3mdev_fib_rule_match(struct net *net, struct flowi *fl,
285 struct fib_lookup_arg *arg)
290 void l3mdev_update_flow(struct net *net, struct flowi *fl)
295 #endif /* _NET_L3MDEV_H_ */