summaryrefslogtreecommitdiffstats
path: root/proto/ospf/lsalib.c
blob: 72ca55c34b06e67ad68512641029ad3522b8155c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/*
 *	BIRD -- OSPF
 *
 *	(c) 1999-2000 Ondrej Filip <feela@network.cz>
 *
 *	Can be freely distributed and used under the terms of the GNU GPL.
 */

#include "ospf.h"

void
htonlsah(struct ospf_lsa_header *h, struct ospf_lsa_header *n)
{
  n->age=htons(h->age);
  n->options=h->options;
  n->type=h->type;
  n->id=htonl(h->id);
  n->rt=htonl(h->rt);
  n->sn=htonl(h->sn);
  n->checksum=htons(h->checksum);
  n->length=htons(h->length);
};

void
ntohlsah(struct ospf_lsa_header *n, struct ospf_lsa_header *h)
{
  h->age=ntohs(n->age);
  h->options=n->options;
  h->type=n->type;
  h->id=ntohl(n->id);
  h->rt=ntohl(n->rt);
  h->sn=ntohl(n->sn);
  h->checksum=ntohs(n->checksum);
  h->length=ntohs(n->length);
};

void
htonlsab(void *h, void *n, u8 type, u16 len)
{
  unsigned int i;
  switch(type)
  {
    case LSA_T_RT:
    {
      struct ospf_lsa_rt *hrt, *nrt;
      struct ospf_lsa_rt_link *hrtl,*nrtl;
      u16 links;

      nrt=n;
      hrt=h;
      links=hrt->links;

      nrt->VEB=hrt->VEB;
      nrt->padding=0;
      nrt->links=htons(hrt->links);
      nrtl=(struct ospf_lsa_rt_link *)(nrt+1);
      hrtl=(struct ospf_lsa_rt_link *)(hrt+1);
      for(i=0;i<links;i++)
      {
        (nrtl+i)->id=htonl((hrtl+i)->id);
        (nrtl+i)->data=htonl((hrtl+i)->data);
        (nrtl+i)->type=(hrtl+i)->type;
        (nrtl+i)->notos=(hrtl+i)->notos;
        (nrtl+i)->metric=htons((hrtl+i)->metric);
      }
      break;
    }
    case LSA_T_NET:
    {
      u32 *hid,*nid;

      nid=n;
      hid=h;

      for(i=0;i<(len/sizeof(u32));i++)
      {
        *(nid+i)=htonl(*(hid+i));
      }
      break;
    }
    case LSA_T_SUM_NET:
    case LSA_T_SUM_RT:
    {
      struct ospf_lsa_summ *hs, *ns;
      struct ospf_lsa_summ_net *hn, *nn;

      hs=h;
      ns=n;

      ns->netmask=htonl(hs->netmask);

      hn=(struct ospf_lsa_summ_net *)(hs+1);
      nn=(struct ospf_lsa_summ_net *)(ns+1);

      for(i=0;i<((len-sizeof(struct ospf_lsa_summ))/
        sizeof(struct ospf_lsa_summ_net));i++)
      {
        (nn+i)->tos=(hn+i)->tos;
	(nn+i)->metric=htons((hn+i)->metric);
	(nn+i)->padding=0;
      }
      break;
    }
    case LSA_T_EXT:
    {
      struct ospf_lsa_ext *he, *ne;
      struct ospf_lsa_ext_tos *ht, *nt;

      he=h;
      ne=n;

      ne->netmask=htonl(he->netmask);

      ht=(struct ospf_lsa_ext_tos *)(he+1);
      nt=(struct ospf_lsa_ext_tos *)(ne+1);

      for(i=0;i<((len-sizeof(struct ospf_lsa_ext))/
        sizeof(struct ospf_lsa_ext_tos));i++)
      {
        (nt+i)->etos=(ht+i)->etos;
        (nt+i)->padding=0;
        (nt+i)->metric=htons((ht+i)->metric);
        (nt+i)->fwaddr=htonl((ht+i)->fwaddr);
        (nt+i)->tag=htonl((ht+i)->tag);
      }
      break;
    }
    default: die("(hton): Unknown LSA\n");
  }
};

