1 | /* -*- indent-tabs-mode: nil; -*- */
|
---|
2 | #include "proxy.h"
|
---|
3 |
|
---|
4 | #include <sys/types.h>
|
---|
5 | #include <sys/socket.h>
|
---|
6 |
|
---|
7 | #include <net/if_dl.h>
|
---|
8 | #include <net/route.h>
|
---|
9 |
|
---|
10 | #include <netinet/in.h>
|
---|
11 | #include <netinet/ip6.h>
|
---|
12 |
|
---|
13 | #include <errno.h>
|
---|
14 | #include <string.h>
|
---|
15 | #include <unistd.h>
|
---|
16 |
|
---|
17 |
|
---|
18 | /**
|
---|
19 | * Query IPv6 routing table - BSD routing sockets version.
|
---|
20 | *
|
---|
21 | * We don't actually monitor the routing socket for updates, and
|
---|
22 | * instead query the kernel each time.
|
---|
23 | *
|
---|
24 | * We take a shortcut and don't read the reply to our RTM_GET - if
|
---|
25 | * there's no default IPv6 route, write(2) will fail with ESRCH
|
---|
26 | * synchronously. In theory it may fail asynchronously and we should
|
---|
27 | * wait for the RTM_GET reply and check rt_msghdr::rtm_errno.
|
---|
28 | *
|
---|
29 | * KAME code in *BSD maintains internally a list of default routers
|
---|
30 | * that it learned from RAs, and installs only one of them into the
|
---|
31 | * routing table (actually, I'm not sure if BSD routing table can
|
---|
32 | * handle multiple routes to the same destination). One side-effect
|
---|
33 | * of this is that when manually configured route (e.g. teredo) is
|
---|
34 | * deleted, the system will lose its default route even when KAME IPv6
|
---|
35 | * has default router(s) in its internal list. Next RA will force the
|
---|
36 | * update, though.
|
---|
37 | *
|
---|
38 | * Solaris does expose multiple routes in the routing table and
|
---|
39 | * replies to RTM_GET with "default default".
|
---|
40 | */
|
---|
41 | int
|
---|
42 | rtmon_get_defaults(void)
|
---|
43 | {
|
---|
44 | int rtsock;
|
---|
45 | struct req {
|
---|
46 | struct rt_msghdr rtm;
|
---|
47 | struct sockaddr_in6 dst;
|
---|
48 | struct sockaddr_in6 mask;
|
---|
49 | struct sockaddr_dl ifp;
|
---|
50 | } req;
|
---|
51 | ssize_t nsent;
|
---|
52 |
|
---|
53 | rtsock = socket(PF_ROUTE, SOCK_RAW, AF_INET6);
|
---|
54 | if (rtsock < 0) {
|
---|
55 | DPRINTF0(("rtmon: failed to create routing socket\n"));
|
---|
56 | return -1;
|
---|
57 | }
|
---|
58 |
|
---|
59 | memset(&req, 0, sizeof(req));
|
---|
60 |
|
---|
61 | req.rtm.rtm_type = RTM_GET;
|
---|
62 | req.rtm.rtm_version = RTM_VERSION;
|
---|
63 | req.rtm.rtm_msglen = sizeof(req);
|
---|
64 | req.rtm.rtm_seq = 0x12345;
|
---|
65 |
|
---|
66 | req.rtm.rtm_flags = RTF_UP;
|
---|
67 | req.rtm.rtm_addrs = RTA_DST | RTA_NETMASK | RTA_IFP;
|
---|
68 |
|
---|
69 | req.dst.sin6_family = AF_INET6;
|
---|
70 | #if HAVE_SA_LEN
|
---|
71 | req.dst.sin6_len = sizeof(req.dst);
|
---|
72 | #endif
|
---|
73 |
|
---|
74 | req.mask.sin6_family = AF_INET6;
|
---|
75 | #if HAVE_SA_LEN
|
---|
76 | req.mask.sin6_len = sizeof(req.mask);
|
---|
77 | #endif
|
---|
78 |
|
---|
79 | req.ifp.sdl_family = AF_LINK;
|
---|
80 | #if HAVE_SA_LEN
|
---|
81 | req.ifp.sdl_len = sizeof(req.ifp);
|
---|
82 | #endif
|
---|
83 |
|
---|
84 | nsent = write(rtsock, &req, req.rtm.rtm_msglen);
|
---|
85 | if (nsent < 0) {
|
---|
86 | if (errno == ESRCH) {
|
---|
87 | /* there's no default route */
|
---|
88 | return 0;
|
---|
89 | }
|
---|
90 | else {
|
---|
91 | DPRINTF0(("rtmon: failed to send RTM_GET\n"));
|
---|
92 | return -1;
|
---|
93 | }
|
---|
94 | }
|
---|
95 |
|
---|
96 | return 1;
|
---|
97 | }
|
---|