ldpd: changes for code maintainability

these changes are for improving the code maintainability and readability

Signed-off-by: sri-mohan1 <sri.mohan@samsung.com>
This commit is contained in:
sri-mohan1 2023-05-07 21:00:52 +05:30
parent 91cde6087d
commit e75e1553fa

View File

@ -47,12 +47,11 @@ send_labelmessage(struct nbr *nbr, uint16_t type, struct mapping_head *mh)
while ((me = TAILQ_FIRST(mh)) != NULL) { while ((me = TAILQ_FIRST(mh)) != NULL) {
/* generate pdu */ /* generate pdu */
if (first) { if (first) {
if ((buf = ibuf_open(nbr->max_pdu_len + if ((buf = ibuf_open(nbr->max_pdu_len + LDP_HDR_DEAD_LEN)) == NULL)
LDP_HDR_DEAD_LEN)) == NULL)
fatal(__func__); fatal(__func__);
/* real size will be set up later */ /* real size will be set up later */
err |= gen_ldp_hdr(buf, 0); SET_FLAG(err, gen_ldp_hdr(buf, 0));
size = LDP_HDR_SIZE; size = LDP_HDR_SIZE;
first = 0; first = 0;
@ -63,9 +62,9 @@ send_labelmessage(struct nbr *nbr, uint16_t type, struct mapping_head *mh)
msg_size += len_fec_tlv(&me->map); msg_size += len_fec_tlv(&me->map);
if (me->map.label != NO_LABEL) if (me->map.label != NO_LABEL)
msg_size += LABEL_TLV_SIZE; msg_size += LABEL_TLV_SIZE;
if (me->map.flags & F_MAP_REQ_ID) if (CHECK_FLAG(me->map.flags, F_MAP_REQ_ID))
msg_size += REQID_TLV_SIZE; msg_size += REQID_TLV_SIZE;
if (me->map.flags & F_MAP_STATUS) if (CHECK_FLAG(me->map.flags, F_MAP_STATUS))
msg_size += STATUS_SIZE; msg_size += STATUS_SIZE;
/* maximum pdu length exceeded, we need a new ldp pdu */ /* maximum pdu length exceeded, we need a new ldp pdu */
@ -78,17 +77,17 @@ send_labelmessage(struct nbr *nbr, uint16_t type, struct mapping_head *mh)
size += msg_size; size += msg_size;
/* append message and tlvs */ /* append message and tlvs */
err |= gen_msg_hdr(buf, type, msg_size); SET_FLAG(err, gen_msg_hdr(buf, type, msg_size));
err |= gen_fec_tlv(buf, &me->map); SET_FLAG(err, gen_fec_tlv(buf, &me->map));
if (me->map.label != NO_LABEL) if (me->map.label != NO_LABEL)
err |= gen_label_tlv(buf, me->map.label); SET_FLAG(err, gen_label_tlv(buf, me->map.label));
if (me->map.flags & F_MAP_REQ_ID) if (CHECK_FLAG(me->map.flags, F_MAP_REQ_ID))
err |= gen_reqid_tlv(buf, me->map.requestid); SET_FLAG(err, gen_reqid_tlv(buf, me->map.requestid));
if (me->map.flags & F_MAP_PW_STATUS) if (CHECK_FLAG(me->map.flags, F_MAP_PW_STATUS))
err |= gen_pw_status_tlv(buf, me->map.pw_status); SET_FLAG(err, gen_pw_status_tlv(buf, me->map.pw_status));
if (me->map.flags & F_MAP_STATUS) if (CHECK_FLAG(me->map.flags, F_MAP_STATUS))
err |= gen_status_tlv(buf, me->map.st.status_code, SET_FLAG(err, gen_status_tlv(buf, me->map.st.status_code,
me->map.st.msg_id, me->map.st.msg_type); me->map.st.msg_id, me->map.st.msg_type));
if (err) { if (err) {
ibuf_free(buf); ibuf_free(buf);
mapping_list_clr(mh); mapping_list_clr(mh);
@ -172,15 +171,13 @@ recv_labelmessage(struct nbr *nbr, char *buf, uint16_t len, uint16_t type)
memset(&map, 0, sizeof(map)); memset(&map, 0, sizeof(map));
map.msg_id = msg.id; map.msg_id = msg.id;
if ((tlen = tlv_decode_fec_elm(nbr, &msg, buf, feclen, if ((tlen = tlv_decode_fec_elm(nbr, &msg, buf, feclen, &map)) == -1)
&map)) == -1)
goto err; goto err;
if (map.type == MAP_TYPE_PWID && if (map.type == MAP_TYPE_PWID &&
!(map.flags & F_MAP_PW_ID) && !CHECK_FLAG(map.flags, F_MAP_PW_ID) &&
type != MSG_TYPE_LABELWITHDRAW && type != MSG_TYPE_LABELWITHDRAW &&
type != MSG_TYPE_LABELRELEASE) { type != MSG_TYPE_LABELRELEASE) {
send_notification(nbr->tcp, S_MISS_MSG, msg.id, send_notification(nbr->tcp, S_MISS_MSG, msg.id, msg.type);
msg.type);
goto err; goto err;
} }
@ -193,8 +190,7 @@ recv_labelmessage(struct nbr *nbr, char *buf, uint16_t len, uint16_t type)
case MSG_TYPE_LABELMAPPING: case MSG_TYPE_LABELMAPPING:
case MSG_TYPE_LABELREQUEST: case MSG_TYPE_LABELREQUEST:
case MSG_TYPE_LABELABORTREQ: case MSG_TYPE_LABELABORTREQ:
session_shutdown(nbr, S_UNKNOWN_FEC, msg.id, session_shutdown(nbr, S_UNKNOWN_FEC, msg.id, msg.type);
msg.type);
goto err; goto err;
default: default:
break; break;
@ -211,8 +207,7 @@ recv_labelmessage(struct nbr *nbr, char *buf, uint16_t len, uint16_t type)
switch (type) { switch (type) {
case MSG_TYPE_LABELMAPPING: case MSG_TYPE_LABELMAPPING:
case MSG_TYPE_LABELABORTREQ: case MSG_TYPE_LABELABORTREQ:
session_shutdown(nbr, S_UNKNOWN_FEC, msg.id, session_shutdown(nbr, S_UNKNOWN_FEC, msg.id, msg.type);
msg.type);
goto err; goto err;
default: default:
break; break;
@ -223,8 +218,7 @@ recv_labelmessage(struct nbr *nbr, char *buf, uint16_t len, uint16_t type)
* LDP supports the use of multiple FEC Elements per * LDP supports the use of multiple FEC Elements per
* FEC for the Label Mapping message only. * FEC for the Label Mapping message only.
*/ */
if (type != MSG_TYPE_LABELMAPPING && if (type != MSG_TYPE_LABELMAPPING && tlen != feclen) {
tlen != feclen) {
session_shutdown(nbr, S_BAD_TLV_VAL, msg.id, msg.type); session_shutdown(nbr, S_BAD_TLV_VAL, msg.id, msg.type);
goto err; goto err;
} }
@ -262,10 +256,10 @@ recv_labelmessage(struct nbr *nbr, char *buf, uint16_t len, uint16_t type)
* For Label Mapping messages the Label TLV is mandatory and * For Label Mapping messages the Label TLV is mandatory and
* should appear right after the FEC TLV. * should appear right after the FEC TLV.
*/ */
if (current_tlv == 1 && type == MSG_TYPE_LABELMAPPING && if (current_tlv == 1 &&
!(tlv_type & TLV_TYPE_GENERICLABEL)) { type == MSG_TYPE_LABELMAPPING &&
send_notification(nbr->tcp, S_MISS_MSG, msg.id, !CHECK_FLAG(tlv_type, TLV_TYPE_GENERICLABEL)) {
msg.type); send_notification(nbr->tcp, S_MISS_MSG, msg.id, msg.type);
goto err; goto err;
} }
@ -275,12 +269,11 @@ recv_labelmessage(struct nbr *nbr, char *buf, uint16_t len, uint16_t type)
case MSG_TYPE_LABELMAPPING: case MSG_TYPE_LABELMAPPING:
case MSG_TYPE_LABELREQUEST: case MSG_TYPE_LABELREQUEST:
if (tlv_len != REQID_TLV_LEN) { if (tlv_len != REQID_TLV_LEN) {
session_shutdown(nbr, S_BAD_TLV_LEN, session_shutdown(nbr, S_BAD_TLV_LEN, msg.id, msg.type);
msg.id, msg.type);
goto err; goto err;
} }
flags |= F_MAP_REQ_ID; SET_FLAG(flags, F_MAP_REQ_ID);
memcpy(&reqbuf, buf, sizeof(reqbuf)); memcpy(&reqbuf, buf, sizeof(reqbuf));
reqid = ntohl(reqbuf); reqid = ntohl(reqbuf);
break; break;
@ -299,8 +292,7 @@ recv_labelmessage(struct nbr *nbr, char *buf, uint16_t len, uint16_t type)
case MSG_TYPE_LABELWITHDRAW: case MSG_TYPE_LABELWITHDRAW:
case MSG_TYPE_LABELRELEASE: case MSG_TYPE_LABELRELEASE:
if (tlv_len != LABEL_TLV_LEN) { if (tlv_len != LABEL_TLV_LEN) {
session_shutdown(nbr, S_BAD_TLV_LEN, session_shutdown(nbr, S_BAD_TLV_LEN, msg.id, msg.type);
msg.id, msg.type);
goto err; goto err;
} }
@ -312,8 +304,7 @@ recv_labelmessage(struct nbr *nbr, char *buf, uint16_t len, uint16_t type)
label != MPLS_LABEL_IPV4_EXPLICIT_NULL && label != MPLS_LABEL_IPV4_EXPLICIT_NULL &&
label != MPLS_LABEL_IPV6_EXPLICIT_NULL && label != MPLS_LABEL_IPV6_EXPLICIT_NULL &&
label != MPLS_LABEL_IMPLICIT_NULL)) { label != MPLS_LABEL_IMPLICIT_NULL)) {
session_shutdown(nbr, S_BAD_TLV_VAL, session_shutdown(nbr, S_BAD_TLV_VAL, msg.id, msg.type);
msg.id, msg.type);
goto err; goto err;
} }
break; break;
@ -329,8 +320,7 @@ recv_labelmessage(struct nbr *nbr, char *buf, uint16_t len, uint16_t type)
case MSG_TYPE_LABELWITHDRAW: case MSG_TYPE_LABELWITHDRAW:
case MSG_TYPE_LABELRELEASE: case MSG_TYPE_LABELRELEASE:
/* unsupported */ /* unsupported */
session_shutdown(nbr, S_BAD_TLV_VAL, msg.id, session_shutdown(nbr, S_BAD_TLV_VAL, msg.id, msg.type);
msg.type);
goto err; goto err;
break; break;
default: default:
@ -340,8 +330,7 @@ recv_labelmessage(struct nbr *nbr, char *buf, uint16_t len, uint16_t type)
break; break;
case TLV_TYPE_STATUS: case TLV_TYPE_STATUS:
if (tlv_len != STATUS_TLV_LEN) { if (tlv_len != STATUS_TLV_LEN) {
session_shutdown(nbr, S_BAD_TLV_LEN, msg.id, session_shutdown(nbr, S_BAD_TLV_LEN, msg.id, msg.type);
msg.type);
goto err; goto err;
} }
/* ignore */ /* ignore */
@ -350,12 +339,11 @@ recv_labelmessage(struct nbr *nbr, char *buf, uint16_t len, uint16_t type)
switch (type) { switch (type) {
case MSG_TYPE_LABELMAPPING: case MSG_TYPE_LABELMAPPING:
if (tlv_len != PW_STATUS_TLV_LEN) { if (tlv_len != PW_STATUS_TLV_LEN) {
session_shutdown(nbr, S_BAD_TLV_LEN, session_shutdown(nbr, S_BAD_TLV_LEN, msg.id, msg.type);
msg.id, msg.type);
goto err; goto err;
} }
flags |= F_MAP_PW_STATUS; SET_FLAG(flags, F_MAP_PW_STATUS);
memcpy(&statusbuf, buf, sizeof(statusbuf)); memcpy(&statusbuf, buf, sizeof(statusbuf));
pw_status = ntohl(statusbuf); pw_status = ntohl(statusbuf);
break; break;
@ -365,7 +353,7 @@ recv_labelmessage(struct nbr *nbr, char *buf, uint16_t len, uint16_t type)
} }
break; break;
default: default:
if (!(ntohs(tlv.type) & UNKNOWN_FLAG)) if (!CHECK_FLAG(ntohs(tlv.type), UNKNOWN_FLAG))
send_notification_rtlvs(nbr, S_UNKNOWN_TLV, send_notification_rtlvs(nbr, S_UNKNOWN_TLV,
msg.id, msg.type, tlv_type, tlv_len, buf); msg.id, msg.type, tlv_type, tlv_len, buf);
/* ignore unknown tlv */ /* ignore unknown tlv */
@ -380,14 +368,13 @@ recv_labelmessage(struct nbr *nbr, char *buf, uint16_t len, uint16_t type)
while ((me = TAILQ_FIRST(&mh)) != NULL) { while ((me = TAILQ_FIRST(&mh)) != NULL) {
int imsg_type = IMSG_NONE; int imsg_type = IMSG_NONE;
me->map.flags |= flags; SET_FLAG(me->map.flags, flags);
switch (me->map.type) { switch (me->map.type) {
case MAP_TYPE_PREFIX: case MAP_TYPE_PREFIX:
switch (me->map.fec.prefix.af) { switch (me->map.fec.prefix.af) {
case AF_INET: case AF_INET:
if (label == MPLS_LABEL_IPV6_EXPLICIT_NULL) { if (label == MPLS_LABEL_IPV6_EXPLICIT_NULL) {
session_shutdown(nbr, S_BAD_TLV_VAL, session_shutdown(nbr, S_BAD_TLV_VAL, msg.id, msg.type);
msg.id, msg.type);
goto err; goto err;
} }
if (!nbr->v4_enabled) if (!nbr->v4_enabled)
@ -395,8 +382,7 @@ recv_labelmessage(struct nbr *nbr, char *buf, uint16_t len, uint16_t type)
break; break;
case AF_INET6: case AF_INET6:
if (label == MPLS_LABEL_IPV4_EXPLICIT_NULL) { if (label == MPLS_LABEL_IPV4_EXPLICIT_NULL) {
session_shutdown(nbr, S_BAD_TLV_VAL, session_shutdown(nbr, S_BAD_TLV_VAL, msg.id, msg.type);
msg.id, msg.type);
goto err; goto err;
} }
if (!nbr->v6_enabled) if (!nbr->v6_enabled)
@ -408,18 +394,17 @@ recv_labelmessage(struct nbr *nbr, char *buf, uint16_t len, uint16_t type)
break; break;
case MAP_TYPE_PWID: case MAP_TYPE_PWID:
if (label <= MPLS_LABEL_RESERVED_MAX) { if (label <= MPLS_LABEL_RESERVED_MAX) {
session_shutdown(nbr, S_BAD_TLV_VAL, msg.id, session_shutdown(nbr, S_BAD_TLV_VAL, msg.id, msg.type);
msg.type);
goto err; goto err;
} }
if (me->map.flags & F_MAP_PW_STATUS) if (CHECK_FLAG(me->map.flags, F_MAP_PW_STATUS))
me->map.pw_status = pw_status; me->map.pw_status = pw_status;
break; break;
default: default:
break; break;
} }
me->map.label = label; me->map.label = label;
if (me->map.flags & F_MAP_REQ_ID) if (CHECK_FLAG(me->map.flags, F_MAP_REQ_ID))
me->map.requestid = reqid; me->map.requestid = reqid;
log_msg_mapping(0, type, nbr, &me->map); log_msg_mapping(0, type, nbr, &me->map);
@ -513,11 +498,11 @@ len_fec_tlv(struct map *map)
break; break;
case MAP_TYPE_PWID: case MAP_TYPE_PWID:
len += FEC_PWID_ELM_MIN_LEN; len += FEC_PWID_ELM_MIN_LEN;
if (map->flags & F_MAP_PW_ID) if (CHECK_FLAG(map->flags, F_MAP_PW_ID))
len += PW_STATUS_TLV_LEN; len += PW_STATUS_TLV_LEN;
if (map->flags & F_MAP_PW_IFMTU) if (CHECK_FLAG(map->flags, F_MAP_PW_IFMTU))
len += FEC_SUBTLV_IFMTU_SIZE; len += FEC_SUBTLV_IFMTU_SIZE;
if (map->flags & F_MAP_PW_STATUS) if (CHECK_FLAG(map->flags, F_MAP_PW_STATUS))
len += PW_STATUS_TLV_SIZE; len += PW_STATUS_TLV_SIZE;
break; break;
case MAP_TYPE_TYPED_WCARD: case MAP_TYPE_TYPED_WCARD:
@ -552,15 +537,15 @@ gen_fec_tlv(struct ibuf *buf, struct map *map)
switch (map->type) { switch (map->type) {
case MAP_TYPE_WILDCARD: case MAP_TYPE_WILDCARD:
ft.length = htons(sizeof(uint8_t)); ft.length = htons(sizeof(uint8_t));
err |= ibuf_add(buf, &ft, sizeof(ft)); SET_FLAG(err, ibuf_add(buf, &ft, sizeof(ft)));
err |= ibuf_add(buf, &map->type, sizeof(map->type)); SET_FLAG(err, ibuf_add(buf, &map->type, sizeof(map->type)));
break; break;
case MAP_TYPE_PREFIX: case MAP_TYPE_PREFIX:
len = PREFIX_SIZE(map->fec.prefix.prefixlen); len = PREFIX_SIZE(map->fec.prefix.prefixlen);
ft.length = htons(sizeof(map->type) + sizeof(family) + ft.length = htons(sizeof(map->type) + sizeof(family) +
sizeof(map->fec.prefix.prefixlen) + len); sizeof(map->fec.prefix.prefixlen) + len);
err |= ibuf_add(buf, &ft, sizeof(ft)); SET_FLAG(err, ibuf_add(buf, &ft, sizeof(ft)));
err |= ibuf_add(buf, &map->type, sizeof(map->type)); SET_FLAG(err, ibuf_add(buf, &map->type, sizeof(map->type)));
switch (map->fec.prefix.af) { switch (map->fec.prefix.af) {
case AF_INET: case AF_INET:
family = htons(AF_IPV4); family = htons(AF_IPV4);
@ -572,45 +557,45 @@ gen_fec_tlv(struct ibuf *buf, struct map *map)
fatalx("gen_fec_tlv: unknown af"); fatalx("gen_fec_tlv: unknown af");
break; break;
} }
err |= ibuf_add(buf, &family, sizeof(family)); SET_FLAG(err, ibuf_add(buf, &family, sizeof(family)));
err |= ibuf_add(buf, &map->fec.prefix.prefixlen, SET_FLAG(err, ibuf_add(buf, &map->fec.prefix.prefixlen,
sizeof(map->fec.prefix.prefixlen)); sizeof(map->fec.prefix.prefixlen)));
if (len) if (len)
err |= ibuf_add(buf, &map->fec.prefix.prefix, len); SET_FLAG(err, ibuf_add(buf, &map->fec.prefix.prefix, len));
break; break;
case MAP_TYPE_PWID: case MAP_TYPE_PWID:
if (map->flags & F_MAP_PW_ID) if (CHECK_FLAG(map->flags, F_MAP_PW_ID))
pw_len += FEC_PWID_SIZE; pw_len += FEC_PWID_SIZE;
if (map->flags & F_MAP_PW_IFMTU) if (CHECK_FLAG(map->flags, F_MAP_PW_IFMTU))
pw_len += FEC_SUBTLV_IFMTU_SIZE; pw_len += FEC_SUBTLV_IFMTU_SIZE;
len = FEC_PWID_ELM_MIN_LEN + pw_len; len = FEC_PWID_ELM_MIN_LEN + pw_len;
ft.length = htons(len); ft.length = htons(len);
err |= ibuf_add(buf, &ft, sizeof(ft)); SET_FLAG(err, ibuf_add(buf, &ft, sizeof(ft)));
err |= ibuf_add(buf, &map->type, sizeof(uint8_t)); SET_FLAG(err, ibuf_add(buf, &map->type, sizeof(uint8_t)));
pw_type = map->fec.pwid.type; pw_type = map->fec.pwid.type;
if (map->flags & F_MAP_PW_CWORD) if (CHECK_FLAG(map->flags, F_MAP_PW_CWORD))
pw_type |= CONTROL_WORD_FLAG; SET_FLAG(pw_type, CONTROL_WORD_FLAG);
pw_type = htons(pw_type); pw_type = htons(pw_type);
err |= ibuf_add(buf, &pw_type, sizeof(uint16_t)); SET_FLAG(err, ibuf_add(buf, &pw_type, sizeof(uint16_t)));
err |= ibuf_add(buf, &pw_len, sizeof(uint8_t)); SET_FLAG(err, ibuf_add(buf, &pw_len, sizeof(uint8_t)));
group_id = htonl(map->fec.pwid.group_id); group_id = htonl(map->fec.pwid.group_id);
err |= ibuf_add(buf, &group_id, sizeof(uint32_t)); SET_FLAG(err, ibuf_add(buf, &group_id, sizeof(uint32_t)));
if (map->flags & F_MAP_PW_ID) { if (CHECK_FLAG(map->flags, F_MAP_PW_ID)) {
pwid = htonl(map->fec.pwid.pwid); pwid = htonl(map->fec.pwid.pwid);
err |= ibuf_add(buf, &pwid, sizeof(uint32_t)); SET_FLAG(err, ibuf_add(buf, &pwid, sizeof(uint32_t)));
} }
if (map->flags & F_MAP_PW_IFMTU) { if (CHECK_FLAG(map->flags, F_MAP_PW_IFMTU)) {
struct subtlv stlv; struct subtlv stlv;
stlv.type = SUBTLV_IFMTU; stlv.type = SUBTLV_IFMTU;
stlv.length = FEC_SUBTLV_IFMTU_SIZE; stlv.length = FEC_SUBTLV_IFMTU_SIZE;
err |= ibuf_add(buf, &stlv, sizeof(uint16_t)); SET_FLAG(err, ibuf_add(buf, &stlv, sizeof(uint16_t)));
ifmtu = htons(map->fec.pwid.ifmtu); ifmtu = htons(map->fec.pwid.ifmtu);
err |= ibuf_add(buf, &ifmtu, sizeof(uint16_t)); SET_FLAG(err, ibuf_add(buf, &ifmtu, sizeof(uint16_t)));
} }
break; break;
case MAP_TYPE_TYPED_WCARD: case MAP_TYPE_TYPED_WCARD:
@ -624,14 +609,14 @@ gen_fec_tlv(struct ibuf *buf, struct map *map)
fatalx("gen_fec_tlv: unexpected fec type"); fatalx("gen_fec_tlv: unexpected fec type");
} }
ft.length = htons(len); ft.length = htons(len);
err |= ibuf_add(buf, &ft, sizeof(ft)); SET_FLAG(err, ibuf_add(buf, &ft, sizeof(ft)));
err |= ibuf_add(buf, &map->type, sizeof(uint8_t)); SET_FLAG(err, ibuf_add(buf, &map->type, sizeof(uint8_t)));
err |= ibuf_add(buf, &map->fec.twcard.type, sizeof(uint8_t)); SET_FLAG(err, ibuf_add(buf, &map->fec.twcard.type, sizeof(uint8_t)));
switch (map->fec.twcard.type) { switch (map->fec.twcard.type) {
case MAP_TYPE_PREFIX: case MAP_TYPE_PREFIX:
twcard_len = sizeof(uint16_t); twcard_len = sizeof(uint16_t);
err |= ibuf_add(buf, &twcard_len, sizeof(uint8_t)); SET_FLAG(err, ibuf_add(buf, &twcard_len, sizeof(uint8_t)));
switch (map->fec.twcard.u.prefix_af) { switch (map->fec.twcard.u.prefix_af) {
case AF_INET: case AF_INET:
@ -645,13 +630,13 @@ gen_fec_tlv(struct ibuf *buf, struct map *map)
break; break;
} }
err |= ibuf_add(buf, &family, sizeof(uint16_t)); SET_FLAG(err, ibuf_add(buf, &family, sizeof(uint16_t)));
break; break;
case MAP_TYPE_PWID: case MAP_TYPE_PWID:
twcard_len = sizeof(uint16_t); twcard_len = sizeof(uint16_t);
err |= ibuf_add(buf, &twcard_len, sizeof(uint8_t)); SET_FLAG(err, ibuf_add(buf, &twcard_len, sizeof(uint8_t)));
pw_type = htons(map->fec.twcard.u.pw_type); pw_type = htons(map->fec.twcard.u.pw_type);
err |= ibuf_add(buf, &pw_type, sizeof(uint16_t)); SET_FLAG(err, ibuf_add(buf, &pw_type, sizeof(uint16_t)));
break; break;
default: default:
fatalx("gen_fec_tlv: unexpected fec type"); fatalx("gen_fec_tlv: unexpected fec type");
@ -679,21 +664,18 @@ tlv_decode_fec_elm(struct nbr *nbr, struct ldp_msg *msg, char *buf,
if (len == FEC_ELM_WCARD_LEN) if (len == FEC_ELM_WCARD_LEN)
return (off); return (off);
else { else {
session_shutdown(nbr, S_BAD_TLV_VAL, msg->id, session_shutdown(nbr, S_BAD_TLV_VAL, msg->id, msg->type);
msg->type);
return (-1); return (-1);
} }
break; break;
case MAP_TYPE_PREFIX: case MAP_TYPE_PREFIX:
if (len < FEC_ELM_PREFIX_MIN_LEN) { if (len < FEC_ELM_PREFIX_MIN_LEN) {
session_shutdown(nbr, S_BAD_TLV_LEN, msg->id, session_shutdown(nbr, S_BAD_TLV_LEN, msg->id, msg->type);
msg->type);
return (-1); return (-1);
} }
/* Address Family */ /* Address Family */
memcpy(&map->fec.prefix.af, buf + off, memcpy(&map->fec.prefix.af, buf + off, sizeof(map->fec.prefix.af));
sizeof(map->fec.prefix.af));
off += sizeof(map->fec.prefix.af); off += sizeof(map->fec.prefix.af);
map->fec.prefix.af = ntohs(map->fec.prefix.af); map->fec.prefix.af = ntohs(map->fec.prefix.af);
switch (map->fec.prefix.af) { switch (map->fec.prefix.af) {
@ -704,8 +686,7 @@ tlv_decode_fec_elm(struct nbr *nbr, struct ldp_msg *msg, char *buf,
map->fec.prefix.af = AF_INET6; map->fec.prefix.af = AF_INET6;
break; break;
default: default:
send_notification(nbr->tcp, S_UNSUP_ADDR, msg->id, send_notification(nbr->tcp, S_UNSUP_ADDR, msg->id, msg->type);
msg->type);
return (-1); return (-1);
} }
@ -716,19 +697,16 @@ tlv_decode_fec_elm(struct nbr *nbr, struct ldp_msg *msg, char *buf,
&& map->fec.prefix.prefixlen > IPV4_MAX_BITLEN) && map->fec.prefix.prefixlen > IPV4_MAX_BITLEN)
|| (map->fec.prefix.af == AF_IPV6 || (map->fec.prefix.af == AF_IPV6
&& map->fec.prefix.prefixlen > IPV6_MAX_BITLEN)) { && map->fec.prefix.prefixlen > IPV6_MAX_BITLEN)) {
session_shutdown(nbr, S_BAD_TLV_VAL, msg->id, session_shutdown(nbr, S_BAD_TLV_VAL, msg->id, msg->type);
msg->type);
return (-1); return (-1);
} }
if (len < off + PREFIX_SIZE(map->fec.prefix.prefixlen)) { if (len < off + PREFIX_SIZE(map->fec.prefix.prefixlen)) {
session_shutdown(nbr, S_BAD_TLV_LEN, msg->id, session_shutdown(nbr, S_BAD_TLV_LEN, msg->id, msg->type);
msg->type);
return (-1); return (-1);
} }
/* Prefix */ /* Prefix */
memset(&map->fec.prefix.prefix, 0, memset(&map->fec.prefix.prefix, 0, sizeof(map->fec.prefix.prefix));
sizeof(map->fec.prefix.prefix));
memcpy(&map->fec.prefix.prefix, buf + off, memcpy(&map->fec.prefix.prefix, buf + off,
PREFIX_SIZE(map->fec.prefix.prefixlen)); PREFIX_SIZE(map->fec.prefix.prefixlen));
@ -739,17 +717,16 @@ tlv_decode_fec_elm(struct nbr *nbr, struct ldp_msg *msg, char *buf,
return (off + PREFIX_SIZE(map->fec.prefix.prefixlen)); return (off + PREFIX_SIZE(map->fec.prefix.prefixlen));
case MAP_TYPE_PWID: case MAP_TYPE_PWID:
if (len < FEC_PWID_ELM_MIN_LEN) { if (len < FEC_PWID_ELM_MIN_LEN) {
session_shutdown(nbr, S_BAD_TLV_LEN, msg->id, session_shutdown(nbr, S_BAD_TLV_LEN, msg->id, msg->type);
msg->type);
return (-1); return (-1);
} }
/* PW type */ /* PW type */
memcpy(&map->fec.pwid.type, buf + off, sizeof(uint16_t)); memcpy(&map->fec.pwid.type, buf + off, sizeof(uint16_t));
map->fec.pwid.type = ntohs(map->fec.pwid.type); map->fec.pwid.type = ntohs(map->fec.pwid.type);
if (map->fec.pwid.type & CONTROL_WORD_FLAG) { if (CHECK_FLAG(map->fec.pwid.type, CONTROL_WORD_FLAG)) {
map->flags |= F_MAP_PW_CWORD; SET_FLAG(map->flags, F_MAP_PW_CWORD);
map->fec.pwid.type &= ~CONTROL_WORD_FLAG; UNSET_FLAG(map->fec.pwid.type, CONTROL_WORD_FLAG);
} }
off += sizeof(uint16_t); off += sizeof(uint16_t);
@ -758,8 +735,7 @@ tlv_decode_fec_elm(struct nbr *nbr, struct ldp_msg *msg, char *buf,
off += sizeof(uint8_t); off += sizeof(uint8_t);
if (len != FEC_PWID_ELM_MIN_LEN + pw_len) { if (len != FEC_PWID_ELM_MIN_LEN + pw_len) {
session_shutdown(nbr, S_BAD_TLV_LEN, msg->id, session_shutdown(nbr, S_BAD_TLV_LEN, msg->id, msg->type);
msg->type);
return (-1); return (-1);
} }
@ -773,14 +749,13 @@ tlv_decode_fec_elm(struct nbr *nbr, struct ldp_msg *msg, char *buf,
return (off); return (off);
if (pw_len < sizeof(uint32_t)) { if (pw_len < sizeof(uint32_t)) {
session_shutdown(nbr, S_BAD_TLV_LEN, msg->id, session_shutdown(nbr, S_BAD_TLV_LEN, msg->id, msg->type);
msg->type);
return (-1); return (-1);
} }
memcpy(&map->fec.pwid.pwid, buf + off, sizeof(uint32_t)); memcpy(&map->fec.pwid.pwid, buf + off, sizeof(uint32_t));
map->fec.pwid.pwid = ntohl(map->fec.pwid.pwid); map->fec.pwid.pwid = ntohl(map->fec.pwid.pwid);
map->flags |= F_MAP_PW_ID; SET_FLAG(map->flags, F_MAP_PW_ID);
off += sizeof(uint32_t); off += sizeof(uint32_t);
pw_len -= sizeof(uint32_t); pw_len -= sizeof(uint32_t);
@ -789,29 +764,26 @@ tlv_decode_fec_elm(struct nbr *nbr, struct ldp_msg *msg, char *buf,
struct subtlv stlv; struct subtlv stlv;
if (pw_len < sizeof(stlv)) { if (pw_len < sizeof(stlv)) {
session_shutdown(nbr, S_BAD_TLV_LEN, msg->id, session_shutdown(nbr, S_BAD_TLV_LEN, msg->id, msg->type);
msg->type);
return (-1); return (-1);
} }
memcpy(&stlv, buf + off, sizeof(stlv)); memcpy(&stlv, buf + off, sizeof(stlv));
if (stlv.length > pw_len) { if (stlv.length > pw_len) {
session_shutdown(nbr, S_BAD_TLV_LEN, msg->id, session_shutdown(nbr, S_BAD_TLV_LEN, msg->id, msg->type);
msg->type);
return (-1); return (-1);
} }
switch (stlv.type) { switch (stlv.type) {
case SUBTLV_IFMTU: case SUBTLV_IFMTU:
if (stlv.length != FEC_SUBTLV_IFMTU_SIZE) { if (stlv.length != FEC_SUBTLV_IFMTU_SIZE) {
session_shutdown(nbr, S_BAD_TLV_LEN, session_shutdown(nbr, S_BAD_TLV_LEN, msg->id, msg->type);
msg->id, msg->type);
return (-1); return (-1);
} }
memcpy(&map->fec.pwid.ifmtu, buf + off + memcpy(&map->fec.pwid.ifmtu, buf + off +
SUBTLV_HDR_SIZE, sizeof(uint16_t)); SUBTLV_HDR_SIZE, sizeof(uint16_t));
map->fec.pwid.ifmtu = ntohs(map->fec.pwid.ifmtu); map->fec.pwid.ifmtu = ntohs(map->fec.pwid.ifmtu);
map->flags |= F_MAP_PW_IFMTU; SET_FLAG(map->flags, F_MAP_PW_IFMTU);
break; break;
default: default:
/* ignore */ /* ignore */
@ -824,8 +796,7 @@ tlv_decode_fec_elm(struct nbr *nbr, struct ldp_msg *msg, char *buf,
return (off); return (off);
case MAP_TYPE_TYPED_WCARD: case MAP_TYPE_TYPED_WCARD:
if (len < FEC_ELM_TWCARD_MIN_LEN) { if (len < FEC_ELM_TWCARD_MIN_LEN) {
session_shutdown(nbr, S_BAD_TLV_LEN, msg->id, session_shutdown(nbr, S_BAD_TLV_LEN, msg->id, msg->type);
msg->type);
return (-1); return (-1);
} }
@ -834,23 +805,19 @@ tlv_decode_fec_elm(struct nbr *nbr, struct ldp_msg *msg, char *buf,
memcpy(&twcard_len, buf + off, sizeof(uint8_t)); memcpy(&twcard_len, buf + off, sizeof(uint8_t));
off += sizeof(uint8_t); off += sizeof(uint8_t);
if (len != FEC_ELM_TWCARD_MIN_LEN + twcard_len) { if (len != FEC_ELM_TWCARD_MIN_LEN + twcard_len) {
session_shutdown(nbr, S_BAD_TLV_LEN, msg->id, session_shutdown(nbr, S_BAD_TLV_LEN, msg->id, msg->type);
msg->type);
return (-1); return (-1);
} }
switch (map->fec.twcard.type) { switch (map->fec.twcard.type) {
case MAP_TYPE_PREFIX: case MAP_TYPE_PREFIX:
if (twcard_len != sizeof(uint16_t)) { if (twcard_len != sizeof(uint16_t)) {
session_shutdown(nbr, S_BAD_TLV_LEN, msg->id, session_shutdown(nbr, S_BAD_TLV_LEN, msg->id, msg->type);
msg->type);
return (-1); return (-1);
} }
memcpy(&map->fec.twcard.u.prefix_af, buf + off, memcpy(&map->fec.twcard.u.prefix_af, buf + off, sizeof(uint16_t));
sizeof(uint16_t)); map->fec.twcard.u.prefix_af = ntohs(map->fec.twcard.u.prefix_af);
map->fec.twcard.u.prefix_af =
ntohs(map->fec.twcard.u.prefix_af);
off += sizeof(uint16_t); off += sizeof(uint16_t);
switch (map->fec.twcard.u.prefix_af) { switch (map->fec.twcard.u.prefix_af) {
@ -861,29 +828,24 @@ tlv_decode_fec_elm(struct nbr *nbr, struct ldp_msg *msg, char *buf,
map->fec.twcard.u.prefix_af = AF_INET6; map->fec.twcard.u.prefix_af = AF_INET6;
break; break;
default: default:
session_shutdown(nbr, S_BAD_TLV_VAL, msg->id, session_shutdown(nbr, S_BAD_TLV_VAL, msg->id, msg->type);
msg->type);
return (-1); return (-1);
} }
break; break;
case MAP_TYPE_PWID: case MAP_TYPE_PWID:
if (twcard_len != sizeof(uint16_t)) { if (twcard_len != sizeof(uint16_t)) {
session_shutdown(nbr, S_BAD_TLV_LEN, msg->id, session_shutdown(nbr, S_BAD_TLV_LEN, msg->id, msg->type);
msg->type);
return (-1); return (-1);
} }
memcpy(&map->fec.twcard.u.pw_type, buf + off, memcpy(&map->fec.twcard.u.pw_type, buf + off, sizeof(uint16_t));
sizeof(uint16_t)); map->fec.twcard.u.pw_type = ntohs(map->fec.twcard.u.pw_type);
map->fec.twcard.u.pw_type =
ntohs(map->fec.twcard.u.pw_type);
/* ignore the reserved bit as per RFC 6667 */ /* ignore the reserved bit as per RFC 6667 */
map->fec.twcard.u.pw_type &= ~PW_TWCARD_RESERVED_BIT; map->fec.twcard.u.pw_type &= ~PW_TWCARD_RESERVED_BIT;
off += sizeof(uint16_t); off += sizeof(uint16_t);
break; break;
default: default:
send_notification(nbr->tcp, S_UNKNOWN_FEC, msg->id, send_notification(nbr->tcp, S_UNKNOWN_FEC, msg->id, msg->type);
msg->type);
return (-1); return (-1);
} }