52
52
struct fib_table *local_table, *main_table;
54
local_table = fib_hash_table(RT_TABLE_LOCAL);
54
local_table = fib_trie_table(RT_TABLE_LOCAL);
55
55
if (local_table == NULL)
58
main_table = fib_hash_table(RT_TABLE_MAIN);
58
main_table = fib_trie_table(RT_TABLE_MAIN);
59
59
if (main_table == NULL)
115
115
#endif /* CONFIG_IP_MULTIPLE_TABLES */
117
void fib_select_default(struct net *net,
118
const struct flowi *flp, struct fib_result *res)
120
struct fib_table *tb;
121
int table = RT_TABLE_MAIN;
122
#ifdef CONFIG_IP_MULTIPLE_TABLES
123
if (res->r == NULL || res->r->action != FR_ACT_TO_TBL)
125
table = res->r->table;
127
tb = fib_get_table(net, table);
128
if (FIB_RES_GW(*res) && FIB_RES_NH(*res).nh_scope == RT_SCOPE_LINK)
129
fib_table_select_default(tb, flp, res);
132
117
static void fib_flush(struct net *net)
147
132
rt_cache_flush(net, -1);
151
* __ip_dev_find - find the first device with a given source address.
152
* @net: the net namespace
153
* @addr: the source address
154
* @devref: if true, take a reference on the found device
156
* If a caller uses devref=false, it should be protected by RCU, or RTNL
158
struct net_device *__ip_dev_find(struct net *net, __be32 addr, bool devref)
163
struct fib_result res = { 0 };
164
struct net_device *dev = NULL;
165
struct fib_table *local_table;
167
#ifdef CONFIG_IP_MULTIPLE_TABLES
172
local_table = fib_get_table(net, RT_TABLE_LOCAL);
174
fib_table_lookup(local_table, &fl, &res, FIB_LOOKUP_NOREF)) {
178
if (res.type != RTN_LOCAL)
180
dev = FIB_RES_DEV(res);
188
EXPORT_SYMBOL(__ip_dev_find);
191
136
* Find address type as if only "dev" was present in the system. If
192
137
* on_dev is NULL then all interfaces are taken into consideration.
195
140
const struct net_device *dev,
198
struct flowi fl = { .fl4_dst = addr };
143
struct flowi4 fl4 = { .daddr = addr };
199
144
struct fib_result res;
200
145
unsigned ret = RTN_BROADCAST;
201
146
struct fib_table *local_table;
213
158
if (local_table) {
214
159
ret = RTN_UNICAST;
216
if (!fib_table_lookup(local_table, &fl, &res, FIB_LOOKUP_NOREF)) {
161
if (!fib_table_lookup(local_table, &fl4, &res, FIB_LOOKUP_NOREF)) {
217
162
if (!dev || dev == res.fi->fib_dev)
248
193
u32 *itag, u32 mark)
250
195
struct in_device *in_dev;
258
197
struct fib_result res;
259
198
int no_addr, rpf, accept_local;
204
fl4.flowi4_iif = oif;
205
fl4.flowi4_mark = mark;
208
fl4.flowi4_tos = tos;
209
fl4.flowi4_scope = RT_SCOPE_UNIVERSE;
264
211
no_addr = rpf = accept_local = 0;
265
212
in_dev = __in_dev_get_rcu(dev);
268
215
rpf = IN_DEV_RPFILTER(in_dev);
269
216
accept_local = IN_DEV_ACCEPT_LOCAL(in_dev);
270
217
if (mark && !IN_DEV_SRC_VMARK(in_dev))
274
221
if (in_dev == NULL)
277
224
net = dev_net(dev);
278
if (fib_lookup(net, &fl, &res))
225
if (fib_lookup(net, &fl4, &res))
279
226
goto last_resort;
280
227
if (res.type != RTN_UNICAST) {
281
228
if (res.type != RTN_LOCAL || !accept_local)
284
*spec_dst = FIB_RES_PREFSRC(res);
231
*spec_dst = FIB_RES_PREFSRC(net, res);
285
232
fib_combine_itag(itag, &res);
286
233
dev_match = false;
306
253
goto last_resort;
309
fl.oif = dev->ifindex;
256
fl4.flowi4_oif = dev->ifindex;
312
if (fib_lookup(net, &fl, &res) == 0) {
259
if (fib_lookup(net, &fl4, &res) == 0) {
313
260
if (res.type == RTN_UNICAST) {
314
*spec_dst = FIB_RES_PREFSRC(res);
261
*spec_dst = FIB_RES_PREFSRC(net, res);
315
262
ret = FIB_RES_NH(res).nh_scope >= RT_SCOPE_HOST;
778
static void fib_del_ifaddr(struct in_ifaddr *ifa)
725
/* Delete primary or secondary address.
726
* Optionally, on secondary address promotion consider the addresses
727
* from subnet iprim as deleted, even if they are in device list.
728
* In this case the secondary ifa can be in device list.
730
void fib_del_ifaddr(struct in_ifaddr *ifa, struct in_ifaddr *iprim)
780
732
struct in_device *in_dev = ifa->ifa_dev;
781
733
struct net_device *dev = in_dev->dev;
782
734
struct in_ifaddr *ifa1;
783
struct in_ifaddr *prim = ifa;
735
struct in_ifaddr *prim = ifa, *prim1 = NULL;
784
736
__be32 brd = ifa->ifa_address | ~ifa->ifa_mask;
785
737
__be32 any = ifa->ifa_address & ifa->ifa_mask;
786
738
#define LOCAL_OK 1
788
740
#define BRD0_OK 4
789
741
#define BRD1_OK 8
743
int subnet = 0; /* Primary network */
744
int gone = 1; /* Address is missing */
745
int same_prefsrc = 0; /* Another primary with same IP */
792
if (!(ifa->ifa_flags & IFA_F_SECONDARY))
747
if (ifa->ifa_flags & IFA_F_SECONDARY) {
748
prim = inet_ifa_byprefix(in_dev, any, ifa->ifa_mask);
750
printk(KERN_WARNING "fib_del_ifaddr: bug: prim == NULL\n");
753
if (iprim && iprim != prim) {
754
printk(KERN_WARNING "fib_del_ifaddr: bug: iprim != prim\n");
757
} else if (!ipv4_is_zeronet(any) &&
758
(any != ifa->ifa_local || ifa->ifa_prefixlen < 32)) {
793
759
fib_magic(RTM_DELROUTE,
794
760
dev->flags & IFF_LOOPBACK ? RTN_LOCAL : RTN_UNICAST,
795
761
any, ifa->ifa_prefixlen, prim);
797
prim = inet_ifa_byprefix(in_dev, any, ifa->ifa_mask);
799
printk(KERN_WARNING "fib_del_ifaddr: bug: prim == NULL\n");
804
765
/* Deletion is more complicated than add.
810
771
for (ifa1 = in_dev->ifa_list; ifa1; ifa1 = ifa1->ifa_next) {
773
/* promotion, keep the IP */
777
/* Ignore IFAs from our subnet */
778
if (iprim && ifa1->ifa_mask == iprim->ifa_mask &&
779
inet_ifa_match(ifa1->ifa_address, iprim))
782
/* Ignore ifa1 if it uses different primary IP (prefsrc) */
783
if (ifa1->ifa_flags & IFA_F_SECONDARY) {
784
/* Another address from our subnet? */
785
if (ifa1->ifa_mask == prim->ifa_mask &&
786
inet_ifa_match(ifa1->ifa_address, prim))
789
/* We reached the secondaries, so
790
* same_prefsrc should be determined.
794
/* Search new prim1 if ifa1 is not
795
* using the current prim1
798
ifa1->ifa_mask != prim1->ifa_mask ||
799
!inet_ifa_match(ifa1->ifa_address, prim1))
800
prim1 = inet_ifa_byprefix(in_dev,
805
if (prim1->ifa_local != prim->ifa_local)
809
if (prim->ifa_local != ifa1->ifa_local)
811
815
if (ifa->ifa_local == ifa1->ifa_local)
813
817
if (ifa->ifa_broadcast == ifa1->ifa_broadcast)
817
821
if (any == ifa1->ifa_broadcast)
823
/* primary has network specific broadcasts */
824
if (prim1 == ifa1 && ifa1->ifa_prefixlen < 31) {
825
__be32 brd1 = ifa1->ifa_address | ~ifa1->ifa_mask;
826
__be32 any1 = ifa1->ifa_address & ifa1->ifa_mask;
828
if (!ipv4_is_zeronet(any1)) {
829
if (ifa->ifa_broadcast == brd1 ||
830
ifa->ifa_broadcast == any1)
832
if (brd == brd1 || brd == any1)
834
if (any == brd1 || any == any1)
821
840
if (!(ok & BRD_OK))
822
841
fib_magic(RTM_DELROUTE, RTN_BROADCAST, ifa->ifa_broadcast, 32, prim);
824
fib_magic(RTM_DELROUTE, RTN_BROADCAST, brd, 32, prim);
826
fib_magic(RTM_DELROUTE, RTN_BROADCAST, any, 32, prim);
842
if (subnet && ifa->ifa_prefixlen < 31) {
844
fib_magic(RTM_DELROUTE, RTN_BROADCAST, brd, 32, prim);
846
fib_magic(RTM_DELROUTE, RTN_BROADCAST, any, 32, prim);
827
848
if (!(ok & LOCAL_OK)) {
828
849
fib_magic(RTM_DELROUTE, RTN_LOCAL, ifa->ifa_local, 32, prim);
830
851
/* Check, that this local address finally disappeared. */
831
if (inet_addr_type(dev_net(dev), ifa->ifa_local) != RTN_LOCAL) {
853
inet_addr_type(dev_net(dev), ifa->ifa_local) != RTN_LOCAL) {
832
854
/* And the last, but not the least thing.
833
855
* We must flush stray FIB entries.
851
873
struct fib_result res;
853
.mark = frn->fl_mark,
854
.fl4_dst = frn->fl_addr,
855
.fl4_tos = frn->fl_tos,
856
.fl4_scope = frn->fl_scope,
874
struct flowi4 fl4 = {
875
.flowi4_mark = frn->fl_mark,
876
.daddr = frn->fl_addr,
877
.flowi4_tos = frn->fl_tos,
878
.flowi4_scope = frn->fl_scope,
859
881
#ifdef CONFIG_IP_MULTIPLE_TABLES
867
889
frn->tb_id = tb->tb_id;
869
frn->err = fib_table_lookup(tb, &fl, &res, FIB_LOOKUP_NOREF);
891
frn->err = fib_table_lookup(tb, &fl4, &res, FIB_LOOKUP_NOREF);
872
894
frn->prefixlen = res.prefixlen;
945
968
#ifdef CONFIG_IP_ROUTE_MULTIPATH
946
969
fib_sync_up(dev);
971
atomic_inc(&net->ipv4.dev_addr_genid);
948
972
rt_cache_flush(dev_net(dev), -1);
950
974
case NETDEV_DOWN:
975
fib_del_ifaddr(ifa, NULL);
976
atomic_inc(&net->ipv4.dev_addr_genid);
952
977
if (ifa->ifa_dev->ifa_list == NULL) {
953
978
/* Last address was deleted from this interface.
967
992
struct net_device *dev = ptr;
968
993
struct in_device *in_dev = __in_dev_get_rtnl(dev);
994
struct net *net = dev_net(dev);
970
996
if (event == NETDEV_UNREGISTER) {
971
997
fib_disable_ip(dev, 2, -1);