File: ocatipv6route.c

package info (click to toggle)
onioncat 0.2.2%2Bsvn553-3
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 956 kB
  • sloc: ansic: 5,400; sh: 3,822; perl: 47; makefile: 22
file content (170 lines) | stat: -rw-r--r-- 4,197 bytes parent folder | download | duplicates (4)
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/* Copyright 2008 Bernhard R. Fischer, Daniel Haslinger.
 *
 * This file is part of OnionCat.
 *
 * OnionCat is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, version 3 of the License.
 *
 * OnionCat is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with OnionCat. If not, see <http://www.gnu.org/licenses/>.
 */

/*! @file
 *  This file contains functions for managing IPv6 routing and
 *  forwarding.
 *
 *  @author Bernhard R. Fischer <rahra _at_ cypherpunk at>
 *  @version 2008/09/03-01
 */


#include "ocat.h"


/*! IPv6 Routing table. Each entry contains 3 values:
 *  destination network, prefix length, gateway
 */
static IPv6Route_t *v6route_ = NULL;
static int v6route_cnt_ = 0;
static pthread_mutex_t v6route_mutex_ = PTHREAD_MUTEX_INITIALIZER;


/*! Reduce IPv6 address to prefix, i.e. cut off host id.
 *  @param net IPv6 address
 *  @param prefixlen Prefix length
 */
void ipv6_reduce(struct in6_addr *net, int prefixlen)
{
   int i;
   char m;

   for (i = 0; i < ((128 - prefixlen) >> 3); i++)
      net->s6_addr[15 - i] = 0;

   m = 0xff << (8 - (prefixlen % 8));
   net->s6_addr[prefixlen >> 3] &= m;

}


/*! Lookup IPv6 route. 
 */
struct in6_addr *ipv6_lookup_route(const struct in6_addr *dest)
{
   struct in6_addr addr;
   int i, n;

   pthread_mutex_lock(&v6route_mutex_);
   n = v6route_cnt_;
   for (i = 0; i < n; i++)
   {
      addr = *dest;
      ipv6_reduce(&addr, v6route_[i].prefixlen);
      if (IN6_ARE_ADDR_EQUAL(&v6route_[i].dest, &addr))
      {
         log_debug("IPv6 route found");
         break;
      }
   }
   pthread_mutex_unlock(&v6route_mutex_);
   return i < n ? &v6route_[i].gw : NULL;
}


/*! Add an IPv6 route to IPv6 routing table.
 *  @return -1 if table is full else return index of entry.
 */
int ipv6_add_route(const IPv6Route_t *route)
{
   int r = -1;
   IPv6Route_t *rt;

   pthread_mutex_lock(&v6route_mutex_);
   if ((rt = realloc(v6route_, sizeof(IPv6Route_t) * (v6route_cnt_ + 1))))
   {
      v6route_ = rt;
      r = v6route_cnt_;
      memcpy(&v6route_[v6route_cnt_++], route, sizeof(IPv6Route_t));
   }
   pthread_mutex_unlock(&v6route_mutex_);
   return r;
}


void ipv6_print(IPv6Route_t *route, void *f)
{
   char addr[INET6_ADDRSTRLEN];

   inet_ntop(AF_INET6, &route->dest, addr, INET6_ADDRSTRLEN);
   fprintf(f, "IN6 %s %3d ", addr, route->prefixlen);
   inet_ntop(AF_INET6, &route->gw, addr, INET6_ADDRSTRLEN);
   fprintf(f, "%s %p\n", addr, route);
}


void ipv6_print_routes(FILE *f)
{
   int i;

   pthread_mutex_lock(&v6route_mutex_);
   for (i = 0; i < v6route_cnt_; i++)
      ipv6_print(&v6route_[i], f);
   pthread_mutex_unlock(&v6route_mutex_);
}


/*! Parse IPv6 route and add it to routing table.
 *  @return index of routing table entry (>= 0) or an integer < 0 on failure.
 */
int ipv6_parse_route(const char *rs)
{
   char buf[strlen(rs) + 1], *s, *b;
   IPv6Route_t route6;

   if (!rs)
      return E_RT_NULLPTR;

   log_debug("IPv6 route parser: \"%s\"", rs);

   strlcpy(buf, rs, strlen(rs) + 1);
   if (!(s = strtok_r(buf, " \t", &b)))
      return E_RT_SYNTAX;

   if (inet_pton(AF_INET6, s, &route6.dest) != 1)
      return E_RT_SYNTAX;

   if (!(s = strtok_r(NULL, " \t", &b)))
      return E_RT_SYNTAX;

   errno = 0;
   route6.prefixlen = strtol(s, NULL, 10);
   if (errno)
      return E_RT_SYNTAX;
   if ((route6.prefixlen < 0) || (route6.prefixlen > 128))
      return E_RT_ILLNM;

   if (!(s = strtok_r(NULL, " \t", &b)))
      return E_RT_SYNTAX;

   if (inet_pton(AF_INET6, s, &route6.gw) != 1)
      return E_RT_SYNTAX;

   if (!has_tor_prefix(&route6.gw))
      return E_RT_NOTORGW;

   if (IN6_ARE_ADDR_EQUAL(&route6.gw, &CNF(ocat_addr)))
      return E_RT_GWSELF;

   ipv6_reduce(&route6.dest, route6.prefixlen);
   if (ipv6_lookup_route(&route6.dest))
      return E_RT_DUP;

   return ipv6_add_route(&route6);
}