Debian Codegraph @

tcpip/rip98r.c

raw
1 #include <unistd.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include <syslog.h>
6
7 #include <sys/types.h>
8 #include <sys/ioctl.h>
9
10 #include <sys/socket.h>
11 #include <arpa/inet.h>
12 #include <net/if.h>
13 #include <netinet/in.h>
14 #include <net/route.h>
15
16
17 #include "rip98d.h"
18
19 #define UNMATCH_ROUTE 0
20 #define NO_ROUTE 1
21 #define REPLACE_ROUTE 2
22 #define ADDITIONAL_ROUTE 3
23
24 static int cmp_route(struct route_struct *route, struct in_addr addr, int bits, int metric)
25 {
26 unsigned int old_mask, new_mask;
27 unsigned int old_addr, new_addr;
28
29 old_mask = bits2mask(route->bits);
30 new_mask = bits2mask(bits);
31
32 old_addr = route->addr.s_addr;
33 new_addr = addr.s_addr;
34
35 if (bits < route->bits) {
36 new_addr = ntohl(new_addr) & new_mask;
37 old_addr = ntohl(old_addr) & new_mask;
38 } else {
39 new_addr = ntohl(new_addr) & old_mask;
40 old_addr = ntohl(old_addr) & old_mask;
41 }
42
43 if (route->action == DEL_ROUTE || route->action == NEW_ROUTE)
44 return UNMATCH_ROUTE;
45
46 if (old_addr != new_addr)
47 return UNMATCH_ROUTE;
48
49 if (route->metric <= metric)
50 return NO_ROUTE;
51
52 if (debug && logging) {
53 syslog(LOG_DEBUG, "comparing old: %s/%d to\n", inet_ntoa(route->addr), route->bits);
54 syslog(LOG_DEBUG, " new: %s/%d\n", inet_ntoa(addr), bits);
55 }
56
57 if (route->bits <= bits) {
58 if (route->action == ORIG_ROUTE) {
59 if (debug && logging)
60 syslog(LOG_DEBUG, " better route, replacing existing route\n");
61 return REPLACE_ROUTE;
62 } else {
63 if (debug && logging)
64 syslog(LOG_DEBUG, " better route, adding additional route\n");
65 return ADDITIONAL_ROUTE;
66 }
67 } else {
68 if (debug && logging)
69 syslog(LOG_DEBUG, " better route, adding additional route\n");
70 return ADDITIONAL_ROUTE;
71 }
72 }
73
74 void receive_routes(int s)
75 {
76 unsigned char message[500], *p;
77 struct sockaddr_in rem_addr;
78 struct in_addr addr;
79 struct route_struct *route, *new;
80 struct sockaddr_in trg;
81 struct rtentry rt;
82 unsigned long int netmask;
83 unsigned long int network;
84 int bits, metric;
85 int found, matched;
86 socklen_t size;
87 int mess_len;
88 int i;
89
90 size = sizeof(struct sockaddr_in);
91
92 mess_len = recvfrom(s, message, 500, 0, (struct sockaddr *)&rem_addr, &size);
93
94 if (message[0] != RIPCMD_RESPONSE || message[1] != RIP_VERSION_98) {
95 if (debug && logging) {
96 syslog(LOG_DEBUG, "invalid RIP98 header received\n");
97 syslog(LOG_DEBUG, " cmd: %d vers: %d\n", message[0], message[1]);
98 }
99 return;
100 }
101
102 for (found = FALSE, i = 0; i < dest_count; i++) {
103 if (rem_addr.sin_addr.s_addr == dest_list[i].dest_addr.s_addr) {
104 found = TRUE;
105 break;
106 }
107 }
108
109 if (!found) {
110 if (debug && logging)
111 syslog(LOG_DEBUG, "RIP98 message from unknown address %s\n", inet_ntoa(rem_addr.sin_addr));
112 return;
113 }
114
115 if (debug && logging)
116 syslog(LOG_DEBUG, "RIP98 message received from %s\n", inet_ntoa(rem_addr.sin_addr));
117
118 for (p = message + RIP98_HEADER; p < message + mess_len; p += RIP98_ENTRY) {
119 memcpy(&addr, p, sizeof(addr));
120 bits = p[4];
121 metric = p[5];
122
123 network = inet_netof(addr);
124
125 if (network == 0 || network == 127) {
126 if (debug && logging)
127 syslog(LOG_DEBUG, " route to %s/%d metric %d - rejected\n", inet_ntoa(addr), bits, metric);
128 continue;
129 }
130
131 if (route_restrict) {
132 if (network != 44) {
133 if (debug && logging)
134 syslog(LOG_DEBUG, " route to %s/%d metric %d - rejected\n", inet_ntoa(addr), bits, metric);
135 continue;
136 }
137 }
138
139 if (debug && logging)
140 syslog(LOG_DEBUG, " route to %s/%d metric %d\n", inet_ntoa(addr), bits, metric);
141
142 metric++;
143
144 if (metric > RIP98_INFINITY)
145 metric = RIP98_INFINITY;
146
147 found = FALSE;
148 matched = FALSE;
149
150 for (route = first_route; route != NULL; route = route->next) {
151
152 switch (cmp_route(route, addr, bits, metric)) {
153
154 case NO_ROUTE:
155 matched = TRUE;
156 break;
157
158 case REPLACE_ROUTE:
159 route->action = DEL_ROUTE;
160
161 case ADDITIONAL_ROUTE:
162 if (!found) {
163 new = malloc(sizeof(struct route_struct));
164 if (new == NULL) {
165 if (logging)
166 syslog(LOG_ERR, "out of memory\n");
167 return;
168 }
169
170 new->addr = addr;
171 new->bits = bits;
172 new->metric = metric;
173 new->action = NEW_ROUTE;
174
175 new->next = first_route;
176 first_route = new;
177
178 found = TRUE;
179 }
180
181 matched = TRUE;
182 break;
183
184 default:
185 break;
186 }
187 }
188
189 if (!matched) {
190 new = malloc(sizeof(struct route_struct));
191 if (new == NULL) {
192 if (logging)
193 syslog(LOG_ERR, "out of memory\n");
194 return;
195 }
196
197 new->addr = addr;
198 new->bits = bits;
199 new->metric = metric;
200 new->action = NEW_ROUTE;
201
202 new->next = first_route;
203 first_route = new;
204 }
205 }
206
207 for (route = first_route; route != NULL; route = route->next) {
208 if (route->action == DEL_ROUTE) {
209 memset(&rt, 0, sizeof(rt));
210
211 trg.sin_family = AF_INET;
212 trg.sin_addr = route->addr;
213 trg.sin_port = 0;
214 memcpy(&rt.rt_dst, &trg, sizeof(struct sockaddr));
215
216 if (ioctl(s, SIOCDELRT, &rt) < 0) {
217 if (logging)
218 syslog(LOG_ERR, "SIOCDELRT: %m");
219 }
220 }
221 }
222
223 for (route = first_route; route != NULL; route = route->next) {
224 if (route->action == NEW_ROUTE) {
225 memset(&rt, 0, sizeof(rt));
226
227 trg.sin_family = AF_INET;
228 trg.sin_addr = route->addr;
229 trg.sin_port = 0;
230 memcpy(&rt.rt_dst, &trg, sizeof(struct sockaddr));
231
232 rt.rt_flags = RTF_UP | RTF_GATEWAY | RTF_DYNAMIC;
233
234 if (route->bits == 32) {
235 rt.rt_flags |= RTF_HOST;
236 } else {
237 netmask = htonl(bits2mask(route->bits));
238
239 trg.sin_family = AF_INET;
240 memcpy(&trg.sin_addr, &netmask,
241 sizeof(struct in_addr));
242 trg.sin_port = 0;
243 memcpy(&rt.rt_genmask, &trg,
244 sizeof(struct sockaddr));
245 }
246
247 rt.rt_metric = route->metric + 1;
248
249 trg.sin_family = AF_INET;
250 trg.sin_addr = rem_addr.sin_addr;
251 trg.sin_port = 0;
252 memcpy(&rt.rt_gateway, &trg, sizeof(struct sockaddr));
253
254 if (ioctl(s, SIOCADDRT, &rt) < 0) {
255 if (logging)
256 syslog(LOG_ERR, "SIOCADDRT: %m");
257 }
258 }
259 }
260 }