blob: 8da86ceca33ddf4c4e5d61850ce54f22a29e6fb9 [file] [log] [blame]
David Ahern1b69c6d2015-09-29 20:07:11 -07001/*
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 Ahern96c63fa2016-06-08 10:55:39 -070013#include <net/fib_rules.h>
David Ahern1b69c6d2015-09-29 20:07:11 -070014#include <net/l3mdev.h>
15
16/**
17 * l3mdev_master_ifindex - get index of L3 master device
18 * @dev: targeted interface
19 */
20
David Ahern3f2fb9a2016-02-24 11:47:02 -080021int l3mdev_master_ifindex_rcu(const struct net_device *dev)
David Ahern1b69c6d2015-09-29 20:07:11 -070022{
23 int ifindex = 0;
24
25 if (!dev)
26 return 0;
27
28 if (netif_is_l3_master(dev)) {
29 ifindex = dev->ifindex;
David Ahernfee6d4c2015-10-05 08:51:24 -070030 } else if (netif_is_l3_slave(dev)) {
David Ahern1b69c6d2015-09-29 20:07:11 -070031 struct net_device *master;
David Ahern3f2fb9a2016-02-24 11:47:02 -080032 struct net_device *_dev = (struct net_device *)dev;
David Ahern1b69c6d2015-09-29 20:07:11 -070033
David Ahern3f2fb9a2016-02-24 11:47:02 -080034 /* 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 Ahernfee6d4c2015-10-05 08:51:24 -070041 if (master)
David Ahern1b69c6d2015-09-29 20:07:11 -070042 ifindex = master->ifindex;
43 }
44
45 return ifindex;
46}
47EXPORT_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
55u32 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 Ahernfee6d4c2015-10-05 08:51:24 -070065 } else if (netif_is_l3_slave(dev)) {
David Ahern1b69c6d2015-09-29 20:07:11 -070066 /* 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 Ahernfee6d4c2015-10-05 08:51:24 -070073 if (master &&
David Ahern1b69c6d2015-09-29 20:07:11 -070074 master->l3mdev_ops->l3mdev_fib_table)
75 tb_id = master->l3mdev_ops->l3mdev_fib_table(master);
76 }
77
78 return tb_id;
79}
80EXPORT_SYMBOL_GPL(l3mdev_fib_table_rcu);
81
82u32 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}
100EXPORT_SYMBOL_GPL(l3mdev_fib_table_by_index);
David Ahern4a658962016-05-07 16:48:59 -0700101
102/**
David Ahern4c1feac2016-09-10 12:09:56 -0700103 * l3mdev_link_scope_lookup - IPv6 route lookup based on flow for link
104 * local and multicast addresses
David Ahern4a658962016-05-07 16:48:59 -0700105 * @net: network namespace for device index lookup
106 * @fl6: IPv6 flow struct for lookup
107 */
108
David Ahern4c1feac2016-09-10 12:09:56 -0700109struct dst_entry *l3mdev_link_scope_lookup(struct net *net,
110 struct flowi6 *fl6)
David Ahern4a658962016-05-07 16:48:59 -0700111{
112 struct dst_entry *dst = NULL;
113 struct net_device *dev;
114
David Ahern1ff23be2016-05-07 16:49:00 -0700115 if (fl6->flowi6_oif) {
116 rcu_read_lock();
117
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);
121
122 if (dev && netif_is_l3_master(dev) &&
David Ahern4c1feac2016-09-10 12:09:56 -0700123 dev->l3mdev_ops->l3mdev_link_scope_lookup)
124 dst = dev->l3mdev_ops->l3mdev_link_scope_lookup(dev, fl6);
David Ahern1ff23be2016-05-07 16:49:00 -0700125
126 rcu_read_unlock();
David Ahern4a658962016-05-07 16:48:59 -0700127 }
128
129 return dst;
130}
David Ahern4c1feac2016-09-10 12:09:56 -0700131EXPORT_SYMBOL_GPL(l3mdev_link_scope_lookup);
David Ahern4a658962016-05-07 16:48:59 -0700132
David Ahern96c63fa2016-06-08 10:55:39 -0700133/**
134 * l3mdev_fib_rule_match - Determine if flowi references an
135 * L3 master device
136 * @net: network namespace for device index lookup
137 * @fl: flow struct
138 */
139
140int l3mdev_fib_rule_match(struct net *net, struct flowi *fl,
141 struct fib_lookup_arg *arg)
142{
143 struct net_device *dev;
144 int rc = 0;
145
146 rcu_read_lock();
147
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);
152 rc = 1;
153 goto out;
154 }
155
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);
160 rc = 1;
161 goto out;
162 }
163
164out:
165 rcu_read_unlock();
166
167 return rc;
168}
David Ahern9ee00342016-09-10 12:09:52 -0700169
170void l3mdev_update_flow(struct net *net, struct flowi *fl)
171{
172 struct net_device *dev;
173 int ifindex;
174
175 rcu_read_lock();
176
177 if (fl->flowi_oif) {
178 dev = dev_get_by_index_rcu(net, fl->flowi_oif);
179 if (dev) {
180 ifindex = l3mdev_master_ifindex_rcu(dev);
181 if (ifindex) {
182 fl->flowi_oif = ifindex;
183 fl->flowi_flags |= FLOWI_FLAG_SKIP_NH_OIF;
184 goto out;
185 }
186 }
187 }
188
189 if (fl->flowi_iif) {
190 dev = dev_get_by_index_rcu(net, fl->flowi_iif);
191 if (dev) {
192 ifindex = l3mdev_master_ifindex_rcu(dev);
193 if (ifindex) {
194 fl->flowi_iif = ifindex;
195 fl->flowi_flags |= FLOWI_FLAG_SKIP_NH_OIF;
196 }
197 }
198 }
199
200out:
201 rcu_read_unlock();
202}
203EXPORT_SYMBOL_GPL(l3mdev_update_flow);