summaryrefslogtreecommitdiffstats
path: root/rtemsbsd/rtems/rtems-routes.c
blob: 6663e8d48f889453511021af6c5647053f09f000 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*
 * Copyright (c) 2016 Chris Johns <chrisj@rtems.org>.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

/*
 * Useful functions to access the routing tables. Based on unpv13e from
 * Stevens.
 */

#include <stdbool.h>
#include <errno.h>

#include <net/if.h>
#include <net/route.h>
#include <netinet/in.h>
#include <sys/socket.h>

/*
 * Round up 'a' to next multiple of 'size', which must be a power of 2
 */
#define RR_ROUNDUP(a, size) (((a) & ((size)-1)) ? (1 + ((a) | ((size)-1))) : (a))

/*
 * Step to next socket address structure;
 * if sa_len is 0, assume it is sizeof(u_long).
 */
#define NEXT_SA(ap)							\
  ap = (struct sockaddr*) ((caddr_t) ap +				\
			   (ap->sa_len ? RR_ROUNDUP(ap->sa_len, sizeof (u_long)) : sizeof(u_long)))

static void
get_rtaddrs(int addrs, struct sockaddr* sa, struct sockaddr** rti_info)
{
  int i;

  for (i = 0; i < RTAX_MAX; i++) {
    if ((addrs & (1 << i)) != 0) {
      rti_info[i] = sa;
      NEXT_SA(sa);
    } else
      rti_info[i] = NULL;
  }
}

/*
 * Get a route.
 */
int rtems_get_route(const struct sockaddr_in* sin, struct sockaddr** rti_info)
{
  int                 s;
  char*               buf;
  const size_t        buflen = sizeof(struct rt_msghdr) + 512;
  pid_t               pid;
  struct rt_msghdr*   rtm;
  struct sockaddr_in* r_sin;
  struct sockaddr*    sa;
  const int           seq = (int) 0x28290817;
  ssize_t             r;

  buf = calloc(1, buflen);
  if (buf == NULL) {
    errno = ENOMEM;
    return -1;
  }

  s = socket(AF_ROUTE, SOCK_RAW, AF_UNSPEC);
  if (s < 0)
    return -1;

  rtm = (struct rt_msghdr *) buf;
  rtm->rtm_msglen = sizeof(struct rt_msghdr) + sizeof(struct sockaddr_in);
  rtm->rtm_version = RTM_VERSION;
  rtm->rtm_type = RTM_GET;
  rtm->rtm_addrs = RTA_DST;
  rtm->rtm_pid = pid = getpid();
  rtm->rtm_seq = seq;

  r_sin = (struct sockaddr_in *) (rtm + 1);
  *r_sin = *sin;

  r = write(s, rtm, rtm->rtm_msglen);
  if (r < 0) {
    int en = errno;
    close(s);
    free(buf);
    errno = en;
    return -1;
  }

  while (true) {
    r = read(s, rtm, buflen);
    if (r < 0) {
      int en = errno;
      close(s);
      free(buf);
      errno = en;
      return -1;
    }

    /*
     * The kernel sends all routing message to all routing sockets. We need to
     * filter them for the one for us.
     */
    if (rtm->rtm_type == RTM_GET &&
	rtm->rtm_seq == seq &&
	rtm->rtm_pid == pid)
      break;
  }

  close(s);

  rtm = (struct rt_msghdr*) buf;
  sa = (struct sockaddr*) (rtm + 1);

  get_rtaddrs(rtm->rtm_addrs, sa, rti_info);

  free(buf);

  return 0;
}