1 | /* |
---|
2 | * dhcpcd - DHCP client daemon |
---|
3 | * Copyright (c) 2006-2013 Roy Marples <roy@marples.name> |
---|
4 | * All rights reserved |
---|
5 | |
---|
6 | * Redistribution and use in source and binary forms, with or without |
---|
7 | * modification, are permitted provided that the following conditions |
---|
8 | * are met: |
---|
9 | * 1. Redistributions of source code must retain the above copyright |
---|
10 | * notice, this list of conditions and the following disclaimer. |
---|
11 | * 2. Redistributions in binary form must reproduce the above copyright |
---|
12 | * notice, this list of conditions and the following disclaimer in the |
---|
13 | * documentation and/or other materials provided with the distribution. |
---|
14 | * |
---|
15 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
---|
16 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
17 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
18 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
---|
19 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
---|
20 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
---|
21 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
---|
22 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
---|
23 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
---|
24 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
---|
25 | * SUCH DAMAGE. |
---|
26 | */ |
---|
27 | |
---|
28 | #include <sys/types.h> |
---|
29 | |
---|
30 | #include "config.h" |
---|
31 | #include "dhcp.h" |
---|
32 | #include "if-pref.h" |
---|
33 | #include "net.h" |
---|
34 | |
---|
35 | /* Interface comparer for working out ordering. */ |
---|
36 | static int |
---|
37 | ifcmp(const struct interface *si, const struct interface *ti) |
---|
38 | { |
---|
39 | int sill, till; |
---|
40 | const struct dhcp_state *sis, *tis; |
---|
41 | |
---|
42 | sis = D_CSTATE(si); |
---|
43 | tis = D_CSTATE(ti); |
---|
44 | if (sis && !tis) |
---|
45 | return -1; |
---|
46 | if (!sis && tis) |
---|
47 | return 1; |
---|
48 | if (!sis && !tis) |
---|
49 | return 0; |
---|
50 | /* If one has a lease and the other not, it takes precedence. */ |
---|
51 | if (sis->new && !tis->new) |
---|
52 | return -1; |
---|
53 | if (!sis->new && tis->new) |
---|
54 | return 1; |
---|
55 | /* If we are either, they neither have a lease, or they both have. |
---|
56 | * We need to check for IPv4LL and make it non-preferred. */ |
---|
57 | if (sis->new && tis->new) { |
---|
58 | sill = (sis->new->cookie == htonl(MAGIC_COOKIE)); |
---|
59 | till = (tis->new->cookie == htonl(MAGIC_COOKIE)); |
---|
60 | if (!sill && till) |
---|
61 | return 1; |
---|
62 | if (sill && !till) |
---|
63 | return -1; |
---|
64 | } |
---|
65 | /* Then carrier status. */ |
---|
66 | if (si->carrier > ti->carrier) |
---|
67 | return -1; |
---|
68 | if (si->carrier < ti->carrier) |
---|
69 | return 1; |
---|
70 | /* Finally, metric */ |
---|
71 | if (si->metric < ti->metric) |
---|
72 | return -1; |
---|
73 | if (si->metric > ti->metric) |
---|
74 | return 1; |
---|
75 | return 0; |
---|
76 | } |
---|
77 | |
---|
78 | /* Sort the interfaces into a preferred order - best first, worst last. */ |
---|
79 | void |
---|
80 | sort_interfaces(void) |
---|
81 | { |
---|
82 | struct if_head sorted; |
---|
83 | struct interface *ifp, *ift; |
---|
84 | |
---|
85 | if (ifaces == NULL || |
---|
86 | (ifp = TAILQ_FIRST(ifaces)) == NULL || |
---|
87 | TAILQ_NEXT(ifp, next) == NULL) |
---|
88 | return; |
---|
89 | |
---|
90 | TAILQ_INIT(&sorted); |
---|
91 | TAILQ_REMOVE(ifaces, ifp, next); |
---|
92 | TAILQ_INSERT_HEAD(&sorted, ifp, next); |
---|
93 | while ((ifp = TAILQ_FIRST(ifaces))) { |
---|
94 | TAILQ_REMOVE(ifaces, ifp, next); |
---|
95 | TAILQ_FOREACH(ift, &sorted, next) { |
---|
96 | if (ifcmp(ifp, ift) == -1) { |
---|
97 | TAILQ_INSERT_BEFORE(ift, ifp, next); |
---|
98 | break; |
---|
99 | } |
---|
100 | } |
---|
101 | if (ift == NULL) |
---|
102 | TAILQ_INSERT_TAIL(&sorted, ifp, next); |
---|
103 | } |
---|
104 | TAILQ_CONCAT(ifaces, &sorted, next); |
---|
105 | } |
---|