2 * dhcpcd - DHCP client daemon
3 * Copyright (c) 2006-2009 Roy Marples <roy@marples.name>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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
28 #include <sys/types.h>
35 /* Interface comparer for working out ordering. */
37 ifcmp(struct interface
*si
, struct interface
*ti
)
41 if (si
->state
&& !ti
->state
)
43 if (!si
->state
&& ti
->state
)
45 if (!si
->state
&& !ti
->state
)
47 /* If one has a lease and the other not, it takes precedence. */
48 if (si
->state
->new && !ti
->state
->new)
50 if (!si
->state
->new && ti
->state
->new)
52 /* If we are either, they neither have a lease, or they both have.
53 * We need to check for IPv4LL and make it non-preferred. */
55 sill
= IN_LINKLOCAL(htonl(si
->state
->new->yiaddr
));
56 till
= IN_LINKLOCAL(htonl(ti
->state
->new->yiaddr
));
62 /* Then carrier status. */
63 if (si
->carrier
> ti
->carrier
)
65 if (si
->carrier
< ti
->carrier
)
68 if (si
->metric
< ti
->metric
)
70 if (si
->metric
> ti
->metric
)
75 /* Sort the interfaces into a preferred order - best first, worst last. */
79 struct interface
*sorted
, *ifp
, *ifn
, *ift
;
81 if (!ifaces
|| !ifaces
->next
)
84 ifaces
= ifaces
->next
;
86 for (ifp
= ifaces
; ifp
&& (ifn
= ifp
->next
, 1); ifp
= ifn
) {
87 /* Are we the new head? */
88 if (ifcmp(ifp
, sorted
) == -1) {
93 /* Do we fit in the middle? */
94 for (ift
= sorted
; ift
->next
; ift
= ift
->next
) {
95 if (ifcmp(ifp
, ift
->next
) == -1) {
96 ifp
->next
= ift
->next
;
101 /* We must be at the end */