2 * net/l3mdev/l3mdev.c - L3 master device implementation
3 * Copyright (c) 2015 Cumulus Networks
4 * Copyright (c) 2015 David Ahern <dsa@cumulusnetworks.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
12 #include <linux/netdevice.h>
13 #include <net/fib_rules.h>
14 #include <net/l3mdev.h>
17 * l3mdev_master_ifindex - get index of L3 master device
18 * @dev: targeted interface
21 int l3mdev_master_ifindex_rcu(const struct net_device
*dev
)
28 if (netif_is_l3_master(dev
)) {
29 ifindex
= dev
->ifindex
;
30 } else if (netif_is_l3_slave(dev
)) {
31 struct net_device
*master
;
32 struct net_device
*_dev
= (struct net_device
*)dev
;
34 /* netdev_master_upper_dev_get_rcu calls
35 * list_first_or_null_rcu to walk the upper dev list.
36 * list_first_or_null_rcu does not handle a const arg. We aren't
37 * making changes, just want the master device from that list so
38 * typecast to remove the const
40 master
= netdev_master_upper_dev_get_rcu(_dev
);
42 ifindex
= master
->ifindex
;
47 EXPORT_SYMBOL_GPL(l3mdev_master_ifindex_rcu
);
50 * l3mdev_fib_table - get FIB table id associated with an L3
52 * @dev: targeted interface
55 u32
l3mdev_fib_table_rcu(const struct net_device
*dev
)
62 if (netif_is_l3_master(dev
)) {
63 if (dev
->l3mdev_ops
->l3mdev_fib_table
)
64 tb_id
= dev
->l3mdev_ops
->l3mdev_fib_table(dev
);
65 } else if (netif_is_l3_slave(dev
)) {
66 /* Users of netdev_master_upper_dev_get_rcu need non-const,
67 * but current inet_*type functions take a const
69 struct net_device
*_dev
= (struct net_device
*) dev
;
70 const struct net_device
*master
;
72 master
= netdev_master_upper_dev_get_rcu(_dev
);
74 master
->l3mdev_ops
->l3mdev_fib_table
)
75 tb_id
= master
->l3mdev_ops
->l3mdev_fib_table(master
);
80 EXPORT_SYMBOL_GPL(l3mdev_fib_table_rcu
);
82 u32
l3mdev_fib_table_by_index(struct net
*net
, int ifindex
)
84 struct net_device
*dev
;
92 dev
= dev_get_by_index_rcu(net
, ifindex
);
94 tb_id
= l3mdev_fib_table_rcu(dev
);
100 EXPORT_SYMBOL_GPL(l3mdev_fib_table_by_index
);
103 * l3mdev_link_scope_lookup - IPv6 route lookup based on flow for link
104 * local and multicast addresses
105 * @net: network namespace for device index lookup
106 * @fl6: IPv6 flow struct for lookup
109 struct dst_entry
*l3mdev_link_scope_lookup(struct net
*net
,
112 struct dst_entry
*dst
= NULL
;
113 struct net_device
*dev
;
115 if (fl6
->flowi6_oif
) {
118 dev
= dev_get_by_index_rcu(net
, fl6
->flowi6_oif
);
119 if (dev
&& netif_is_l3_slave(dev
))
120 dev
= netdev_master_upper_dev_get_rcu(dev
);
122 if (dev
&& netif_is_l3_master(dev
) &&
123 dev
->l3mdev_ops
->l3mdev_link_scope_lookup
)
124 dst
= dev
->l3mdev_ops
->l3mdev_link_scope_lookup(dev
, fl6
);
131 EXPORT_SYMBOL_GPL(l3mdev_link_scope_lookup
);
134 * l3mdev_fib_rule_match - Determine if flowi references an
136 * @net: network namespace for device index lookup
140 int l3mdev_fib_rule_match(struct net
*net
, struct flowi
*fl
,
141 struct fib_lookup_arg
*arg
)
143 struct net_device
*dev
;
148 dev
= dev_get_by_index_rcu(net
, fl
->flowi_oif
);
149 if (dev
&& netif_is_l3_master(dev
) &&
150 dev
->l3mdev_ops
->l3mdev_fib_table
) {
151 arg
->table
= dev
->l3mdev_ops
->l3mdev_fib_table(dev
);
156 dev
= dev_get_by_index_rcu(net
, fl
->flowi_iif
);
157 if (dev
&& netif_is_l3_master(dev
) &&
158 dev
->l3mdev_ops
->l3mdev_fib_table
) {
159 arg
->table
= dev
->l3mdev_ops
->l3mdev_fib_table(dev
);
170 void l3mdev_update_flow(struct net
*net
, struct flowi
*fl
)
172 struct net_device
*dev
;
178 dev
= dev_get_by_index_rcu(net
, fl
->flowi_oif
);
180 ifindex
= l3mdev_master_ifindex_rcu(dev
);
182 fl
->flowi_oif
= ifindex
;
183 fl
->flowi_flags
|= FLOWI_FLAG_SKIP_NH_OIF
;
190 dev
= dev_get_by_index_rcu(net
, fl
->flowi_iif
);
192 ifindex
= l3mdev_master_ifindex_rcu(dev
);
194 fl
->flowi_iif
= ifindex
;
195 fl
->flowi_flags
|= FLOWI_FLAG_SKIP_NH_OIF
;
203 EXPORT_SYMBOL_GPL(l3mdev_update_flow
);