Statistics
| Branch: | Revision:

iof-bird-daemon / proto / ospf / packet.c @ 394acced

History | View | Annotate | Download (4.26 KB)

1
/*
2
 *        BIRD -- OSPF
3
 *
4
 *        (c) 1999 Ondrej Filip <feela@network.cz>
5
 *
6
 *        Can be freely distributed and used under the terms of the GNU GPL.
7
 */
8

    
9
#include "ospf.h"
10

    
11
void
12
fill_ospf_pkt_hdr(struct ospf_iface *ifa, void *buf, u8 h_type)
13
{
14
  struct ospf_packet *pkt;
15
  struct proto *p;
16
  
17
  p=(struct proto *)(ifa->proto);
18

    
19
  pkt=(struct ospf_packet *)buf;
20

    
21
  pkt->version=OSPF_VERSION;
22

    
23
  pkt->type=h_type;
24

    
25
  pkt->routerid=htonl(p->cf->global->router_id);
26
  pkt->areaid=htonl(ifa->an);
27
  pkt->autype=htons(ifa->autype);
28
  pkt->checksum=0;
29
}
30

    
31
void
32
ospf_tx_authenticate(struct ospf_iface *ifa, struct ospf_packet *pkt)
33
{
34
  /* FIXME */
35
  return;
36
}
37

    
38
void
39
ospf_pkt_finalize(struct ospf_iface *ifa, struct ospf_packet *pkt)
40
{
41

    
42
  ospf_tx_authenticate(ifa, pkt);
43

    
44
  /* Count checksum */
45
  pkt->checksum=ipsum_calculate(pkt,sizeof(struct ospf_packet)-8,
46
    (pkt+1),ntohs(pkt->length)-sizeof(struct ospf_packet),NULL);
47
}
48

    
49
int
50
ospf_rx_hook(sock *sk, int size)
51
{
52
#ifndef IPV6
53
  struct ospf_packet *ps;
54
  struct ospf_iface *ifa;
55
  struct proto *p;
56
  int i;
57
  u8 *pu8;
58

    
59

    
60
  ifa=(struct ospf_iface *)(sk->data);
61

    
62
  p=(struct proto *)(ifa->proto);
63
  DBG("%s: RX_Hook called on interface %s.\n",p->name, sk->iface->name);
64

    
65
  ps = (struct ospf_packet *) ipv4_skip_header(sk->rbuf, &size);
66
  if(ps==NULL)
67
  {
68
    log("%s: Bad OSPF packet received: bad IP header", p->name);
69
    log("%s: Discarding",p->name);
70
    return(1);
71
  }
72
  
73
  if((unsigned)size < sizeof(struct ospf_packet))
74
  {
75
    log("%s: Bad OSPF packet received: too short (%u bytes)", p->name, size);
76
    log("%s: Discarding",p->name);
77
    return(1);
78
  }
79

    
80
  if(ntohs(ps->length) != size)
81
  {
82
    log("%s: Bad OSPF packet received: size field does not match", p->name);
83
    log("%s: Discarding",p->name);
84
    return(1);
85
  }
86

    
87
  if(ps->version!=OSPF_VERSION)
88
  {
89
    log("%s: Bad OSPF packet received: version %u", p->name, ps->version);
90
    log("%s: Discarding",p->name);
91
    return(1);
92
  }
93

    
94
  if(!ipsum_verify(ps, 16,(void *)ps+sizeof(struct ospf_packet),
95
    ntohs(ps->length)-sizeof(struct ospf_packet), NULL))
96
  {
97
    log("%s: Bad OSPF packet received: bad checksum", p->name);
98
    log("%s: Discarding",p->name);
99
    return(1);
100
  }
101

    
102
  /* FIXME: Do authetification */
103

    
104
  if(ps->areaid!=ifa->an)
105
  {
106
    log("%s: Bad OSPF packet received: other area %ld", p->name, ps->areaid);
107
    log("%s: Discarding",p->name);
108
    return(1);
109
  }
110

    
111
  if(ntohl(ps->routerid)==p->cf->global->router_id)
112
  {
113
    log("%s: Bad OSPF packet received: received my own IP!.", p->name);
114
    log("%s: Discarding",p->name);
115
    return(1);
116
  }
117

    
118
  if(ntohl(ps->routerid)==0)
119
  {
120
    log("%s: Bad OSPF packet received: Id 0.0.0.0 is not allowed.", p->name);
121
    log("%s: Discarding",p->name);
122
    return(1);
123
  }
124
  
125
  /* Dump packet */
126
  pu8=(u8 *)(sk->rbuf+5*4);
127
  for(i=0;i<ntohs(ps->length);i+=4)
128
    DBG("%s: received %u,%u,%u,%u\n",p->name, pu8[i+0], pu8[i+1], pu8[i+2],
129
                    pu8[i+3]);
130
  debug("%s: received size: %u\n",p->name,size);
131

    
132
  switch(ps->type)
133
  {
134
    case HELLO:
135
      DBG("%s: Hello received.\n", p->name);
136
      ospf_hello_rx((struct ospf_hello_packet *)ps, p, ifa, size, sk->faddr);
137
      break;
138
    case DBDES:
139
      DBG("%s: Database description received.\n", p->name);
140
      ospf_dbdes_rx((struct ospf_dbdes_packet *)ps, p, ifa, size);
141
      break;
142
    case LSREQ:
143
      DBG("%s: Link state request received.\n", p->name);
144
      ospf_lsreq_rx((struct ospf_lsreq_packet *)ps, p, ifa, size);
145
      break;
146
    case LSUPD:
147
      DBG("%s: Link state update received.\n", p->name);
148
      ospf_lsupd_rx((struct ospf_lsupd_packet *)ps, p, ifa, size);
149
      break;
150
    case LSACK:
151
      DBG("%s: Link state ack received.\n", p->name);
152
      break;
153
    default:
154
      log("%s: Bad packet received: wrong type %u", p->name, ps->type);
155
      log("%s: Discarding",p->name);
156
      return(1);
157
  };
158
  DBG("\n");
159
#else
160
#error RX_Hook does not work for IPv6 now.
161
#endif
162
  return(1);
163
}
164

    
165
void
166
ospf_tx_hook(sock *sk)
167
{
168
  struct ospf_iface *ifa;
169
  struct proto *p;
170

    
171
  ifa=(struct ospf_iface *)(sk->data);
172

    
173
  p=(struct proto *)(ifa->proto);
174
  DBG("%s: TX_Hook called on interface %s\n", p->name,sk->iface->name);
175
}
176

    
177
void
178
ospf_err_hook(sock *sk, int err)
179
{
180
  struct ospf_iface *ifa;
181
  struct proto *p;
182

    
183
  ifa=(struct ospf_iface *)(sk->data);
184

    
185
  p=(struct proto *)(ifa->proto);
186
  DBG("%s: Err_Hook called on interface %s\n", p->name,sk->iface->name);
187
}
188