David Ahern | 1b69c6d | 2015-09-29 20:07:11 -0700 | [diff] [blame] | 1 | /* |
| 2 | * net/l3mdev/l3mdev.c - L3 master device implementation |
| 3 | * Copyright (c) 2015 Cumulus Networks |
| 4 | * Copyright (c) 2015 David Ahern <dsa@cumulusnetworks.com> |
| 5 | * |
| 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. |
| 10 | */ |
| 11 | |
| 12 | #include <linux/netdevice.h> |
David Ahern | 96c63fa | 2016-06-08 10:55:39 -0700 | [diff] [blame] | 13 | #include <net/fib_rules.h> |
David Ahern | 1b69c6d | 2015-09-29 20:07:11 -0700 | [diff] [blame] | 14 | #include <net/l3mdev.h> |
| 15 | |
| 16 | /** |
| 17 | * l3mdev_master_ifindex - get index of L3 master device |
| 18 | * @dev: targeted interface |
| 19 | */ |
| 20 | |
David Ahern | 3f2fb9a | 2016-02-24 11:47:02 -0800 | [diff] [blame] | 21 | int l3mdev_master_ifindex_rcu(const struct net_device *dev) |
David Ahern | 1b69c6d | 2015-09-29 20:07:11 -0700 | [diff] [blame] | 22 | { |
| 23 | int ifindex = 0; |
| 24 | |
| 25 | if (!dev) |
| 26 | return 0; |
| 27 | |
| 28 | if (netif_is_l3_master(dev)) { |
| 29 | ifindex = dev->ifindex; |
David Ahern | fee6d4c | 2015-10-05 08:51:24 -0700 | [diff] [blame] | 30 | } else if (netif_is_l3_slave(dev)) { |
David Ahern | 1b69c6d | 2015-09-29 20:07:11 -0700 | [diff] [blame] | 31 | struct net_device *master; |
David Ahern | 3f2fb9a | 2016-02-24 11:47:02 -0800 | [diff] [blame] | 32 | struct net_device *_dev = (struct net_device *)dev; |
David Ahern | 1b69c6d | 2015-09-29 20:07:11 -0700 | [diff] [blame] | 33 | |
David Ahern | 3f2fb9a | 2016-02-24 11:47:02 -0800 | [diff] [blame] | 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 |
| 39 | */ |
| 40 | master = netdev_master_upper_dev_get_rcu(_dev); |
David Ahern | fee6d4c | 2015-10-05 08:51:24 -0700 | [diff] [blame] | 41 | if (master) |
David Ahern | 1b69c6d | 2015-09-29 20:07:11 -0700 | [diff] [blame] | 42 | ifindex = master->ifindex; |
| 43 | } |
| 44 | |
| 45 | return ifindex; |
| 46 | } |
| 47 | EXPORT_SYMBOL_GPL(l3mdev_master_ifindex_rcu); |
| 48 | |
| 49 | /** |
| 50 | * l3mdev_fib_table - get FIB table id associated with an L3 |
| 51 | * master interface |
| 52 | * @dev: targeted interface |
| 53 | */ |
| 54 | |
| 55 | u32 l3mdev_fib_table_rcu(const struct net_device *dev) |
| 56 | { |
| 57 | u32 tb_id = 0; |
| 58 | |
| 59 | if (!dev) |
| 60 | return 0; |
| 61 | |
| 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); |
David Ahern | fee6d4c | 2015-10-05 08:51:24 -0700 | [diff] [blame] | 65 | } else if (netif_is_l3_slave(dev)) { |
David Ahern | 1b69c6d | 2015-09-29 20:07:11 -0700 | [diff] [blame] | 66 | /* Users of netdev_master_upper_dev_get_rcu need non-const, |
| 67 | * but current inet_*type functions take a const |
| 68 | */ |
| 69 | struct net_device *_dev = (struct net_device *) dev; |
| 70 | const struct net_device *master; |
| 71 | |
| 72 | master = netdev_master_upper_dev_get_rcu(_dev); |
David Ahern | fee6d4c | 2015-10-05 08:51:24 -0700 | [diff] [blame] | 73 | if (master && |
David Ahern | 1b69c6d | 2015-09-29 20:07:11 -0700 | [diff] [blame] | 74 | master->l3mdev_ops->l3mdev_fib_table) |
| 75 | tb_id = master->l3mdev_ops->l3mdev_fib_table(master); |
| 76 | } |
| 77 | |
| 78 | return tb_id; |
| 79 | } |
| 80 | EXPORT_SYMBOL_GPL(l3mdev_fib_table_rcu); |
| 81 | |
| 82 | u32 l3mdev_fib_table_by_index(struct net *net, int ifindex) |
| 83 | { |
| 84 | struct net_device *dev; |
| 85 | u32 tb_id = 0; |
| 86 | |
| 87 | if (!ifindex) |
| 88 | return 0; |
| 89 | |
| 90 | rcu_read_lock(); |
| 91 | |
| 92 | dev = dev_get_by_index_rcu(net, ifindex); |
| 93 | if (dev) |
| 94 | tb_id = l3mdev_fib_table_rcu(dev); |
| 95 | |
| 96 | rcu_read_unlock(); |
| 97 | |
| 98 | return tb_id; |
| 99 | } |
| 100 | EXPORT_SYMBOL_GPL(l3mdev_fib_table_by_index); |
David Ahern | 4a65896 | 2016-05-07 16:48:59 -0700 | [diff] [blame] | 101 | |
| 102 | /** |
| 103 | * l3mdev_get_rt6_dst - IPv6 route lookup based on flow. Returns |
| 104 | * cached route for L3 master device if relevant |
| 105 | * to flow |
| 106 | * @net: network namespace for device index lookup |
| 107 | * @fl6: IPv6 flow struct for lookup |
| 108 | */ |
| 109 | |
| 110 | struct dst_entry *l3mdev_get_rt6_dst(struct net *net, |
David Ahern | cd2a9e6 | 2016-06-13 13:44:17 -0700 | [diff] [blame] | 111 | struct flowi6 *fl6) |
David Ahern | 4a65896 | 2016-05-07 16:48:59 -0700 | [diff] [blame] | 112 | { |
| 113 | struct dst_entry *dst = NULL; |
| 114 | struct net_device *dev; |
| 115 | |
David Ahern | 1ff23be | 2016-05-07 16:49:00 -0700 | [diff] [blame] | 116 | if (fl6->flowi6_oif) { |
| 117 | rcu_read_lock(); |
| 118 | |
| 119 | dev = dev_get_by_index_rcu(net, fl6->flowi6_oif); |
| 120 | if (dev && netif_is_l3_slave(dev)) |
| 121 | dev = netdev_master_upper_dev_get_rcu(dev); |
| 122 | |
| 123 | if (dev && netif_is_l3_master(dev) && |
David Ahern | 4a65896 | 2016-05-07 16:48:59 -0700 | [diff] [blame] | 124 | dev->l3mdev_ops->l3mdev_get_rt6_dst) |
| 125 | dst = dev->l3mdev_ops->l3mdev_get_rt6_dst(dev, fl6); |
David Ahern | 1ff23be | 2016-05-07 16:49:00 -0700 | [diff] [blame] | 126 | |
| 127 | rcu_read_unlock(); |
David Ahern | 4a65896 | 2016-05-07 16:48:59 -0700 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | return dst; |
| 131 | } |
| 132 | EXPORT_SYMBOL_GPL(l3mdev_get_rt6_dst); |
| 133 | |
| 134 | /** |
| 135 | * l3mdev_get_saddr - get source address for a flow based on an interface |
| 136 | * enslaved to an L3 master device |
| 137 | * @net: network namespace for device index lookup |
| 138 | * @ifindex: Interface index |
| 139 | * @fl4: IPv4 flow struct |
| 140 | */ |
| 141 | |
| 142 | int l3mdev_get_saddr(struct net *net, int ifindex, struct flowi4 *fl4) |
| 143 | { |
| 144 | struct net_device *dev; |
| 145 | int rc = 0; |
| 146 | |
| 147 | if (ifindex) { |
| 148 | rcu_read_lock(); |
| 149 | |
| 150 | dev = dev_get_by_index_rcu(net, ifindex); |
David Ahern | 1ff23be | 2016-05-07 16:49:00 -0700 | [diff] [blame] | 151 | if (dev && netif_is_l3_slave(dev)) |
| 152 | dev = netdev_master_upper_dev_get_rcu(dev); |
| 153 | |
David Ahern | 4a65896 | 2016-05-07 16:48:59 -0700 | [diff] [blame] | 154 | if (dev && netif_is_l3_master(dev) && |
| 155 | dev->l3mdev_ops->l3mdev_get_saddr) |
| 156 | rc = dev->l3mdev_ops->l3mdev_get_saddr(dev, fl4); |
| 157 | |
| 158 | rcu_read_unlock(); |
| 159 | } |
| 160 | |
| 161 | return rc; |
| 162 | } |
| 163 | EXPORT_SYMBOL_GPL(l3mdev_get_saddr); |
David Ahern | 96c63fa | 2016-06-08 10:55:39 -0700 | [diff] [blame] | 164 | |
| 165 | /** |
| 166 | * l3mdev_fib_rule_match - Determine if flowi references an |
| 167 | * L3 master device |
| 168 | * @net: network namespace for device index lookup |
| 169 | * @fl: flow struct |
| 170 | */ |
| 171 | |
| 172 | int l3mdev_fib_rule_match(struct net *net, struct flowi *fl, |
| 173 | struct fib_lookup_arg *arg) |
| 174 | { |
| 175 | struct net_device *dev; |
| 176 | int rc = 0; |
| 177 | |
| 178 | rcu_read_lock(); |
| 179 | |
| 180 | dev = dev_get_by_index_rcu(net, fl->flowi_oif); |
| 181 | if (dev && netif_is_l3_master(dev) && |
| 182 | dev->l3mdev_ops->l3mdev_fib_table) { |
| 183 | arg->table = dev->l3mdev_ops->l3mdev_fib_table(dev); |
| 184 | rc = 1; |
| 185 | goto out; |
| 186 | } |
| 187 | |
| 188 | dev = dev_get_by_index_rcu(net, fl->flowi_iif); |
| 189 | if (dev && netif_is_l3_master(dev) && |
| 190 | dev->l3mdev_ops->l3mdev_fib_table) { |
| 191 | arg->table = dev->l3mdev_ops->l3mdev_fib_table(dev); |
| 192 | rc = 1; |
| 193 | goto out; |
| 194 | } |
| 195 | |
| 196 | out: |
| 197 | rcu_read_unlock(); |
| 198 | |
| 199 | return rc; |
| 200 | } |