First step towards making this work:
[mpls-ldp-portable.git] / ldp / ldp_pdu_setup.c
blob69f90cbf742cf4353f7e5fa74c9f151b017f0098
2 /*
3 * Copyright (C) James R. Leu 2000
4 * jleu@mindspring.com
6 * This software is covered under the LGPL, for more
7 * info check out http://www.gnu.org/copyleft/lgpl.html
8 */
10 #include "ldp_struct.h"
11 #include "ldp_pdu_setup.h"
13 void setBaseMsgId(mplsLdpMsg_t * baseMsg, unsigned int msgId)
15 baseMsg->msgId = msgId;
18 void setupBaseMsg(mplsLdpMsg_t * baseMsg, unsigned int type, int uBit,
19 unsigned int msgId)
21 baseMsg->flags.flags.msgType = type;
22 baseMsg->flags.flags.uBit = uBit;
23 baseMsg->msgLength = MPLS_MSGIDFIXLEN;
24 setBaseMsgId(baseMsg, msgId);
27 int setupChpTlv(mplsLdpChpTlv_t * chpTlv, int target, int request, int res,
28 int holdTime)
30 chpTlv->baseTlv.flags.flags.tBit = MPLS_CHP_TLVTYPE;
31 chpTlv->baseTlv.flags.flags.uBit = 0;
32 chpTlv->baseTlv.flags.flags.fBit = 0;
33 chpTlv->baseTlv.length = MPLS_CHPFIXLEN;
34 chpTlv->flags.flags.target = target;
35 chpTlv->flags.flags.request = request;
36 chpTlv->flags.flags.res = res;
37 chpTlv->holdTime = holdTime;
38 return MPLS_TLVFIXLEN + MPLS_CHPFIXLEN;
41 int setupPinningTlv(mplsLdpPinningTlv_t * pinningTlv, int pBit, int res)
43 pinningTlv->baseTlv.flags.flags.tBit = MPLS_PINNING_TLVTYPE;
44 pinningTlv->baseTlv.flags.flags.uBit = 0;
45 pinningTlv->baseTlv.flags.flags.fBit = 0;
46 pinningTlv->baseTlv.length = 4;
47 pinningTlv->flags.flags.pBit = pBit;
48 pinningTlv->flags.flags.res = res;
49 return 4 + MPLS_TLVFIXLEN;
52 int setupResClassTlv(mplsLdpResClsTlv_t * resClsTlv, unsigned int rsCls)
54 resClsTlv->baseTlv.flags.flags.tBit = MPLS_RESCLASS_TLVTYPE;
55 resClsTlv->baseTlv.flags.flags.uBit = 0;
56 resClsTlv->baseTlv.flags.flags.fBit = 0;
57 resClsTlv->baseTlv.length = 4;
58 resClsTlv->rsCls = rsCls;
59 return 4 + MPLS_TLVFIXLEN;
62 int setupPreemptTlv(mplsLdpPreemptTlv_t * preemptTlv, unsigned char setPrio,
63 unsigned char holdPrio, unsigned short res)
65 preemptTlv->baseTlv.flags.flags.tBit = MPLS_PREEMPT_TLVTYPE;
66 preemptTlv->baseTlv.flags.flags.uBit = 0;
67 preemptTlv->baseTlv.flags.flags.fBit = 0;
68 preemptTlv->baseTlv.length = MPLS_PREEMPTTLV_FIXLEN;
69 preemptTlv->setPrio = setPrio;
70 preemptTlv->holdPrio = holdPrio;
71 preemptTlv->res = res;
72 return MPLS_PREEMPTTLV_FIXLEN + MPLS_TLVFIXLEN;
75 int addErHop2ErHopTvl(mplsLdpErTlv_t * erHopTlv, mplsLdpErHop_t * erHop,
76 unsigned short type)
78 int num = erHopTlv->numberErHops;
79 int result = 0;
81 memcpy(&(erHopTlv->erHopArray[num]), erHop, sizeof(mplsLdpErHop_t));
82 erHopTlv->erHopTypes[num] = type;
83 erHopTlv->numberErHops++;
84 switch (type) {
85 case MPLS_ERHOP_IPV4_TLVTYPE:
86 result = MPLS_ERHOP_IPV4_FIXLEN;
87 break;
88 case MPLS_ERHOP_IPV6_TLVTYPE:
89 result = MPLS_ERHOP_IPV6_FIXLEN;
90 break;
91 case MPLS_ERHOP_AS_TLVTYPE:
92 result = MPLS_ERHOP_AS_FIXLEN;
93 break;
94 case MPLS_ERHOP_LSPID_TLVTYPE:
95 result = MPLS_ERHOP_LSPID_FIXLEN;
96 break;
98 return result + MPLS_TLVFIXLEN;
101 int setupErHopTlv(mplsLdpErTlv_t * erHopTlv)
103 erHopTlv->baseTlv.flags.flags.tBit = MPLS_ERHOP_IPV4_TLVTYPE;
104 erHopTlv->baseTlv.flags.flags.uBit = 0;
105 erHopTlv->baseTlv.flags.flags.fBit = 0;
106 erHopTlv->baseTlv.length = 0;
107 return MPLS_TLVFIXLEN;
110 int setupTrAddrTlv(mplsLdpTrAdrTlv_t * trAddrTlv, unsigned int trAddr)
112 trAddrTlv->baseTlv.flags.flags.tBit = MPLS_TRADR_TLVTYPE;
113 trAddrTlv->baseTlv.flags.flags.uBit = 0;
114 trAddrTlv->baseTlv.flags.flags.fBit = 0;
115 trAddrTlv->baseTlv.length = MPLS_TRADRFIXLEN;
116 trAddrTlv->address = trAddr;
117 return MPLS_TRADRFIXLEN + MPLS_TLVFIXLEN;
120 int setupCsnTlv(mplsLdpCsnTlv_t * csnTlv, unsigned int confSeqNum)
122 csnTlv->baseTlv.flags.flags.tBit = MPLS_CSN_TLVTYPE;
123 csnTlv->baseTlv.flags.flags.uBit = 0;
124 csnTlv->baseTlv.flags.flags.fBit = 0;
125 csnTlv->baseTlv.length = MPLS_CSNFIXLEN;
126 csnTlv->seqNumber = confSeqNum;
127 return MPLS_CSNFIXLEN + MPLS_TLVFIXLEN;
130 int setupCspTlv(mplsLdpCspTlv_t * cspTlv, uint16_t keepalive,
131 uint8_t adv_discp, uint8_t loop, uint8_t pvl, uint16_t mtu,
132 uint32_t remote_lsraddr, uint16_t remote_labelspace, uint32_t res)
134 cspTlv->baseTlv.flags.flags.tBit = MPLS_CSP_TLVTYPE;
135 cspTlv->baseTlv.flags.flags.uBit = 0;
136 cspTlv->baseTlv.flags.flags.fBit = 0;
137 cspTlv->baseTlv.length = MPLS_CSPFIXLEN;
138 cspTlv->protocolVersion = 1;
139 cspTlv->holdTime = keepalive;
140 cspTlv->flags.flags.lad = adv_discp;
141 cspTlv->flags.flags.ld = loop;
142 cspTlv->flags.flags.res = res;
143 cspTlv->flags.flags.pvl = pvl;
144 cspTlv->maxPduLen = mtu;
145 cspTlv->rcvLsrAddress = remote_lsraddr;
146 cspTlv->rcvLsId = remote_labelspace;
147 return MPLS_CSPFIXLEN + MPLS_TLVFIXLEN;
150 int addLblRng2AspTlv(mplsLdpAspTlv_t * aspTlv, unsigned int minvpi,
151 unsigned int minvci, unsigned int maxvpi, unsigned int maxvci)
153 int num = aspTlv->baseTlv.length / MPLS_ASPFIXLEN;
155 aspTlv->baseTlv.length += MPLS_ASPFIXLEN;
156 aspTlv->lblRngList[num].flags.flags.res1 = 0;
157 aspTlv->lblRngList[num].flags.flags.minVpi = minvpi;
158 aspTlv->lblRngList[num].flags.flags.minVci = minvci;
159 aspTlv->lblRngList[num].flags.flags.res2 = 0;
160 aspTlv->lblRngList[num].flags.flags.maxVpi = maxvpi;
161 aspTlv->lblRngList[num].flags.flags.maxVci = maxvci;
162 return MPLS_ASPFIXLEN;
165 int addLblRng2FspTlv(mplsLdpFspTlv_t * fspTlv, unsigned int resmin,
166 unsigned int len, unsigned int mindlci, unsigned int resmax,
167 unsigned int maxdlci)
169 int num = fspTlv->baseTlv.length / MPLS_FSPFIXLEN;
171 fspTlv->baseTlv.length += MPLS_FSPFIXLEN;
172 fspTlv->lblRngList[num].flags.flags.res_min = resmin;
173 fspTlv->lblRngList[num].flags.flags.len = len;
174 fspTlv->lblRngList[num].flags.flags.minDlci = mindlci;
175 fspTlv->lblRngList[num].flags.flags.res_max = resmax;
176 fspTlv->lblRngList[num].flags.flags.maxDlci = maxdlci;
177 return MPLS_FSPFIXLEN;
180 int setupAspTlv(mplsLdpAspTlv_t * aspTlv, uint8_t merge, uint8_t direction)
182 aspTlv->baseTlv.flags.flags.tBit = MPLS_ASP_TLVTYPE;
183 aspTlv->baseTlv.flags.flags.uBit = 0;
184 aspTlv->baseTlv.flags.flags.fBit = 0;
185 aspTlv->flags.flags.dir = direction;
186 aspTlv->flags.flags.mergeType = merge;
187 aspTlv->baseTlv.length = 0;
188 return MPLS_TLVFIXLEN;
191 int setupFspTlv(mplsLdpFspTlv_t * fspTlv, uint8_t merge, uint8_t direction)
193 fspTlv->baseTlv.flags.flags.tBit = MPLS_FSP_TLVTYPE;
194 fspTlv->baseTlv.flags.flags.uBit = 0;
195 fspTlv->baseTlv.flags.flags.fBit = 0;
196 fspTlv->flags.flags.dir = direction;
197 fspTlv->flags.flags.mergeType = merge;
198 fspTlv->baseTlv.length = 0;
199 return MPLS_TLVFIXLEN;
202 int setupFecTlv(mplsLdpFecTlv_t * fecTlv)
204 fecTlv->baseTlv.flags.flags.tBit = MPLS_FEC_TLVTYPE;
205 fecTlv->baseTlv.flags.flags.uBit = 0;
206 fecTlv->baseTlv.flags.flags.fBit = 0;
207 fecTlv->baseTlv.length = 0;
208 fecTlv->wcElemExists = 0;
209 fecTlv->numberFecElements = 0;
210 return MPLS_TLVFIXLEN;
214 #if 0
215 mplsFecElement_t * createFecElemFromFecType(struct mpls_fec * fec)
217 mplsFecElement_t * fecElem =
218 (mplsFecElement_t *) malloc(sizeof(mplsFecElement_t));
219 fecElem->addressEl.type = MPLS_PREFIX_FEC;
220 fecElem->addressEl.addressFam = 1;
221 fecElem->addressEl.preLen = fec->len;
222 fecElem->addressEl.address = fec->prefix;
223 return fecElem;
226 mplsFecElement_t * createFecElemFromRoute(routeT * r)
228 mplsFecElement_t * fecElem =
229 (mplsFecElement_t *) malloc(sizeof(mplsFecElement_t));
230 memset(fecElem, 0, sizeof(mplsFecElement_t));
231 fecElem->addressEl.type = MPLS_PREFIX_FEC;
232 fecElem->addressEl.addressFam = 1;
233 fecElem->addressEl.preLen = r->len;
234 fecElem->addressEl.address = r->prefix;
235 return fecElem;
239 #endif /* */
240 int addFecElem2FecTlv(mplsLdpFecTlv_t * fecTlv, mplsFecElement_t * elem)
242 int num = fecTlv->numberFecElements;
243 int size = 0;
245 switch (elem->addressEl.type) {
246 case MPLS_PREFIX_FEC:
247 case MPLS_HOSTADR_FEC:
248 size = elem->addressEl.preLen / 8;
249 if (elem->addressEl.preLen % 8)
250 size++;
251 size += 4;
252 break;
253 case MPLS_CRLSP_FEC:
254 size = 4;
255 break;
257 fecTlv->baseTlv.length += size;
258 memcpy(&(fecTlv->fecElArray[num]), elem, sizeof(mplsFecElement_t));
259 fecTlv->fecElemTypes[num] = elem->addressEl.type;
260 fecTlv->numberFecElements++;
261 return size;
265 #if 0
266 void copyLabelType2MapLabelTlv(struct mpls_label *label,
267 mplsLdpLblMapMsg_t * lblMap)
269 switch (label->ml_type) {
270 case MPLS_LABEL_ATM:
271 lblMap->baseMsg.msgLength +=
272 setupAtmLblTlv(&(lblMap->atmLblTlv), 0, 0, label->u.ml_atm.mla_vpi,
273 label->u.ml_atm.mla_vci); lblMap->atmLblTlvExists = 1;
274 lblMap->genLblTlvExists = 0;
275 lblMap->frLblTlvExists = 0;
276 break;
277 case MPLS_LABEL_GEN:
278 lblMap->baseMsg.msgLength +=
279 setupGenLblTlv(&(lblMap->genLblTlv), label->u.ml_gen);
280 lblMap->atmLblTlvExists = 0;
281 lblMap->genLblTlvExists = 1;
282 lblMap->frLblTlvExists = 0;
283 break;
284 case MPLS_LABEL_FR:
285 lblMap->baseMsg.msgLength +=
286 setupFrLblTlv(&(lblMap->frLblTlv), 0, 0, label->u.ml_fr);
287 lblMap->atmLblTlvExists = 0;
288 lblMap->genLblTlvExists = 0;
289 lblMap->frLblTlvExists = 1;
290 break;
291 default:
292 LDP_PRINT(g->user_data, "invalid label type\n");
293 break;
296 void copyAtmLblTlv2MplsLabel(mplsLdpAtmLblTlv_t * atmLblTlv,
297 struct mpls_label *label)
299 label->ml_type = MPLS_LABEL_ATM;
300 label->u.ml_atm.mla_vpi = atmLblTlv->flags.flags.vpi;
301 label->u.ml_atm.mla_vci = atmLblTlv->vci;
305 #endif /* */
307 int setupAtmLblTlv(mplsLdpAtmLblTlv_t * atmLblTlv, int res, int v,
308 unsigned int vpi, unsigned int vci)
310 atmLblTlv->baseTlv.flags.flags.tBit = MPLS_ATMLBL_TLVTYPE;
311 atmLblTlv->baseTlv.flags.flags.uBit = 0;
312 atmLblTlv->baseTlv.flags.flags.fBit = 0;
313 atmLblTlv->baseTlv.length = MPLS_LBLFIXLEN;
314 atmLblTlv->flags.flags.res = res;
315 atmLblTlv->flags.flags.v = v;
316 atmLblTlv->flags.flags.vpi = vpi;
317 atmLblTlv->vci = vci;
318 return MPLS_LBLFIXLEN + MPLS_TLVFIXLEN;
322 #if 0
323 void copyFrLblTlv2MplsLabel(mplsLdpFrLblTlv_t * frLblTlv,
324 struct mpls_label *label)
326 label->ml_type = MPLS_LABEL_FR;
327 label->u.ml_fr = frLblTlv->flags.flags.dlci;
331 #endif /* */
333 int setupFrLblTlv(mplsLdpFrLblTlv_t * frLblTlv, int res, int len,
334 unsigned int dlci)
336 frLblTlv->baseTlv.flags.flags.tBit = MPLS_FRLBL_TLVTYPE;
337 frLblTlv->baseTlv.flags.flags.uBit = 0;
338 frLblTlv->baseTlv.flags.flags.fBit = 0;
339 frLblTlv->baseTlv.length = MPLS_LBLFIXLEN;
340 frLblTlv->flags.flags.res = res;
341 frLblTlv->flags.flags.len = len;
342 frLblTlv->flags.flags.dlci = dlci;
343 return MPLS_LBLFIXLEN + MPLS_TLVFIXLEN;
347 #if 0
348 void copyGenLblTlv2MplsLabel(mplsLdpGenLblTlv_t * genLblTlv,
349 struct mpls_label *label)
351 label->ml_type = MPLS_LABEL_GEN;
352 label->u.ml_gen = genLblTlv->label;
356 #endif /* */
358 int setupGenLblTlv(mplsLdpGenLblTlv_t * genLblTlv, int label)
360 genLblTlv->baseTlv.flags.flags.tBit = MPLS_GENLBL_TLVTYPE;
361 genLblTlv->baseTlv.flags.flags.uBit = 0;
362 genLblTlv->baseTlv.flags.flags.fBit = 0;
363 genLblTlv->baseTlv.length = MPLS_LBLFIXLEN;
364 genLblTlv->label = label;
365 return MPLS_LBLFIXLEN + MPLS_TLVFIXLEN;
368 int setupHopCountTlv(mplsLdpHopTlv_t * hopCountTlv, unsigned int hopCount)
370 hopCountTlv->baseTlv.flags.flags.tBit = MPLS_HOPCOUNT_TLVTYPE;
371 hopCountTlv->baseTlv.flags.flags.uBit = 0;
372 hopCountTlv->baseTlv.flags.flags.fBit = 0;
373 hopCountTlv->baseTlv.length = MPLS_HOPCOUNTFIXLEN;
374 hopCountTlv->hcValue = hopCount;
375 return MPLS_HOPCOUNTFIXLEN + MPLS_TLVFIXLEN;
378 int setupPathTlv(mplsLdpPathTlv_t * pathTlv)
380 pathTlv->baseTlv.flags.flags.tBit = MPLS_PATH_TLVTYPE;
381 pathTlv->baseTlv.flags.flags.uBit = 0;
382 pathTlv->baseTlv.flags.flags.fBit = 0;
383 pathTlv->baseTlv.length = 0;
384 return MPLS_TLVFIXLEN;
387 int addLsrId2PathTlv(mplsLdpPathTlv_t * pathTlv, unsigned int lsrId)
389 int num = pathTlv->baseTlv.length / sizeof(unsigned int);
390 pathTlv->baseTlv.length += sizeof(unsigned int);
392 pathTlv->lsrId[num] = lsrId;
393 return sizeof(unsigned int);
396 int setupAddrTlv(mplsLdpAdrTlv_t * addrTlv)
398 addrTlv->baseTlv.flags.flags.tBit = MPLS_ADDRLIST_TLVTYPE;
399 addrTlv->baseTlv.flags.flags.uBit = 0;
400 addrTlv->baseTlv.flags.flags.fBit = 0;
401 addrTlv->baseTlv.length = MPLS_ADDFAMFIXLEN;
402 addrTlv->addrFamily = 1;
403 return MPLS_TLVFIXLEN + MPLS_ADDFAMFIXLEN;
406 int addAddrElem2AddrTlv(mplsLdpAdrTlv_t * addrTlv, unsigned int addr)
408 int num = (addrTlv->baseTlv.length - MPLS_ADDFAMFIXLEN) / MPLS_IPv4LEN;
410 addrTlv->address[num] = addr;
411 addrTlv->baseTlv.length += MPLS_IPv4LEN;
412 return MPLS_IPv4LEN;
415 int setupStatusTlv(mplsLdpStatusTlv_t * statTlv, int fatal, int forward,
416 int status, unsigned int msgId, int msgType)
418 statTlv->baseTlv.flags.flags.tBit = MPLS_NOT_ST_TLVTYPE;
419 statTlv->baseTlv.flags.flags.uBit = 0;
420 statTlv->baseTlv.flags.flags.fBit = 0;
421 statTlv->baseTlv.length = MPLS_STATUSFIXLEN;
422 statTlv->flags.flags.error = fatal;
423 statTlv->flags.flags.forward = forward;
424 statTlv->flags.flags.status = status;
425 statTlv->msgId = msgId;
426 statTlv->msgType = msgType;
427 return MPLS_STATUSFIXLEN + MPLS_TLVFIXLEN;
430 int setupExStatusTlv(mplsLdpExStatusTlv_t * exStatus, unsigned int value)
432 exStatus->baseTlv.flags.flags.tBit = MPLS_NOT_ES_TLVTYPE;
433 exStatus->baseTlv.flags.flags.uBit = 0;
434 exStatus->baseTlv.flags.flags.fBit = 0;
435 exStatus->baseTlv.length = MPLS_EXSTATUSLEN;
436 exStatus->value = value;
437 return MPLS_EXSTATUSLEN + MPLS_TLVFIXLEN;
440 int setupRetPduTlv(mplsLdpRetPduTlv_t * retPduTvl, unsigned int len,
441 mplsLdpHeader_t * hdr, void *data)
443 retPduTvl->baseTlv.flags.flags.tBit = MPLS_NOT_RP_TLVTYPE;
444 retPduTvl->baseTlv.flags.flags.uBit = 0;
445 retPduTvl->baseTlv.flags.flags.fBit = 0;
446 retPduTvl->baseTlv.length = MPLS_LDP_HDRSIZE + len;
447 memcpy(&(retPduTvl->headerTlv), hdr, MPLS_LDP_HDRSIZE);
448 memcpy(retPduTvl->data, data, len);
449 return MPLS_LDP_HDRSIZE + len + MPLS_TLVFIXLEN;
452 int setupRetMsgTlv(mplsLdpRetMsgTlv_t * retMsgTlv, unsigned type,
453 unsigned len, void *data)
455 retMsgTlv->baseTlv.flags.flags.tBit = MPLS_NOT_RM_TLVTYPE;
456 retMsgTlv->baseTlv.flags.flags.uBit = 0;
457 retMsgTlv->baseTlv.flags.flags.fBit = 0;
458 retMsgTlv->baseTlv.length = len;
459 retMsgTlv->msgType = type;
460 retMsgTlv->msgLength = 4 + len;
461 memcpy(retMsgTlv->data, data, len);
462 return 4 + len + MPLS_TLVFIXLEN;
465 int setupLspidTlv(mplsLdpLspIdTlv_t * lspidTlv, int res,
466 unsigned int localCrlspId, unsigned int routerId)
468 lspidTlv->baseTlv.flags.flags.tBit = MPLS_LSPID_TLVTYPE;
469 lspidTlv->baseTlv.flags.flags.uBit = 0;
470 lspidTlv->baseTlv.flags.flags.fBit = 0;
471 lspidTlv->baseTlv.length = MPLS_LSPIDTLV_FIXLEN;
472 lspidTlv->res = res;
473 lspidTlv->localCrlspId = localCrlspId;
474 lspidTlv->routerId = routerId;
475 return MPLS_LSPIDTLV_FIXLEN + MPLS_TLVFIXLEN;
478 int setupTrafficTlv(mplsLdpTrafficTlv_t * trafficTlv, unsigned char freq,
479 unsigned char res, unsigned char weight, float pdr, float pbs, float cdr,
480 float cbs, float ebs)
482 trafficTlv->baseTlv.flags.flags.tBit = MPLS_TRAFFIC_TLVTYPE;
483 trafficTlv->baseTlv.flags.flags.uBit = 0;
484 trafficTlv->baseTlv.flags.flags.fBit = 0;
485 trafficTlv->baseTlv.length = 0;
486 trafficTlv->flags.flags.res = 0;
487 trafficTlv->flags.flags.f6Bit = 0;
488 trafficTlv->flags.flags.f5Bit = 0;
489 trafficTlv->flags.flags.f4Bit = 0;
490 trafficTlv->flags.flags.f3Bit = 0;
491 trafficTlv->flags.flags.f2Bit = 0;
492 trafficTlv->flags.flags.f1Bit = 0;
493 trafficTlv->freq = freq;
494 trafficTlv->res = res;
495 trafficTlv->weight = weight;
496 trafficTlv->pdr.pdr = pdr;
497 trafficTlv->pbs.pbs = pbs;
498 trafficTlv->cdr.cdr = cdr;
499 trafficTlv->cbs.cbs = cbs;
500 trafficTlv->ebs.ebs = ebs;
501 return MPLS_TLVFIXLEN;
504 int setupLblMsgIdTlv(mplsLdpLblMsgIdTlv_t * lblMsgIdTlv, unsigned int msgId)
506 lblMsgIdTlv->baseTlv.flags.flags.tBit = MPLS_REQMSGID_TLVTYPE;
507 lblMsgIdTlv->baseTlv.flags.flags.uBit = 0;
508 lblMsgIdTlv->baseTlv.flags.flags.fBit = 0;
509 lblMsgIdTlv->baseTlv.length = MPLS_MSGIDFIXLEN;
510 lblMsgIdTlv->msgId = msgId;
511 return MPLS_MSGIDFIXLEN + MPLS_TLVFIXLEN;