* (at your option) any later version.
*/
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
#include <netinet/if_ether.h>
#include "nhrpd.h"
#include "zbuf.h"
static uint16_t family2proto(int family)
{
switch (family) {
- case AF_INET: return ETH_P_IP;
- case AF_INET6: return ETH_P_IPV6;
+ case AF_INET:
+ return ETH_P_IP;
+ case AF_INET6:
+ return ETH_P_IPV6;
}
return 0;
}
static int proto2family(uint16_t proto)
{
switch (proto) {
- case ETH_P_IP: return AF_INET;
- case ETH_P_IPV6: return AF_INET6;
+ case ETH_P_IP:
+ return AF_INET;
+ case ETH_P_IPV6:
+ return AF_INET6;
}
return AF_UNSPEC;
}
-struct nhrp_packet_header *nhrp_packet_push(
- struct zbuf *zb, uint8_t type,
- const union sockunion *src_nbma,
- const union sockunion *src_proto,
- const union sockunion *dst_proto)
+struct nhrp_packet_header *nhrp_packet_push(struct zbuf *zb, uint8_t type,
+ const union sockunion *src_nbma,
+ const union sockunion *src_proto,
+ const union sockunion *dst_proto)
{
struct nhrp_packet_header *hdr;
hdr = zbuf_push(zb, struct nhrp_packet_header);
- if (!hdr) return NULL;
+ if (!hdr)
+ return NULL;
- *hdr = (struct nhrp_packet_header) {
+ *hdr = (struct nhrp_packet_header){
.afnum = htons(family2afi(sockunion_family(src_nbma))),
- .protocol_type = htons(family2proto(sockunion_family(src_proto))),
+ .protocol_type =
+ htons(family2proto(sockunion_family(src_proto))),
.version = NHRP_VERSION_RFC2332,
.type = type,
.hop_count = 64,
};
zbuf_put(zb, sockunion_get_addr(src_nbma), hdr->src_nbma_address_len);
- zbuf_put(zb, sockunion_get_addr(src_proto), hdr->src_protocol_address_len);
- zbuf_put(zb, sockunion_get_addr(dst_proto), hdr->dst_protocol_address_len);
+ zbuf_put(zb, sockunion_get_addr(src_proto),
+ hdr->src_protocol_address_len);
+ zbuf_put(zb, sockunion_get_addr(dst_proto),
+ hdr->dst_protocol_address_len);
return hdr;
}
-struct nhrp_packet_header *nhrp_packet_pull(
- struct zbuf *zb,
- union sockunion *src_nbma,
- union sockunion *src_proto,
- union sockunion *dst_proto)
+struct nhrp_packet_header *nhrp_packet_pull(struct zbuf *zb,
+ union sockunion *src_nbma,
+ union sockunion *src_proto,
+ union sockunion *dst_proto)
{
struct nhrp_packet_header *hdr;
hdr = zbuf_pull(zb, struct nhrp_packet_header);
- if (!hdr) return NULL;
-
- sockunion_set(
- src_nbma, afi2family(htons(hdr->afnum)),
- zbuf_pulln(zb, hdr->src_nbma_address_len + hdr->src_nbma_subaddress_len),
- hdr->src_nbma_address_len + hdr->src_nbma_subaddress_len);
- sockunion_set(
- src_proto, proto2family(htons(hdr->protocol_type)),
- zbuf_pulln(zb, hdr->src_protocol_address_len),
- hdr->src_protocol_address_len);
- sockunion_set(
- dst_proto, proto2family(htons(hdr->protocol_type)),
- zbuf_pulln(zb, hdr->dst_protocol_address_len),
- hdr->dst_protocol_address_len);
+ if (!hdr)
+ return NULL;
+
+ sockunion_set(src_nbma, afi2family(htons(hdr->afnum)),
+ zbuf_pulln(zb,
+ hdr->src_nbma_address_len
+ + hdr->src_nbma_subaddress_len),
+ hdr->src_nbma_address_len + hdr->src_nbma_subaddress_len);
+ sockunion_set(src_proto, proto2family(htons(hdr->protocol_type)),
+ zbuf_pulln(zb, hdr->src_protocol_address_len),
+ hdr->src_protocol_address_len);
+ sockunion_set(dst_proto, proto2family(htons(hdr->protocol_type)),
+ zbuf_pulln(zb, hdr->dst_protocol_address_len),
+ hdr->dst_protocol_address_len);
return hdr;
}
uint16_t nhrp_packet_calculate_checksum(const uint8_t *pdu, uint16_t len)
{
- const uint16_t *pdu16 = (const uint16_t *) pdu;
+ const uint16_t *pdu16 = (const uint16_t *)pdu;
uint32_t csum = 0;
int i;
unsigned short size;
if (hdr->extension_offset)
- nhrp_ext_push(zb, hdr, NHRP_EXTENSION_END | NHRP_EXTENSION_FLAG_COMPULSORY);
+ nhrp_ext_push(zb, hdr,
+ NHRP_EXTENSION_END
+ | NHRP_EXTENSION_FLAG_COMPULSORY);
size = zb->tail - (uint8_t *)hdr;
hdr->packet_size = htons(size);
hdr->checksum = 0;
- hdr->checksum = nhrp_packet_calculate_checksum((uint8_t *) hdr, size);
+ hdr->checksum = nhrp_packet_calculate_checksum((uint8_t *)hdr, size);
}
-struct nhrp_cie_header *nhrp_cie_push(
- struct zbuf *zb,
- uint8_t code,
- const union sockunion *nbma,
- const union sockunion *proto)
+struct nhrp_cie_header *nhrp_cie_push(struct zbuf *zb, uint8_t code,
+ const union sockunion *nbma,
+ const union sockunion *proto)
{
struct nhrp_cie_header *cie;
cie = zbuf_push(zb, struct nhrp_cie_header);
- *cie = (struct nhrp_cie_header) {
+ *cie = (struct nhrp_cie_header){
.code = code,
};
if (nbma) {
}
if (proto) {
cie->protocol_address_len = sockunion_get_addrlen(proto);
- zbuf_put(zb, sockunion_get_addr(proto), cie->protocol_address_len);
+ zbuf_put(zb, sockunion_get_addr(proto),
+ cie->protocol_address_len);
}
return cie;
}
-struct nhrp_cie_header *nhrp_cie_pull(
- struct zbuf *zb,
- struct nhrp_packet_header *hdr,
- union sockunion *nbma,
- union sockunion *proto)
+struct nhrp_cie_header *nhrp_cie_pull(struct zbuf *zb,
+ struct nhrp_packet_header *hdr,
+ union sockunion *nbma,
+ union sockunion *proto)
{
struct nhrp_cie_header *cie;
cie = zbuf_pull(zb, struct nhrp_cie_header);
- if (!cie) return NULL;
-
- if (cie->nbma_address_len + cie->nbma_subaddress_len) {
- sockunion_set(
- nbma, afi2family(htons(hdr->afnum)),
- zbuf_pulln(zb, cie->nbma_address_len + cie->nbma_subaddress_len),
- cie->nbma_address_len + cie->nbma_subaddress_len);
+ if (!cie)
+ return NULL;
+
+ if (cie->nbma_address_len + cie->nbma_subaddress_len > 0) {
+ sockunion_set(nbma, afi2family(htons(hdr->afnum)),
+ zbuf_pulln(zb,
+ cie->nbma_address_len
+ + cie->nbma_subaddress_len),
+ cie->nbma_address_len + cie->nbma_subaddress_len);
} else {
sockunion_family(nbma) = AF_UNSPEC;
}
if (cie->protocol_address_len) {
- sockunion_set(
- proto, proto2family(htons(hdr->protocol_type)),
- zbuf_pulln(zb, cie->protocol_address_len),
- cie->protocol_address_len);
+ sockunion_set(proto, proto2family(htons(hdr->protocol_type)),
+ zbuf_pulln(zb, cie->protocol_address_len),
+ cie->protocol_address_len);
} else {
sockunion_family(proto) = AF_UNSPEC;
}
return cie;
}
-struct nhrp_extension_header *nhrp_ext_push(struct zbuf *zb, struct nhrp_packet_header *hdr, uint16_t type)
+struct nhrp_extension_header *
+nhrp_ext_push(struct zbuf *zb, struct nhrp_packet_header *hdr, uint16_t type)
{
struct nhrp_extension_header *ext;
ext = zbuf_push(zb, struct nhrp_extension_header);
- if (!ext) return NULL;
+ if (!ext)
+ return NULL;
if (!hdr->extension_offset)
- hdr->extension_offset = htons(zb->tail - (uint8_t*) hdr - sizeof(struct nhrp_extension_header));
+ hdr->extension_offset =
+ htons(zb->tail - (uint8_t *)hdr
+ - sizeof(struct nhrp_extension_header));
- *ext = (struct nhrp_extension_header) {
- .type = htons(type),
- .length = 0,
+ *ext = (struct nhrp_extension_header){
+ .type = htons(type), .length = 0,
};
return ext;
}
void nhrp_ext_complete(struct zbuf *zb, struct nhrp_extension_header *ext)
{
- ext->length = htons(zb->tail - (uint8_t*)ext - sizeof(struct nhrp_extension_header));
+ ext->length = htons(zb->tail - (uint8_t *)ext
+ - sizeof(struct nhrp_extension_header));
}
-struct nhrp_extension_header *nhrp_ext_pull(struct zbuf *zb, struct zbuf *payload)
+struct nhrp_extension_header *nhrp_ext_pull(struct zbuf *zb,
+ struct zbuf *payload)
{
struct nhrp_extension_header *ext;
uint16_t plen;
ext = zbuf_pull(zb, struct nhrp_extension_header);
- if (!ext) return NULL;
+ if (!ext)
+ return NULL;
plen = htons(ext->length);
zbuf_init(payload, zbuf_pulln(zb, plen), plen, plen);
return ext;
}
-void nhrp_ext_request(struct zbuf *zb, struct nhrp_packet_header *hdr, struct interface *ifp)
+void nhrp_ext_request(struct zbuf *zb, struct nhrp_packet_header *hdr,
+ struct interface *ifp)
{
/* Place holders for standard extensions */
- nhrp_ext_push(zb, hdr, NHRP_EXTENSION_FORWARD_TRANSIT_NHS | NHRP_EXTENSION_FLAG_COMPULSORY);
- nhrp_ext_push(zb, hdr, NHRP_EXTENSION_REVERSE_TRANSIT_NHS | NHRP_EXTENSION_FLAG_COMPULSORY);
- nhrp_ext_push(zb, hdr, NHRP_EXTENSION_RESPONDER_ADDRESS | NHRP_EXTENSION_FLAG_COMPULSORY);
+ nhrp_ext_push(zb, hdr,
+ NHRP_EXTENSION_FORWARD_TRANSIT_NHS
+ | NHRP_EXTENSION_FLAG_COMPULSORY);
+ nhrp_ext_push(zb, hdr,
+ NHRP_EXTENSION_REVERSE_TRANSIT_NHS
+ | NHRP_EXTENSION_FLAG_COMPULSORY);
+ nhrp_ext_push(zb, hdr,
+ NHRP_EXTENSION_RESPONDER_ADDRESS
+ | NHRP_EXTENSION_FLAG_COMPULSORY);
}
-int nhrp_ext_reply(struct zbuf *zb, struct nhrp_packet_header *hdr, struct interface *ifp, struct nhrp_extension_header *ext, struct zbuf *extpayload)
+int nhrp_ext_reply(struct zbuf *zb, struct nhrp_packet_header *hdr,
+ struct interface *ifp, struct nhrp_extension_header *ext,
+ struct zbuf *extpayload)
{
struct nhrp_interface *nifp = ifp->info;
struct nhrp_afi_data *ad = &nifp->afi[htons(hdr->afnum)];
return 0;
dst = nhrp_ext_push(zb, hdr, htons(ext->type));
- if (!dst) goto err;
+ if (!dst)
+ goto err;
switch (type) {
case NHRP_EXTENSION_RESPONDER_ADDRESS:
- cie = nhrp_cie_push(zb, NHRP_CODE_SUCCESS, &nifp->nbma, &ad->addr);
- if (!cie) goto err;
+ cie = nhrp_cie_push(zb, NHRP_CODE_SUCCESS, &nifp->nbma,
+ &ad->addr);
+ if (!cie)
+ goto err;
cie->holding_time = htons(ad->holdtime);
break;
default:
if (type & NHRP_EXTENSION_FLAG_COMPULSORY)
goto err;
- /* fallthru */
+ /* fallthru */
case NHRP_EXTENSION_FORWARD_TRANSIT_NHS:
case NHRP_EXTENSION_REVERSE_TRANSIT_NHS:
/* Supported compulsory extensions, and any
thread_add_read(master, nhrp_packet_recvraw, 0, fd, NULL);
zb = zbuf_alloc(1500);
- if (!zb) return 0;
+ if (!zb)
+ return 0;
len = zbuf_size(zb);
addrlen = sizeof(addr);
}
ifp = if_lookup_by_index(ifindex, VRF_DEFAULT);
- if (!ifp) goto err;
+ if (!ifp)
+ goto err;
p = nhrp_peer_get(ifp, &remote_nbma);
- if (!p) goto err;
+ if (!p)
+ goto err;
nhrp_peer_recv(p, zb);
nhrp_peer_unref(p);