Statistics
| Branch: | Revision:

iof-bird-daemon / proto / ospf / packet.c @ 29239ba2

History | View | Annotate | Download (12.6 KB)

1 4364b47e Ondrej Filip
/*
2
 *        BIRD -- OSPF
3
 *
4 e6ea2e37 Ondrej Filip
 *        (c) 1999--2005 Ondrej Filip <feela@network.cz>
5 70945cb6 Ondrej Zajicek
 *        (c) 2009--2014 Ondrej Zajicek <santiago@crfreenet.org>
6
 *        (c) 2009--2014 CZ.NIC z.s.p.o.
7 4364b47e Ondrej Filip
 *
8
 *        Can be freely distributed and used under the terms of the GNU GPL.
9
 */
10
11
#include "ospf.h"
12 3e2bd0f1 Ondrej Filip
#include "nest/password.h"
13
#include "lib/md5.h"
14 de2a27e2 Ondrej Zajicek (work)
#include "lib/mac.h"
15 33b4f40a Pavel TvrdĂ­k
#include "lib/socket.h"
16 4364b47e Ondrej Filip
17
void
18 3e2bd0f1 Ondrej Filip
ospf_pkt_fill_hdr(struct ospf_iface *ifa, void *buf, u8 h_type)
19 4364b47e Ondrej Filip
{
20 70945cb6 Ondrej Zajicek
  struct ospf_proto *p = ifa->oa->po;
21 4364b47e Ondrej Filip
  struct ospf_packet *pkt;
22
23 00bd27a1 Ondrej Filip
  pkt = (struct ospf_packet *) buf;
24 4364b47e Ondrej Filip
25 70945cb6 Ondrej Zajicek
  pkt->version = ospf_get_version(p);
26 00bd27a1 Ondrej Filip
  pkt->type = h_type;
27 70945cb6 Ondrej Zajicek
  pkt->length = htons(ospf_pkt_maxsize(ifa));
28
  pkt->routerid = htonl(p->router_id);
29 3b16080c Ondrej Filip
  pkt->areaid = htonl(ifa->oa->areaid);
30 00bd27a1 Ondrej Filip
  pkt->checksum = 0;
31 70945cb6 Ondrej Zajicek
  pkt->instance_id = ifa->instance_id;
32
  pkt->autype = ifa->autype;
33 4364b47e Ondrej Filip
}
34
35 70945cb6 Ondrej Zajicek
/* We assume OSPFv2 in ospf_pkt_finalize() */
36 c3226991 Ondrej Zajicek
static void
37 29239ba2 Ondrej Zajicek (work)
ospf_pkt_finalize(struct ospf_iface *ifa, struct ospf_packet *pkt, uint *plen)
38 4364b47e Ondrej Filip
{
39 29239ba2 Ondrej Zajicek (work)
  struct password_item *pass = NULL;
40 70945cb6 Ondrej Zajicek
  union ospf_auth *auth = (void *) (pkt + 1);
41 3e2bd0f1 Ondrej Filip
42 1155c792 Ondrej Zajicek
  pkt->checksum = 0;
43 70945cb6 Ondrej Zajicek
  pkt->autype = ifa->autype;
44
  bzero(auth, sizeof(union ospf_auth));
45 1155c792 Ondrej Zajicek
46 70945cb6 Ondrej Zajicek
  /* Compatibility note: auth may contain anything if autype is
47 1155c792 Ondrej Zajicek
     none, but nonzero values do not work with Mikrotik OSPF */
48 3e2bd0f1 Ondrej Filip
49 70945cb6 Ondrej Zajicek
  switch (ifa->autype)
50 3e2bd0f1 Ondrej Filip
  {
51 70945cb6 Ondrej Zajicek
  case OSPF_AUTH_SIMPLE:
52 29239ba2 Ondrej Zajicek (work)
    pass = password_find(ifa->passwords, 1);
53
    if (!pass)
54 70945cb6 Ondrej Zajicek
    {
55
      log(L_ERR "No suitable password found for authentication");
56
      return;
57
    }
58 29239ba2 Ondrej Zajicek (work)
    strncpy(auth->password, pass->password, sizeof(auth->password));
59 3e2bd0f1 Ondrej Filip
60 70945cb6 Ondrej Zajicek
  case OSPF_AUTH_NONE:
61
    {
62
      void *body = (void *) (auth + 1);
63 29239ba2 Ondrej Zajicek (work)
      uint blen = *plen - sizeof(struct ospf_packet) - sizeof(union ospf_auth);
64 70945cb6 Ondrej Zajicek
      pkt->checksum = ipsum_calculate(pkt, sizeof(struct ospf_packet), body, blen, NULL);
65
    }
66
    break;
67 024c310b Ondrej Zajicek
68 70945cb6 Ondrej Zajicek
  case OSPF_AUTH_CRYPT:
69 29239ba2 Ondrej Zajicek (work)
    pass = password_find(ifa->passwords, 0);
70
    if (!pass)
71 70945cb6 Ondrej Zajicek
    {
72
      log(L_ERR "No suitable password found for authentication");
73
      return;
74
    }
75 024c310b Ondrej Zajicek
76 70945cb6 Ondrej Zajicek
    /* Perhaps use random value to prevent replay attacks after
77
       reboot when system does not have independent RTC? */
78
    if (!ifa->csn)
79
    {
80
      ifa->csn = (u32) now;
81 024c310b Ondrej Zajicek
      ifa->csn_use = now;
82 70945cb6 Ondrej Zajicek
    }
83
84 742029eb Ondrej Zajicek
    /* We must have sufficient delay between sending a packet and increasing
85 70945cb6 Ondrej Zajicek
       CSN to prevent reordering of packets (in a network) with different CSNs */
86
    if ((now - ifa->csn_use) > 1)
87
      ifa->csn++;
88
89
    ifa->csn_use = now;
90
91 29239ba2 Ondrej Zajicek (work)
    uint auth_len = mac_type_length(pass->alg);
92
    byte *auth_tail = ((byte *) pkt + *plen);
93
    *plen += auth_len;
94
95
    ASSERT(*plen < ifa->sk->tbsize);
96 70945cb6 Ondrej Zajicek
97 29239ba2 Ondrej Zajicek (work)
    auth->c32.zero = 0;
98
    auth->c32.keyid = pass->id;
99
    auth->c32.len = auth_len;
100
    auth->c32.csn = htonl(ifa->csn);
101 3e2bd0f1 Ondrej Filip
102 29239ba2 Ondrej Zajicek (work)
    /* Append key for keyed hash, append padding for HMAC (RFC 5709 3.3) */
103
    if (pass->alg < ALG_HMAC)
104
      strncpy(auth_tail, pass->password, auth_len);
105
    else
106
      memset32(auth_tail, HMAC_MAGIC, auth_len / 4);
107
108
    mac_fill(pass->alg, pass->password, pass->length,
109
             (byte *) pkt, *plen, auth_tail);
110 70945cb6 Ondrej Zajicek
    break;
111
112
  default:
113
    bug("Unknown authentication type");
114 3e2bd0f1 Ondrej Filip
  }
115 4364b47e Ondrej Filip
}
116
117 f8fefde3 Ondrej Zajicek
118 70945cb6 Ondrej Zajicek
/* We assume OSPFv2 in ospf_pkt_checkauth() */
119 9831e591 Martin Mares
static int
120 3e236955 Jan Moskyto Matejka
ospf_pkt_checkauth(struct ospf_neighbor *n, struct ospf_iface *ifa, struct ospf_packet *pkt, uint len)
121 c1824c4d Ondrej Filip
{
122 70945cb6 Ondrej Zajicek
  struct ospf_proto *p = ifa->oa->po;
123
  union ospf_auth *auth = (void *) (pkt + 1);
124 f8fefde3 Ondrej Zajicek
  struct password_item *pass = NULL;
125
  const char *err_dsc = NULL;
126
  uint err_val = 0;
127 3e2bd0f1 Ondrej Filip
128 70945cb6 Ondrej Zajicek
  uint plen = ntohs(pkt->length);
129
  u8 autype = pkt->autype;
130 3e2bd0f1 Ondrej Filip
131 70945cb6 Ondrej Zajicek
  if (autype != ifa->autype)
132 f8fefde3 Ondrej Zajicek
    DROP("authentication method mismatch", autype);
133 c1824c4d Ondrej Filip
134 70945cb6 Ondrej Zajicek
  switch (autype)
135 3e2bd0f1 Ondrej Filip
  {
136 70945cb6 Ondrej Zajicek
  case OSPF_AUTH_NONE:
137
    return 1;
138 4364b47e Ondrej Filip
139 70945cb6 Ondrej Zajicek
  case OSPF_AUTH_SIMPLE:
140
    pass = password_find(ifa->passwords, 1);
141
    if (!pass)
142 f8fefde3 Ondrej Zajicek
      DROP1("no password found");
143
144
    if (!password_verify(pass, auth->password, sizeof(auth->password)))
145
      DROP("wrong password", pass->id);
146 3b108f18 Ondrej Zajicek
147 70945cb6 Ondrej Zajicek
    return 1;
148 3e2bd0f1 Ondrej Filip
149 70945cb6 Ondrej Zajicek
  case OSPF_AUTH_CRYPT:
150 29239ba2 Ondrej Zajicek (work)
    pass = password_find_by_id(ifa->passwords, auth->c32.keyid);
151
    if (!pass)
152
      DROP("no suitable password found", auth->c32.keyid);
153
154
    uint auth_len = mac_type_length(pass->alg);
155 3e2bd0f1 Ondrej Filip
156 29239ba2 Ondrej Zajicek (work)
    if (plen + auth->c32.len > len)
157
      DROP("packet length mismatch", len);
158 3e2bd0f1 Ondrej Filip
159 29239ba2 Ondrej Zajicek (work)
    if (auth->c32.len != auth_len)
160
      DROP("wrong authentication length", auth->c32.len);
161
162
    u32 rcv_csn = ntohl(auth->c32.csn);
163 f8fefde3 Ondrej Zajicek
    if (n && (rcv_csn < n->csn))
164
      // DROP("lower sequence number", rcv_csn);
165 70945cb6 Ondrej Zajicek
    {
166 f8fefde3 Ondrej Zajicek
      /* We want to report both new and old CSN */
167
      LOG_PKT_AUTH("Authentication failed for nbr %R on %s - "
168
                   "lower sequence number (rcv %u, old %u)",
169
                   n->rid, ifa->ifname, rcv_csn, n->csn);
170
      return 0;
171 70945cb6 Ondrej Zajicek
    }
172
173 29239ba2 Ondrej Zajicek (work)
    byte *auth_tail = ((byte *) pkt) + plen;
174
    byte *auth_data = alloca(auth_len);
175
    memcpy(auth_data, auth_tail, auth_len);
176 c3226991 Ondrej Zajicek
177 29239ba2 Ondrej Zajicek (work)
    /* Append key for keyed hash, append padding for HMAC (RFC 5709 3.3) */
178
    if (pass->alg < ALG_HMAC)
179
      strncpy(auth_tail, pass->password, auth_len);
180
    else
181
      memset32(auth_tail, HMAC_MAGIC, auth_len / 4);
182 c3226991 Ondrej Zajicek
183 29239ba2 Ondrej Zajicek (work)
    if (!mac_verify(pass->alg, pass->password, pass->length,
184
                    (byte *) pkt, plen + auth_len, auth_data))
185
      DROP("wrong authentication code", pass->id);
186 f8fefde3 Ondrej Zajicek
187
    if (n)
188
      n->csn = rcv_csn;
189
190 70945cb6 Ondrej Zajicek
    return 1;
191 c3226991 Ondrej Zajicek
192 70945cb6 Ondrej Zajicek
  default:
193 f8fefde3 Ondrej Zajicek
    bug("Unknown authentication type");
194 70945cb6 Ondrej Zajicek
  }
195 353729f5 Ondrej Zajicek
196 f8fefde3 Ondrej Zajicek
drop:
197
  LOG_PKT_AUTH("Authentication failed for nbr %R on %s - %s (%u)",
198
               (n ? n->rid : ntohl(pkt->routerid)), ifa->ifname, err_dsc, err_val);
199
200
  return 0;
201
}
202 c3226991 Ondrej Zajicek
203 351feeb5 Ondrej Filip
/**
204
 * ospf_rx_hook
205 353729f5 Ondrej Zajicek
 * @sk: socket we received the packet.
206 8e433d6a Pavel Tvrdik
 * @len: size of the packet
207 351feeb5 Ondrej Filip
 *
208 baa5dd6c Ondrej Filip
 * This is the entry point for messages from neighbors. Many checks (like
209
 * authentication, checksums, size) are done before the packet is passed to
210 351feeb5 Ondrej Filip
 * non generic functions.
211
 */
