00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042 #include "common/avl.h"
00043 #include "avl_olsr_comp.h"
00044 #include "net_olsr.h"
00045 #include "ipcalc.h"
00046 #include "olsr.h"
00047 #include "os_net.h"
00048 #include "link_set.h"
00049 #include "lq_packet.h"
00050 #include "olsr_logging.h"
00051
00052 #include <stdlib.h>
00053 #include <assert.h>
00054 #include <limits.h>
00055 #include <errno.h>
00056
00057 static void olsr_add_invalid_address(const union olsr_ip_addr *);
00058 static void olsr_expire_buffer_timer(void *context);
00059
00060 #if 0 // WIN32
00061 #define perror(x) WinSockPError(x)
00062 void
00063 WinSockPError(const char *);
00064 #endif
00065
00066
00067
00068
00069 static struct avl_tree filter_tree;
00070
00071
00072
00073 struct ptf {
00074 packet_transform_function function;
00075 struct ptf *next;
00076 };
00077
00078 static struct ptf *ptf_list;
00079
00080 static const char *const deny_ipv4_defaults[] = {
00081 "0.0.0.0",
00082 "127.0.0.1",
00083 NULL
00084 };
00085
00086 static const char *const deny_ipv6_defaults[] = {
00087 "0::0",
00088 "0::1",
00089 NULL
00090 };
00091
00092
00093 struct olsr_timer_info *buffer_hold_timer_info;
00094
00095
00096
00097
00098
00099 void
00100 init_net(void)
00101 {
00102 const char *const *defaults = olsr_cnf->ip_version == AF_INET ? deny_ipv4_defaults : deny_ipv6_defaults;
00103
00104
00105 avl_init(&filter_tree, avl_comp_default, false, NULL);
00106
00107 if (*defaults) {
00108 OLSR_INFO(LOG_NETWORKING, "Initializing invalid IP list.\n");
00109 }
00110
00111 for (; *defaults != NULL; defaults++) {
00112 union olsr_ip_addr addr;
00113 if (inet_pton(olsr_cnf->ip_version, *defaults, &addr) <= 0) {
00114 OLSR_WARN(LOG_NETWORKING, "Error converting fixed IP %s for deny rule!!\n", *defaults);
00115 continue;
00116 }
00117 olsr_add_invalid_address(&addr);
00118 }
00119
00120 buffer_hold_timer_info = olsr_timer_add("Buffer writeback", olsr_expire_buffer_timer, false);
00121 }
00122
00126 static void
00127 olsr_expire_buffer_timer(void *context)
00128 {
00129 struct interface *ifn;
00130
00131 ifn = (struct interface *)context;
00132
00133
00134
00135
00136
00137
00138 ifn->buffer_hold_timer = NULL;
00139
00140
00141
00142
00143 if (!net_output_pending(ifn)) {
00144 return;
00145 }
00146
00147 OLSR_DEBUG(LOG_NETWORKING, "Buffer Holdtimer for %s timed out, sending data.\n", ifn->int_name);
00148
00149 net_output(ifn);
00150 }
00151
00152
00153
00154
00155
00156
00157
00158 void
00159 set_buffer_timer(struct interface *ifn)
00160 {
00161
00162
00163
00164
00165 if (ifn->buffer_hold_timer) {
00166 return;
00167 }
00168
00169
00170
00171
00172
00173 ifn->buffer_hold_timer =
00174 olsr_timer_start(OLSR_BUFFER_HOLD_TIME, OLSR_BUFFER_HOLD_JITTER,
00175 ifn, buffer_hold_timer_info);
00176 }
00177
00178 void
00179 deinit_netfilters(void)
00180 {
00181 struct filter_entry *filter, *iterator;
00182 OLSR_FOR_ALL_FILTERS(filter, iterator) {
00183 avl_delete(&filter_tree, &filter->filter_node);
00184 free(filter);
00185 }
00186 }
00187
00198 int
00199 net_add_buffer(struct interface *ifp)
00200 {
00201
00202
00203
00204 if (ifp->netbuf.bufsize != ifp->int_mtu && ifp->netbuf.buff != NULL) {
00205 free(ifp->netbuf.buff);
00206 ifp->netbuf.buff = NULL;
00207 }
00208
00209 if (ifp->netbuf.buff == NULL) {
00210 ifp->netbuf.buff = olsr_malloc(ifp->int_mtu, "add_netbuff");
00211 }
00212
00213
00214 ifp->netbuf.bufsize = ifp->int_mtu;
00215 ifp->netbuf.maxsize = ifp->int_mtu - OLSR_HEADERSIZE;
00216
00217 ifp->netbuf.pending = 0;
00218 ifp->netbuf.reserved = 0;
00219
00220 return 0;
00221 }
00222
00231 void
00232 net_remove_buffer(struct interface *ifp)
00233 {
00234
00235 if (ifp->netbuf.pending != 0) {
00236 net_output(ifp);
00237 }
00238 free(ifp->netbuf.buff);
00239 ifp->netbuf.buff = NULL;
00240 }
00241
00242 #if 0
00243
00258 int
00259 net_reserve_bufspace(struct interface *ifp, int size)
00260 {
00261 if (size > ifp->netbuf.maxsize) {
00262 return -1;
00263 }
00264 ifp->netbuf.reserved = size;
00265 ifp->netbuf.maxsize -= size;
00266
00267 return 0;
00268 }
00269 #endif
00270
00282 int
00283 net_outbuffer_push(struct interface *ifp, const void *data, const uint16_t size)
00284 {
00285 if (ifp->netbuf.pending + size > ifp->netbuf.maxsize) {
00286 return 0;
00287 }
00288 memcpy(&ifp->netbuf.buff[ifp->netbuf.pending + OLSR_HEADERSIZE], data, size);
00289 ifp->netbuf.pending += size;
00290
00291 return size;
00292 }
00293
00294 #if 0
00295
00307 int
00308 net_outbuffer_push_reserved(struct interface *ifp, const void *data, const uint16_t size)
00309 {
00310 if (ifp->netbuf.pending + size > ifp->netbuf.maxsize + ifp->netbuf.reserved) {
00311 return 0;
00312 }
00313 memcpy(&ifp->netbuf.buff[ifp->netbuf.pending + OLSR_HEADERSIZE], data, size);
00314 ifp->netbuf.pending += size;
00315
00316 return size;
00317 }
00318 #endif
00319
00328 void
00329 add_ptf(packet_transform_function f)
00330 {
00331 struct ptf *new_ptf = olsr_malloc(sizeof(struct ptf), "Add PTF");
00332
00333 new_ptf->function = f;
00334 new_ptf->next = ptf_list;
00335 ptf_list = new_ptf;
00336 }
00337
00338 #if 0
00339
00348 int
00349 del_ptf(packet_transform_function f)
00350 {
00351 struct ptf *prev, *tmp_ptf;
00352 for (prev = NULL, tmp_ptf = ptf_list; tmp_ptf != NULL; prev = tmp_ptf, tmp_ptf = tmp_ptf->next) {
00353 if (tmp_ptf->function == f) {
00354
00355 if (prev == NULL) {
00356 ptf_list = tmp_ptf->next;
00357 } else {
00358 prev->next = tmp_ptf->next;
00359 }
00360 free(tmp_ptf);
00361 return 1;
00362 }
00363 }
00364 return 0;
00365 }
00366 #endif
00367
00375 int
00376 net_output(struct interface *ifp)
00377 {
00378 struct ptf *tmp_ptf;
00379 struct olsr_packet *outmsg;
00380 int retval;
00381
00382 if (ifp->netbuf.pending == 0) {
00383 return 0;
00384 }
00385
00386 ifp->netbuf.pending += OLSR_HEADERSIZE;
00387
00388 retval = ifp->netbuf.pending;
00389
00390 outmsg = (struct olsr_packet *)ifp->netbuf.buff;
00391
00392 outmsg->seqno = htons(ifp->olsr_seqnum++);
00393
00394 outmsg->size = htons(ifp->netbuf.pending);
00395
00396
00397
00398
00399 for (tmp_ptf = ptf_list; tmp_ptf != NULL; tmp_ptf = tmp_ptf->next) {
00400 tmp_ptf->function(ifp->netbuf.buff, &ifp->netbuf.pending);
00401 }
00402
00403 if (os_sendto(ifp->send_socket_fd, ifp->netbuf.buff, ifp->netbuf.pending,
00404 MSG_DONTROUTE, &ifp->int_multicast) < 0) {
00405 #if !defined REMOVE_LOG_WARN
00406 const int save_errno = errno;
00407 struct ipaddr_str buf;
00408 #endif
00409 OLSR_WARN(LOG_NETWORKING, "sending %d bytes (IPv%d) to %s:%d on interface %s/%d: %s (%d)\n",
00410 ifp->netbuf.pending, olsr_cnf->ip_version == AF_INET ? 4 : 6,
00411 olsr_sockaddr_to_string(&buf, &ifp->int_multicast), ntohs(ifp->int_multicast.v4.sin_port),
00412 ifp->int_name, ifp->if_index,
00413 strerror(save_errno), save_errno);
00414 retval = -1;
00415 }
00416
00417 ifp->netbuf.pending = 0;
00418 return retval;
00419 }
00420
00421
00422
00423
00424 static void
00425 olsr_add_invalid_address(const union olsr_ip_addr *addr)
00426 {
00427 #if !defined REMOVE_LOG_INFO
00428 struct ipaddr_str buf;
00429 #endif
00430 struct filter_entry *filter;
00431
00432
00433
00434
00435 if (!olsr_validate_address(addr)) {
00436 return;
00437 }
00438
00439 filter = olsr_malloc(sizeof(struct filter_entry), "Add filter address");
00440
00441 filter->filter_addr = *addr;
00442 filter->filter_node.key = &filter->filter_addr;
00443 avl_insert(&filter_tree, &filter->filter_node);
00444
00445 OLSR_INFO(LOG_NETWORKING, "Added %s to filter set\n", olsr_ip_to_string(&buf, &filter->filter_addr));
00446 }
00447
00448
00449 bool
00450 olsr_validate_address(const union olsr_ip_addr *addr)
00451 {
00452 if (avl_find(&filter_tree, addr)) {
00453 #if !defined REMOVE_LOG_DEBUG
00454 struct ipaddr_str buf;
00455 #endif
00456 OLSR_DEBUG(LOG_NETWORKING, "Validation of address %s failed!\n", olsr_ip_to_string(&buf, addr));
00457 return false;
00458 }
00459 return true;
00460 }
00461
00462
00463
00464
00465
00466
00467