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