212 4364b47e Ondrej Filip
int
213 3e236955 Jan Moskyto Matejka
ospf_rx_hook(sock *sk, uint len)
214 4364b47e Ondrej Filip
{
215 f8fefde3 Ondrej Zajicek
  /* We want just packets from sk->iface. Unfortunately, on BSD we cannot filter
216
     out other packets at kernel level and we receive all packets on all sockets */
217 e7b76b97 Ondrej Zajicek
  if (sk->lifindex != sk->iface->index)
218
    return 1;
219
220
  DBG("OSPF: RX hook called (iface %s, src %I, dst %I)\n",
221 8465dccb Ondrej Zajicek (work)
      sk->iface->name, sk->faddr, sk->laddr);
222 e7b76b97 Ondrej Zajicek
223
  /* Initially, the packet is associated with the 'master' iface */
224
  struct ospf_iface *ifa = sk->data;
225 70945cb6 Ondrej Zajicek
  struct ospf_proto *p = ifa->oa->po;
226 f8fefde3 Ondrej Zajicek
  const char *err_dsc = NULL;
227
  uint err_val = 0;
228 e7b76b97 Ondrej Zajicek
229 b5e76398 Ondrej Zajicek
  /* Should not happen */
230
  if (ifa->state <= OSPF_IS_LOOP)
231
    return 1;
232
233 742029eb Ondrej Zajicek
  int src_local, dst_local, dst_mcast;
234 ab164971 Ondrej Zajicek
  src_local = ipa_in_net(sk->faddr, ifa->addr->prefix, ifa->addr->pxlen);
235
  dst_local = ipa_equal(sk->laddr, ifa->addr->ip);
236 70945cb6 Ondrej Zajicek
  dst_mcast = ipa_equal(sk->laddr, ifa->all_routers) || ipa_equal(sk->laddr, ifa->des_routers);
237 00bd27a1 Ondrej Filip
238 70945cb6 Ondrej Zajicek
  if (ospf_is_v2(p))
239
  {
240
    /* First, we eliminate packets with strange address combinations.
241
     * In OSPFv2, they might be for other ospf_ifaces (with different IP
242
     * prefix) on the same real iface, so we don't log it. We enforce
243
     * that (src_local || dst_local), therefore we are eliminating all
244 742029eb Ondrej Zajicek
     * such cases.
245 70945cb6 Ondrej Zajicek
     */
246
    if (dst_mcast && !src_local)
247
      return 1;
248
    if (!dst_mcast && !dst_local)
249
      return 1;
250 e7b76b97 Ondrej Zajicek
251 70945cb6 Ondrej Zajicek
    /* Ignore my own broadcast packets */
252
    if (ifa->cf->real_bcast && ipa_equal(sk->faddr, ifa->addr->ip))
253
      return 1;
254
  }
255
  else
256
  {
257 742029eb Ondrej Zajicek
    /* In OSPFv3, src_local and dst_local mean link-local.
258 70945cb6 Ondrej Zajicek
     * RFC 5340 says that local (non-vlink) packets use
259
     * link-local src address, but does not enforce it. Strange.
260
     */
261
    if (dst_mcast && !src_local)
262 6f8bbaa1 Ondrej Zajicek
      LOG_PKT_WARN("Multicast packet received from non-link-local %I via %s",
263 f8fefde3 Ondrej Zajicek
                   sk->faddr, ifa->ifname);
264 70945cb6 Ondrej Zajicek
  }
265 e7b76b97 Ondrej Zajicek
266 f8fefde3 Ondrej Zajicek
  /* Second, we check packet length, checksum, and the protocol version */
267 88a183c6 Ondrej Zajicek
  struct ospf_packet *pkt = (void *) sk_rx_buffer(sk, &len);
268
269 3b16080c Ondrej Filip
270 70945cb6 Ondrej Zajicek
  if (pkt == NULL)
271 f8fefde3 Ondrej Zajicek
    DROP("bad IP header", len);
272 0e9617e4 Ondrej Zajicek
273 05476c4d Ondrej Zajicek
  if (ifa->check_ttl && (sk->rcv_ttl < 255))
274 f8fefde3 Ondrej Zajicek
    DROP("wrong TTL", sk->rcv_ttl);
275 70e212f9 Ondrej Zajicek
276 f8fefde3 Ondrej Zajicek
  if (len < sizeof(struct ospf_packet))
277
    DROP("too short", len);
278
279
  if (pkt->version != ospf_get_version(p))
280
    DROP("version mismatch", pkt->version);
281 00bd27a1 Ondrej Filip
282 70945cb6 Ondrej Zajicek
  uint plen = ntohs(pkt->length);
283 48e5f32d Ondrej Zajicek
  if ((plen < sizeof(struct ospf_packet)) || ((plen % 4) != 0))
284 f8fefde3 Ondrej Zajicek
    DROP("invalid length", plen);
285 d600909d Ondrej Filip
286 48e5f32d Ondrej Zajicek
  if (sk->flags & SKF_TRUNCATED)
287 2e10a170 Ondrej Filip
  {
288 48e5f32d Ondrej Zajicek
    /* If we have dynamic buffers and received truncated message, we expand RX buffer */
289
290
    uint bs = plen + 256;
291
    bs = BIRD_ALIGN(bs, 1024);
292
293
    if (!ifa->cf->rx_buffer && (bs > sk->rbsize))
294
      sk_set_rbsize(sk, bs);
295
296 f8fefde3 Ondrej Zajicek
    DROP("truncated", plen);
297 2e10a170 Ondrej Filip
  }
298 00bd27a1 Ondrej Filip
299 f8fefde3 Ondrej Zajicek
  if (plen > len)
300
    DROP("length mismatch", plen);
301 00bd27a1 Ondrej Filip
302 70945cb6 Ondrej Zajicek
  if (ospf_is_v2(p) && (pkt->autype != OSPF_AUTH_CRYPT))
303 2e10a170 Ondrej Filip
  {
304 70945cb6 Ondrej Zajicek
    uint hlen = sizeof(struct ospf_packet) + sizeof(union ospf_auth);
305
    uint blen = plen - hlen;
306
    void *body = ((void *) pkt) + hlen;
307 00bd27a1 Ondrej Filip
308 f8fefde3 Ondrej Zajicek
    if (!ipsum_verify(pkt, sizeof(struct ospf_packet), body, blen, NULL))
309
      DROP1("invalid checksum");
310 70945cb6 Ondrej Zajicek
  }
311 353729f5 Ondrej Zajicek
312 e7b76b97 Ondrej Zajicek
  /* Third, we resolve associated iface and handle vlinks. */
313
314 70945cb6 Ondrej Zajicek
  u32 areaid = ntohl(pkt->areaid);
315
  u32 rid = ntohl(pkt->routerid);
316
  u8 instance_id = pkt->instance_id;
317 e7b76b97 Ondrej Zajicek
318 70945cb6 Ondrej Zajicek
  if (areaid == ifa->oa->areaid)
319 e7b76b97 Ondrej Zajicek
  {
320 70945cb6 Ondrej Zajicek
    /* Matching area ID */
321
322
    if (instance_id != ifa->instance_id)
323
      return 1;
324
325 e7b76b97 Ondrej Zajicek
    /* It is real iface, source should be local (in OSPFv2) */
326 70945cb6 Ondrej Zajicek
    if (ospf_is_v2(p) && !src_local)
327 f8fefde3 Ondrej Zajicek
      DROP1("strange source address");
328 e7b76b97 Ondrej Zajicek
329 70945cb6 Ondrej Zajicek
    goto found;
330 e7b76b97 Ondrej Zajicek
  }
331 70945cb6 Ondrej Zajicek
  else if ((areaid == 0) && !dst_mcast)
332 2e10a170 Ondrej Filip
  {
333 70945cb6 Ondrej Zajicek
    /* Backbone area ID and possible vlink packet */
334
335
    if ((p->areano == 1) || !oa_is_ext(ifa->oa))
336
      return 1;
337 e7b76b97 Ondrej Zajicek
338 70945cb6 Ondrej Zajicek
    struct ospf_iface *iff = NULL;
339
    WALK_LIST(iff, p->iface_list)
340 353729f5 Ondrej Zajicek
    {
341 742029eb Ondrej Zajicek
      if ((iff->type == OSPF_IT_VLINK) &&
342 e7b76b97 Ondrej Zajicek
          (iff->voa == ifa->oa) &&
343 70945cb6 Ondrej Zajicek
          (iff->instance_id == instance_id) &&
344 e7b76b97 Ondrej Zajicek
          (iff->vid == rid))
345 70945cb6 Ondrej Zajicek
      {
346
        /* Vlink should be UP */
347
        if (iff->state != OSPF_IS_PTP)
348
          return 1;
349
350
        ifa = iff;
351
        goto found;
352
      }
353 353729f5 Ondrej Zajicek
    }
354
355 70945cb6 Ondrej Zajicek
    /*
356
     * Cannot find matching vlink. It is either misconfigured vlink; NBMA or
357
     * PtMP with misconfigured area ID, or packet for some other instance (that
358
     * is possible even if instance_id == ifa->instance_id, because it may be
359
     * also vlink packet in the other instance, which is different namespace).
360
     */
361
362
    return 1;
363
  }
364
  else
365
  {
366
    /* Non-matching area ID but cannot be vlink packet */
367
368
    if (instance_id != ifa->instance_id)
369
      return 1;
370
371 f8fefde3 Ondrej Zajicek
    DROP("area mismatch", areaid);
372 2e10a170 Ondrej Filip
  }
373 00bd27a1 Ondrej Filip
374 70945cb6 Ondrej Zajicek
375 f8fefde3 Ondrej Zajicek
found:
376 353729f5 Ondrej Zajicek
  if (ifa->stub)            /* This shouldn't happen */
377
    return 1;
378
379 70945cb6 Ondrej Zajicek
  if (ipa_equal(sk->laddr, ifa->des_routers) && (ifa->sk_dr == 0))
380 c3226991 Ondrej Zajicek
    return 1;
381
382 70945cb6 Ondrej Zajicek
  if (rid == p->router_id)
383 f8fefde3 Ondrej Zajicek
    DROP1("my own router ID");
384 00bd27a1 Ondrej Filip
385 e7b76b97 Ondrej Zajicek
  if (rid == 0)
386 f8fefde3 Ondrej Zajicek
    DROP1("zero router ID");
387 5e3436d2 Ondrej Filip
388 f8fefde3 Ondrej Zajicek
  /* In OSPFv2, neighbors are identified by either IP or Router ID, based on network type */
389 70945cb6 Ondrej Zajicek
  uint t = ifa->type;
390 9d1ee138 Ondrej Zajicek
  struct ospf_neighbor *n;
391 70945cb6 Ondrej Zajicek
  if (ospf_is_v2(p) && ((t == OSPF_IT_BCAST) || (t == OSPF_IT_NBMA) || (t == OSPF_IT_PTMP)))
392 9d1ee138 Ondrej Zajicek
    n = find_neigh_by_ip(ifa, sk->faddr);
393
  else
394
    n = find_neigh(ifa, rid);
395 5e3436d2 Ondrej Filip
396 70945cb6 Ondrej Zajicek
  if (!n && (pkt->type != HELLO_P))
397 5e3436d2 Ondrej Filip
  {
398 6f8bbaa1 Ondrej Zajicek
    OSPF_TRACE(D_PACKETS, "Non-HELLO packet received from unknown nbr %R on %s, src %I",
399 f8fefde3 Ondrej Zajicek
               rid, ifa->ifname, sk->faddr);
400 5e3436d2 Ondrej Filip
    return 1;
401 2e10a170 Ondrej Filip
  }
402 00bd27a1 Ondrej Filip
403 f8fefde3 Ondrej Zajicek
  /* ospf_pkt_checkauth() has its own error logging */
404
  if (ospf_is_v2(p) && !ospf_pkt_checkauth(n, ifa, pkt, len))
405 3e2bd0f1 Ondrej Filip
    return 1;
406
407 70945cb6 Ondrej Zajicek
  switch (pkt->type)
408 2e10a170 Ondrej Filip
  {
409
  case HELLO_P:
410 70945cb6 Ondrej Zajicek
    ospf_receive_hello(pkt, ifa, n, sk->faddr);
411 2e10a170 Ondrej Filip
    break;
412 70945cb6 Ondrej Zajicek
413 2e10a170 Ondrej Filip
  case DBDES_P:
414 70945cb6 Ondrej Zajicek
    ospf_receive_dbdes(pkt, ifa, n);
415 2e10a170 Ondrej Filip
    break;
416 70945cb6 Ondrej Zajicek
417 2e10a170 Ondrej Filip
  case LSREQ_P:
418 70945cb6 Ondrej Zajicek
    ospf_receive_lsreq(pkt, ifa, n);
419 2e10a170 Ondrej Filip
    break;
420 70945cb6 Ondrej Zajicek
421 2e10a170 Ondrej Filip
  case LSUPD_P:
422 70945cb6 Ondrej Zajicek
    ospf_receive_lsupd(pkt, ifa, n);
423 2e10a170 Ondrej Filip
    break;
424 70945cb6 Ondrej Zajicek
425 2e10a170 Ondrej Filip
  case LSACK_P:
426 70945cb6 Ondrej Zajicek
    ospf_receive_lsack(pkt, ifa, n);
427 2e10a170 Ondrej Filip
    break;
428 70945cb6 Ondrej Zajicek
429 2e10a170 Ondrej Filip
  default:
430 f8fefde3 Ondrej Zajicek
    DROP("invalid packet type", pkt->type);
431 2e10a170 Ondrej Filip
  };
432 5e3436d2 Ondrej Filip
  return 1;
433 f8fefde3 Ondrej Zajicek
434
drop:
435
  LOG_PKT("Bad packet from %I via %s - %s (%u)",
436
          sk->faddr, ifa->ifname, err_dsc, err_val);
437
438
  return 1;
439 4364b47e Ondrej Filip
}
440
441 48e5f32d Ondrej Zajicek
/*
442 4364b47e Ondrej Filip
void
443 2e10a170 Ondrej Filip
ospf_tx_hook(sock * sk)
444 4364b47e Ondrej Filip
{
445 5c78e0e3 Ondrej Zajicek
  struct ospf_iface *ifa= (struct ospf_iface *) (sk->data);
446 70945cb6 Ondrej Zajicek
//  struct proto *p = (struct proto *) (ifa->oa->p);
447 48e5f32d Ondrej Zajicek
  log(L_ERR "OSPF: TX hook called on %s", ifa->ifname);
448 4364b47e Ondrej Filip
}
449 48e5f32d Ondrej Zajicek
*/
450 4364b47e Ondrej Filip
451
void
452 2eef9e88 Ondrej Filip
ospf_err_hook(sock * sk, int err)
453 4364b47e Ondrej Filip
{
454 5c78e0e3 Ondrej Zajicek
  struct ospf_iface *ifa= (struct ospf_iface *) (sk->data);
455 70945cb6 Ondrej Zajicek
  struct ospf_proto *p = ifa->oa->po;
456
  log(L_ERR "%s: Socket error on %s: %M", p->p.name, ifa->ifname, err);
457 48e5f32d Ondrej Zajicek
}
458
459
void
460
ospf_verr_hook(sock *sk, int err)
461
{
462 70945cb6 Ondrej Zajicek
  struct ospf_proto *p = (struct ospf_proto *) (sk->data);
463
  log(L_ERR "%s: Vlink socket error: %M", p->p.name, err);
464
}
465
466
void
467
ospf_send_to(struct ospf_iface *ifa, ip_addr dst)
468
{
469
  sock *sk = ifa->sk;
470
  struct ospf_packet *pkt = (struct ospf_packet *) sk->tbuf;
471 29239ba2 Ondrej Zajicek (work)
  uint plen = ntohs(pkt->length);
472 70945cb6 Ondrej Zajicek
473
  if (ospf_is_v2(ifa->oa->po))
474 29239ba2 Ondrej Zajicek (work)
    ospf_pkt_finalize(ifa, pkt, &plen);
475 70945cb6 Ondrej Zajicek
476
  int done = sk_send_to(sk, plen, dst, 0);
477
  if (!done)
478
    log(L_WARN "OSPF: TX queue full on %s", ifa->ifname);
479 4364b47e Ondrej Filip
}
480
481 67315ef6 Ondrej Filip
void
482 f9c799a0 Ondrej Zajicek
ospf_send_to_agt(struct ospf_iface *ifa, u8 state)
483 67315ef6 Ondrej Filip
{
484
  struct ospf_neighbor *n;
485
486 353729f5 Ondrej Zajicek
  WALK_LIST(n, ifa->neigh_list)
487
    if (n->state >= state)
488
      ospf_send_to(ifa, n->ip);
489 00bd27a1 Ondrej Filip
}
490 eb436e16 Ondrej Filip
491
void
492 f9c799a0 Ondrej Zajicek
ospf_send_to_bdr(struct ospf_iface *ifa)
493 eb436e16 Ondrej Filip
{
494 70945cb6 Ondrej Zajicek
  if (ipa_nonzero(ifa->drip))
495 f9c799a0 Ondrej Zajicek
    ospf_send_to(ifa, ifa->drip);
496 70945cb6 Ondrej Zajicek
  if (ipa_nonzero(ifa->bdrip))
497 f9c799a0 Ondrej Zajicek
    ospf_send_to(ifa, ifa->bdrip);
498 eb436e16 Ondrej Filip
}