Statistics
| Branch: | Revision:

iof-bird-daemon / proto / radv / packets.c @ 153f02da

History | View | Annotate | Download (9.61 KB)

1
/*
2
 *        BIRD -- RAdv Packet Processing
3
 *
4
 *
5
 *        Can be freely distributed and used under the terms of the GNU GPL.
6
 */
7

    
8

    
9
#include <stdlib.h>
10
#include "radv.h"
11

    
12
struct radv_ra_packet
13
{
14
  u8 type;
15
  u8 code;
16
  u16 checksum;
17
  u8 current_hop_limit;
18
  u8 flags;
19
  u16 router_lifetime;
20
  u32 reachable_time;
21
  u32 retrans_timer;
22
};
23

    
24
#define OPT_RA_MANAGED 0x80
25
#define OPT_RA_OTHER_CFG 0x40
26

    
27
#define OPT_PREFIX        3
28
#define OPT_MTU                5
29
#define OPT_RDNSS        25
30
#define OPT_DNSSL        31
31

    
32
struct radv_opt_prefix
33
{
34
  u8 type;
35
  u8 length;
36
  u8 pxlen;
37
  u8 flags;
38
  u32 valid_lifetime;
39
  u32 preferred_lifetime;
40
  u32 reserved;
41
  ip6_addr prefix;
42
};
43

    
44
#define OPT_PX_ONLINK 0x80
45
#define OPT_PX_AUTONOMOUS 0x40
46

    
47
struct radv_opt_mtu
48
{
49
  u8 type;
50
  u8 length;
51
  u16 reserved;
52
  u32 mtu;
53
};
54

    
55
struct radv_opt_rdnss
56
{
57
  u8 type;
58
  u8 length;
59
  u16 reserved;
60
  u32 lifetime;
61
  ip6_addr servers[];
62
};
63

    
64
struct radv_opt_dnssl
65
{
66
  u8 type;
67
  u8 length;
68
  u16 reserved;
69
  u32 lifetime;
70
  char domain[];
71
};
72

    
73

    
74
static struct radv_prefix_config default_prefix = {
75
  .onlink = 1,
76
  .autonomous = 1,
77
  .valid_lifetime = DEFAULT_VALID_LIFETIME,
78
  .preferred_lifetime = DEFAULT_PREFERRED_LIFETIME
79
};
80

    
81

    
82
static struct radv_prefix_config *
83
radv_prefix_match(struct radv_iface *ifa, struct ifa *a)
84
{
85
  struct radv_proto *p = ifa->ra;
86
  struct radv_config *cf = (struct radv_config *) (p->p.cf);
87
  struct radv_prefix_config *pc;
88

    
89
  if (a->scope <= SCOPE_LINK)
90
    return NULL;
91

    
92
  WALK_LIST(pc, ifa->cf->pref_list)
93
    if (net_in_netX(&a->prefix, (net_addr *) &pc->prefix))
94
      return pc;
95

    
96
  WALK_LIST(pc, cf->pref_list)
97
    if (net_in_netX(&a->prefix, (net_addr *) &pc->prefix))
98
      return pc;
99

    
100
  return &default_prefix;
101
}
102

    
103
static int
104
radv_prepare_rdnss(struct radv_iface *ifa, list *rdnss_list, char **buf, char *bufend)
105
{
106
  struct radv_rdnss_config *rcf = HEAD(*rdnss_list);
107

    
108
  while(NODE_VALID(rcf))
109
  {
110
    struct radv_rdnss_config *rcf_base = rcf;
111
    struct radv_opt_rdnss *op = (void *) *buf;
112
    int max_i = (bufend - *buf - sizeof(struct radv_opt_rdnss)) / sizeof(ip6_addr);
113
    int i = 0;
114

    
115
    if (max_i < 1)
116
      goto too_much;
117

    
118
    op->type = OPT_RDNSS;
119
    op->reserved = 0;
120

    
121
    if (rcf->lifetime_mult)
122
      op->lifetime = htonl(rcf->lifetime_mult * ifa->cf->max_ra_int);
123
    else
124
      op->lifetime = htonl(rcf->lifetime);
125

    
126
    while(NODE_VALID(rcf) &&
127
          (rcf->lifetime == rcf_base->lifetime) &&
128
          (rcf->lifetime_mult == rcf_base->lifetime_mult))
129
      {
130
        if (i >= max_i)
131
          goto too_much;
132

    
133
        op->servers[i] = ip6_hton(rcf->server);
134
        i++;
135

    
136
        rcf = NODE_NEXT(rcf);
137
      }
138

    
139
    op->length = 1+2*i;
140
    *buf += 8 * op->length;
141
  }
142

    
143
  return 0;
144

    
145
 too_much:
146
  log(L_WARN "%s: Too many RA options on interface %s",
147
      ifa->ra->p.name, ifa->iface->name);
148
  return -1;
149
}
150

    
151
int
152
radv_process_domain(struct radv_dnssl_config *cf)
153
{
154
  /* Format of domain in search list is <size> <label> <size> <label> ... 0 */
155

    
156
  char *dom = cf->domain;
157
  char *dom_end = dom; /* Just to  */
158
  u8 *dlen_save = &cf->dlen_first;
159
  uint len;
160

    
161
  while (dom_end)
162
  {
163
    dom_end = strchr(dom, '.');
164
    len = dom_end ? (uint)(dom_end - dom) : strlen(dom);
165

    
166
    if (len < 1 || len > 63)
167
      return -1;
168

    
169
    *dlen_save = len;
170
    dlen_save = (u8 *) dom_end;
171

    
172
    dom += len + 1;
173
  }
174

    
175
  len = dom - cf->domain;
176
  if (len > 254)
177
    return -1;
178

    
179
  cf->dlen_all = len;
180

    
181
  return 0;
182
}
183

    
184
static int
185
radv_prepare_dnssl(struct radv_iface *ifa, list *dnssl_list, char **buf, char *bufend)
186
{
187
  struct radv_dnssl_config *dcf = HEAD(*dnssl_list);
188

    
189
  while(NODE_VALID(dcf))
190
  {
191
    struct radv_dnssl_config *dcf_base = dcf;
192
    struct radv_opt_dnssl *op = (void *) *buf;
193
    int bsize = bufend - *buf - sizeof(struct radv_opt_dnssl);
194
    int bpos = 0;
195

    
196
    if (bsize < 0)
197
      goto too_much;
198

    
199
    bsize = bsize & ~7; /* Round down to multiples of 8 */
200

    
201
    op->type = OPT_DNSSL;
202
    op->reserved = 0;
203

    
204
    if (dcf->lifetime_mult)
205
      op->lifetime = htonl(dcf->lifetime_mult * ifa->cf->max_ra_int);
206
    else
207
      op->lifetime = htonl(dcf->lifetime);
208

    
209
    while(NODE_VALID(dcf) &&
210
          (dcf->lifetime == dcf_base->lifetime) &&
211
          (dcf->lifetime_mult == dcf_base->lifetime_mult))
212
      {
213
        if (bpos + dcf->dlen_all + 1 > bsize)
214
          goto too_much;
215

    
216
        op->domain[bpos++] = dcf->dlen_first;
217
        memcpy(op->domain + bpos, dcf->domain, dcf->dlen_all);
218
        bpos += dcf->dlen_all;
219

    
220
        dcf = NODE_NEXT(dcf);
221
      }
222

    
223
    int blen = (bpos + 7) / 8;
224
    bzero(op->domain + bpos, 8 * blen - bpos);
225
    op->length = 1 + blen;
226
    *buf += 8 * op->length;
227
  }
228

    
229
  return 0;
230

    
231
 too_much:
232
  log(L_WARN "%s: Too many RA options on interface %s",
233
      ifa->ra->p.name, ifa->iface->name);
234
  return -1;
235
}
236

    
237
static void
238
radv_prepare_ra(struct radv_iface *ifa)
239
{
240
  struct radv_proto *p = ifa->ra;
241
  struct radv_config *cf = (struct radv_config *) (p->p.cf);
242
  struct radv_iface_config *ic = ifa->cf;
243

    
244
  char *buf = ifa->sk->tbuf;
245
  char *bufstart = buf;
246
  char *bufend = buf + ifa->sk->tbsize;
247

    
248
  struct radv_ra_packet *pkt = (void *) buf;
249
  pkt->type = ICMPV6_RA;
250
  pkt->code = 0;
251
  pkt->checksum = 0;
252
  pkt->current_hop_limit = ic->current_hop_limit;
253
  pkt->router_lifetime = (p->active || !ic->default_lifetime_sensitive) ?
254
    htons(ic->default_lifetime) : 0;
255
  pkt->flags = (ic->managed ? OPT_RA_MANAGED : 0) |
256
    (ic->other_config ? OPT_RA_OTHER_CFG : 0) |
257
    (pkt->router_lifetime ? ic->default_preference : 0);
258
  pkt->reachable_time = htonl(ic->reachable_time);
259
  pkt->retrans_timer = htonl(ic->retrans_timer);
260
  buf += sizeof(*pkt);
261

    
262
  if (ic->link_mtu)
263
  {
264
    struct radv_opt_mtu *om = (void *) buf;
265
    om->type = OPT_MTU;
266
    om->length = 1;
267
    om->reserved = 0;
268
    om->mtu = htonl(ic->link_mtu);
269
    buf += sizeof (*om);
270
  }
271

    
272
  struct ifa *addr;
273
  WALK_LIST(addr, ifa->iface->addrs)
274
  {
275
    if (addr->prefix.type != NET_IP6)
276
      continue;
277

    
278
    struct radv_prefix_config *pc;
279
    pc = radv_prefix_match(ifa, addr);
280

    
281
    if (!pc || pc->skip)
282
      continue;
283

    
284
    if (buf + sizeof(struct radv_opt_prefix) > bufend)
285
    {
286
      log(L_WARN "%s: Too many prefixes on interface %s", p->p.name, ifa->iface->name);
287
      goto done;
288
    }
289

    
290
    struct radv_opt_prefix *op = (void *) buf;
291
    op->type = OPT_PREFIX;
292
    op->length = 4;
293
    op->pxlen = net6_pxlen(&addr->prefix);
294
    op->flags = (pc->onlink ? OPT_PX_ONLINK : 0) |
295
      (pc->autonomous ? OPT_PX_AUTONOMOUS : 0);
296
    op->valid_lifetime = (p->active || !pc->valid_lifetime_sensitive) ?
297
      htonl(pc->valid_lifetime) : 0;
298
    op->preferred_lifetime = (p->active || !pc->preferred_lifetime_sensitive) ?
299
      htonl(pc->preferred_lifetime) : 0;
300
    op->reserved = 0;
301
    op->prefix = ip6_hton(net6_prefix(&addr->prefix));
302
    buf += sizeof(*op);
303
  }
304

    
305
  if (! ic->rdnss_local)
306
    if (radv_prepare_rdnss(ifa, &cf->rdnss_list, &buf, bufend) < 0)
307
      goto done;
308

    
309
  if (radv_prepare_rdnss(ifa, &ic->rdnss_list, &buf, bufend) < 0)
310
    goto done;
311

    
312
  if (! ic->dnssl_local)
313
    if (radv_prepare_dnssl(ifa, &cf->dnssl_list, &buf, bufend) < 0)
314
      goto done;
315

    
316
  if (radv_prepare_dnssl(ifa, &ic->dnssl_list, &buf, bufend) < 0)
317
    goto done;
318

    
319
 done:
320
  ifa->plen = buf - bufstart;
321
}
322

    
323

    
324
void
325
radv_send_ra(struct radv_iface *ifa, int shutdown)
326
{
327
  struct radv_proto *p = ifa->ra;
328

    
329
  /* We store prepared RA in tbuf */
330
  if (!ifa->plen)
331
    radv_prepare_ra(ifa);
332

    
333
  if (shutdown)
334
  {
335
    /*
336
     * Modify router lifetime to 0, it is not restored because we suppose that
337
     * the iface will be removed. The preference value also has to be zeroed.
338
     * (RFC 4191 2.2: If router lifetime is 0, the preference value must be 0.)
339
     */
340

    
341
    struct radv_ra_packet *pkt = (void *) ifa->sk->tbuf;
342
    pkt->router_lifetime = 0;
343
    pkt->flags &= ~RA_PREF_MASK;
344
  }
345

    
346
  RADV_TRACE(D_PACKETS, "Sending RA via %s", ifa->iface->name);
347
  sk_send_to(ifa->sk, ifa->plen, IP6_ALL_NODES, 0);
348
}
349

    
350

    
351
static int
352
radv_rx_hook(sock *sk, uint size)
353
{
354
  struct radv_iface *ifa = sk->data;
355
  struct radv_proto *p = ifa->ra;
356

    
357
  /* We want just packets from sk->iface */
358
  if (sk->lifindex != sk->iface->index)
359
    return 1;
360

    
361
  if (ipa_equal(sk->faddr, sk->saddr))
362
    return 1;
363

    
364
  if (size < 8)
365
    return 1;
366

    
367
  byte *buf = sk->rbuf;
368

    
369
  if (buf[1] != 0)
370
    return 1;
371

    
372
  /* Validation is a bit sloppy - Hop Limit is not checked and
373
     length of options is ignored for RS and left to later for RA */
374

    
375
  switch (buf[0])
376
  {
377
  case ICMPV6_RS:
378
    RADV_TRACE(D_PACKETS, "Received RS from %I via %s",
379
               sk->faddr, ifa->iface->name);
380
    radv_iface_notify(ifa, RA_EV_RS);
381
    return 1;
382

    
383
  case ICMPV6_RA:
384
    RADV_TRACE(D_PACKETS, "Received RA from %I via %s",
385
               sk->faddr, ifa->iface->name);
386
    /* FIXME - there should be some checking of received RAs, but we just ignore them */
387
    return 1;
388

    
389
  default:
390
    return 1;
391
  }
392
}
393

    
394
static void
395
radv_tx_hook(sock *sk)
396
{
397
  struct radv_iface *ifa = sk->data;
398
  log(L_WARN "%s: TX hook called", ifa->ra->p.name);
399
}
400

    
401
static void
402
radv_err_hook(sock *sk, int err)
403
{
404
  struct radv_iface *ifa = sk->data;
405
  log(L_ERR "%s: Socket error on %s: %M", ifa->ra->p.name, ifa->iface->name, err);
406
}
407

    
408
int
409
radv_sk_open(struct radv_iface *ifa)
410
{
411
  sock *sk = sk_new(ifa->ra->p.pool);
412
  sk->type = SK_IP;
413
  sk->subtype = SK_IPV6;
414
  sk->dport = ICMPV6_PROTO;
415
  sk->saddr = ifa->addr->ip;
416

    
417
  sk->ttl = 255; /* Mandatory for Neighbor Discovery packets */
418
  sk->rx_hook = radv_rx_hook;
419
  sk->tx_hook = radv_tx_hook;
420
  sk->err_hook = radv_err_hook;
421
  sk->iface = ifa->iface;
422
  sk->rbsize = 1024; // bufsize(ifa);
423
  sk->tbsize = 1024; // bufsize(ifa);
424
  sk->data = ifa;
425
  sk->flags = SKF_LADDR_RX;
426

    
427
  if (sk_open(sk) < 0)
428
    goto err;
429

    
430
  /* We want listen just to ICMPv6 messages of type RS and RA */
431
  if (sk_set_icmp6_filter(sk, ICMPV6_RS, ICMPV6_RA) < 0)
432
    goto err;
433

    
434
  if (sk_setup_multicast(sk) < 0)
435
    goto err;
436

    
437
  if (sk_join_group(sk, IP6_ALL_ROUTERS) < 0)
438
    goto err;
439

    
440
  ifa->sk = sk;
441
  return 1;
442

    
443
 err:
444
  sk_log_error(sk, ifa->ra->p.name);
445
  rfree(sk);
446
  return 0;
447
}
448