void
ntohlsab(void *n, void *h, u8 type, u16 len)
{
  unsigned int i;
  switch(type)
  {
    case LSA_T_RT:
    {
      struct ospf_lsa_rt *hrt, *nrt;
      struct ospf_lsa_rt_link *hrtl,*nrtl;
      u16 links;

      nrt=n;
      hrt=h;

      hrt->VEB=nrt->VEB;
      hrt->padding=0;
      links=hrt->links=ntohs(nrt->links);
      nrtl=(struct ospf_lsa_rt_link *)(nrt+1);
      hrtl=(struct ospf_lsa_rt_link *)(hrt+1);
      for(i=0;i<links;i++)
      {
        (hrtl+i)->id=ntohl((nrtl+i)->id);
        (hrtl+i)->data=ntohl((nrtl+i)->data);
        (hrtl+i)->type=(nrtl+i)->type;
        (hrtl+i)->notos=(nrtl+i)->notos;
        (hrtl+i)->metric=ntohs((nrtl+i)->metric);
      }
      break;
    }
    case LSA_T_NET:
    {
      u32 *hid,*nid;

      hid=h;
      nid=n;

      for(i=0;i<(len/sizeof(u32));i++)
      {
        *(hid+i)=ntohl(*(nid+i));
      }
      break;
    }
    case LSA_T_SUM_NET:
    case LSA_T_SUM_RT:
    {
      struct ospf_lsa_summ *hs, *ns;
      struct ospf_lsa_summ_net *hn, *nn;

      hs=h;
      ns=n;

      hs->netmask=ntohl(ns->netmask);

      hn=(struct ospf_lsa_summ_net *)(hs+1);
      nn=(struct ospf_lsa_summ_net *)(ns+1);

      for(i=0;i<((len-sizeof(struct ospf_lsa_summ))/
        sizeof(struct ospf_lsa_summ_net));i++)
      {
        (hn+i)->tos=(nn+i)->tos;
	(hn+i)->metric=ntohs((nn+i)->metric);
	(hn+i)->padding=0;
      }
      break;
    }
    case LSA_T_EXT:
    {
      struct ospf_lsa_ext *he, *ne;
      struct ospf_lsa_ext_tos *ht, *nt;

      he=h;
      ne=n;

      he->netmask=ntohl(ne->netmask);

      ht=(struct ospf_lsa_ext_tos *)(he+1);
      nt=(struct ospf_lsa_ext_tos *)(ne+1);

      for(i=0;i<((len-sizeof(struct ospf_lsa_ext))/
        sizeof(struct ospf_lsa_ext_tos));i++)
      {
        (ht+i)->etos=(nt+i)->etos;
        (ht+i)->padding=0;
        (ht+i)->metric=ntohs((nt+i)->metric);
        (ht+i)->fwaddr=ntohl((nt+i)->fwaddr);
        (ht+i)->tag=ntohl((nt+i)->tag);
      }
      break;
    }
    default: die("(ntoh): Unknown LSA\n");
  }
};

#define MODX 4102		/* larges signed value without overflow */

/* Fletcher Checksum -- Refer to RFC1008. */
#define MODX                 4102
#define LSA_CHECKSUM_OFFSET    15

/* FIXME This is VERY uneficient, I have huge endianity problems */

void
lsasum_calculate(struct ospf_lsa_header *h,void *body,struct proto_ospf *po)
{
  u8 *sp, *ep, *p, *q, *b;
  int c0 = 0, c1 = 0;
  int x, y;
  u16 length;

  h->checksum = 0;
  b=body;
  length = h->length-2;
  sp = (char *) &h->options;

  htonlsah(h,h);
  htonlsab(b,b,h->type,length+2);

  for (ep = sp + length; sp < ep; sp = q)
    {
      q = sp + MODX;
      if (q > ep)
        q = ep;
      for (p = sp; p < q; p++)
        {
          if(p<(u8 *)(h+1))
	  {
            c0 += *p;
	    /*DBG("XXXXXX: %u\t%u\n", p-sp, *p);*/
	  }
	  else
          {
            c0 += *(b+(p-sp)-sizeof(struct ospf_lsa_header)+2);
	    /*DBG("YYYYYY: %u\t%u\n", p-sp,*(b+(p-sp)-sizeof(struct ospf_lsa_header)+2));*/
          }
          c1 += c0;
        }
      c0 %= 255;
      c1 %= 255;
    }

  x = ((length - LSA_CHECKSUM_OFFSET) * c0 - c1) % 255;
  if (x <= 0)
    x += 255;
  y = 510 - c0 - x;
  if (y > 255)
    y -= 255;

  h->checksum = x + (y << 8);
  
  ntohlsah(h,h);
  ntohlsab(b,b,h->type,length+2);
}

int
lsa_comp(struct ospf_lsa_header *l1, struct ospf_lsa_header *l2)
			/* Return codes form view of l1 */
{
  if(l1->sn<l2->sn) return CMP_NEWER;
    if(l1->sn==l2->sn)
    {
      if(l1->checksum=!l2->checksum)
        return l1->checksum<l2->checksum ? CMP_OLDER : CMP_NEWER;
      if(l1->age==MAXAGE) return CMP_NEWER;
      if(l2->age==MAXAGE) return CMP_OLDER;
      if(abs(l1->age-l2->age)>MAXAGEDIFF)
        return l1->age<l2->age ? CMP_NEWER : CMP_OLDER;
    }
    return CMP_SAME;
}