diff options
author | Lou Berger <lberger@labn.net> | 2018-03-06 20:02:52 +0100 |
---|---|---|
committer | Lou Berger <lberger@labn.net> | 2018-03-06 20:04:32 +0100 |
commit | 996c93142d3abfab0f6d6c800474e22a8cfbdbc5 (patch) | |
tree | 2b28846d256c84cf7b7f1a8988fb3267c8611722 /nhrpd | |
parent | bgpd: another change to keep indent.py happy (diff) | |
download | frr-996c93142d3abfab0f6d6c800474e22a8cfbdbc5.tar.xz frr-996c93142d3abfab0f6d6c800474e22a8cfbdbc5.zip |
*: conform with COMMUNITY.md formatting rules, via 'make indent'
Signed-off-by: Lou Berger <lberger@labn.net>
Diffstat (limited to 'nhrpd')
-rw-r--r-- | nhrpd/debug.h | 25 | ||||
-rw-r--r-- | nhrpd/linux.c | 17 | ||||
-rw-r--r-- | nhrpd/list.h | 75 | ||||
-rw-r--r-- | nhrpd/netlink.h | 6 | ||||
-rw-r--r-- | nhrpd/netlink_arp.c | 71 | ||||
-rw-r--r-- | nhrpd/netlink_gre.c | 28 | ||||
-rw-r--r-- | nhrpd/nhrp_cache.c | 128 | ||||
-rw-r--r-- | nhrpd/nhrp_event.c | 95 | ||||
-rw-r--r-- | nhrpd/nhrp_interface.c | 156 | ||||
-rw-r--r-- | nhrpd/nhrp_main.c | 41 | ||||
-rw-r--r-- | nhrpd/nhrp_nhs.c | 139 | ||||
-rw-r--r-- | nhrpd/nhrp_packet.c | 184 | ||||
-rw-r--r-- | nhrpd/nhrp_peer.c | 407 | ||||
-rw-r--r-- | nhrpd/nhrp_protocol.h | 60 | ||||
-rw-r--r-- | nhrpd/nhrp_route.c | 92 | ||||
-rw-r--r-- | nhrpd/nhrp_shortcut.c | 145 | ||||
-rw-r--r-- | nhrpd/nhrp_vc.c | 56 | ||||
-rw-r--r-- | nhrpd/nhrp_vty.c | 309 | ||||
-rw-r--r-- | nhrpd/nhrpd.h | 179 | ||||
-rw-r--r-- | nhrpd/os.h | 6 | ||||
-rw-r--r-- | nhrpd/reqid.c | 14 | ||||
-rw-r--r-- | nhrpd/resolver.c | 52 | ||||
-rw-r--r-- | nhrpd/vici.c | 206 | ||||
-rw-r--r-- | nhrpd/vici.h | 14 | ||||
-rw-r--r-- | nhrpd/zbuf.c | 53 | ||||
-rw-r--r-- | nhrpd/zbuf.h | 30 | ||||
-rw-r--r-- | nhrpd/znl.c | 44 | ||||
-rw-r--r-- | nhrpd/znl.h | 4 |
28 files changed, 1527 insertions, 1109 deletions
diff --git a/nhrpd/debug.h b/nhrpd/debug.h index b1f49aa8b..db4bac791 100644 --- a/nhrpd/debug.h +++ b/nhrpd/debug.h @@ -20,23 +20,24 @@ extern unsigned int debug_flags; #if defined __STDC_VERSION__ && __STDC_VERSION__ >= 199901L -#define debugf(level, ...) \ - do { \ - if (unlikely(debug_flags & level)) \ - zlog_debug(__VA_ARGS__); \ - } while(0) +#define debugf(level, ...) \ + do { \ + if (unlikely(debug_flags & level)) \ + zlog_debug(__VA_ARGS__); \ + } while (0) #elif defined __GNUC__ -#define debugf(level, _args...) \ - do { \ - if (unlikely(debug_flags & level)) \ - zlog_debug(_args); \ - } while(0) +#define debugf(level, _args...) \ + do { \ + if (unlikely(debug_flags & level)) \ + zlog_debug(_args); \ + } while (0) #else -static inline void debugf(int level, const char *format, ...) { } +static inline void debugf(int level, const char *format, ...) +{ +} #endif - diff --git a/nhrpd/linux.c b/nhrpd/linux.c index 88804a87d..46a327b59 100644 --- a/nhrpd/linux.c +++ b/nhrpd/linux.c @@ -31,16 +31,17 @@ static int nhrp_socket_fd = -1; int os_socket(void) { if (nhrp_socket_fd < 0) - nhrp_socket_fd = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_NHRP)); + nhrp_socket_fd = + socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_NHRP)); return nhrp_socket_fd; } -int os_sendmsg(const uint8_t *buf, size_t len, int ifindex, const uint8_t *addr, size_t addrlen) +int os_sendmsg(const uint8_t *buf, size_t len, int ifindex, const uint8_t *addr, + size_t addrlen) { struct sockaddr_ll lladdr; struct iovec iov = { - .iov_base = (void*) buf, - .iov_len = len, + .iov_base = (void *)buf, .iov_len = len, }; struct msghdr msg = { .msg_name = &lladdr, @@ -67,12 +68,12 @@ int os_sendmsg(const uint8_t *buf, size_t len, int ifindex, const uint8_t *addr, return 0; } -int os_recvmsg(uint8_t *buf, size_t *len, int *ifindex, uint8_t *addr, size_t *addrlen) +int os_recvmsg(uint8_t *buf, size_t *len, int *ifindex, uint8_t *addr, + size_t *addrlen) { struct sockaddr_ll lladdr; struct iovec iov = { - .iov_base = buf, - .iov_len = *len, + .iov_base = buf, .iov_len = *len, }; struct msghdr msg = { .msg_name = &lladdr, @@ -89,7 +90,7 @@ int os_recvmsg(uint8_t *buf, size_t *len, int *ifindex, uint8_t *addr, size_t *a *len = r; *ifindex = lladdr.sll_ifindex; - if (*addrlen <= (size_t) lladdr.sll_addr) { + if (*addrlen <= (size_t)lladdr.sll_addr) { if (memcmp(lladdr.sll_addr, "\x00\x00\x00\x00", 4) != 0) { memcpy(addr, lladdr.sll_addr, lladdr.sll_halen); *addrlen = lladdr.sll_halen; diff --git a/nhrpd/list.h b/nhrpd/list.h index 32f21ed5e..ee7f1c440 100644 --- a/nhrpd/list.h +++ b/nhrpd/list.h @@ -25,9 +25,11 @@ #endif #ifndef container_of -#define container_of(ptr, type, member) ({ \ - const typeof( ((type *)0)->member ) *__mptr = (ptr); \ - (type *)( (char *)__mptr - offsetof(type,member) );}) +#define container_of(ptr, type, member) \ + ({ \ + const typeof(((type *)0)->member) *__mptr = (ptr); \ + (type *)((char *)__mptr - offsetof(type, member)); \ + }) #endif struct hlist_head { @@ -73,7 +75,8 @@ static inline void hlist_add_head(struct hlist_node *n, struct hlist_head *h) h->first = n; } -static inline void hlist_add_after(struct hlist_node *n, struct hlist_node *prev) +static inline void hlist_add_after(struct hlist_node *n, + struct hlist_node *prev) { n->next = prev->next; n->pprev = &prev->next; @@ -92,21 +95,34 @@ static inline struct hlist_node **hlist_tail_ptr(struct hlist_head *h) #define hlist_entry(ptr, type, member) container_of(ptr,type,member) -#define hlist_for_each(pos, head) \ +#define hlist_for_each(pos, head) \ for (pos = (head)->first; pos; pos = pos->next) -#define hlist_for_each_safe(pos, n, head) \ - for (pos = (head)->first; pos && ({ n = pos->next; 1; }); pos = n) +#define hlist_for_each_safe(pos, n, head) \ + for (pos = (head)->first; pos && ({ \ + n = pos->next; \ + 1; \ + }); \ + pos = n) -#define hlist_for_each_entry(tpos, pos, head, member) \ - for (pos = (head)->first; pos && \ - ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \ +#define hlist_for_each_entry(tpos, pos, head, member) \ + for (pos = (head)->first; \ + pos && ({ \ + tpos = hlist_entry(pos, typeof(*tpos), member); \ + 1; \ + }); \ pos = pos->next) -#define hlist_for_each_entry_safe(tpos, pos, n, head, member) \ - for (pos = (head)->first; \ - pos && ({ n = pos->next; 1; }) && \ - ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \ +#define hlist_for_each_entry_safe(tpos, pos, n, head, member) \ + for (pos = (head)->first; \ + pos && ({ \ + n = pos->next; \ + 1; \ + }) \ + && ({ \ + tpos = hlist_entry(pos, typeof(*tpos), member); \ + 1; \ + }); \ pos = n) @@ -122,8 +138,7 @@ static inline void list_init(struct list_head *list) list->prev = list; } -static inline void __list_add(struct list_head *new, - struct list_head *prev, +static inline void __list_add(struct list_head *new, struct list_head *prev, struct list_head *next) { next->prev = new; @@ -142,7 +157,7 @@ static inline void list_add_tail(struct list_head *new, struct list_head *head) __list_add(new, head->prev, head); } -static inline void __list_del(struct list_head * prev, struct list_head * next) +static inline void __list_del(struct list_head *prev, struct list_head *next) { next->prev = prev; prev->next = next; @@ -165,27 +180,27 @@ static inline int list_empty(const struct list_head *n) return !list_hashed(n); } -#define list_next(ptr, type, member) \ - (list_hashed(ptr) ? container_of((ptr)->next,type,member) : NULL) +#define list_next(ptr, type, member) \ + (list_hashed(ptr) ? container_of((ptr)->next, type, member) : NULL) #define list_entry(ptr, type, member) container_of(ptr,type,member) -#define list_for_each(pos, head) \ +#define list_for_each(pos, head) \ for (pos = (head)->next; pos != (head); pos = pos->next) -#define list_for_each_safe(pos, n, head) \ - for (pos = (head)->next, n = pos->next; pos != (head); \ - pos = n, n = pos->next) +#define list_for_each_safe(pos, n, head) \ + for (pos = (head)->next, n = pos->next; pos != (head); \ + pos = n, n = pos->next) -#define list_for_each_entry(pos, head, member) \ - for (pos = list_entry((head)->next, typeof(*pos), member); \ - &pos->member != (head); \ +#define list_for_each_entry(pos, head, member) \ + for (pos = list_entry((head)->next, typeof(*pos), member); \ + &pos->member != (head); \ pos = list_entry(pos->member.next, typeof(*pos), member)) -#define list_for_each_entry_safe(pos, n, head, member) \ - for (pos = list_entry((head)->next, typeof(*pos), member), \ - n = list_entry(pos->member.next, typeof(*pos), member); \ - &pos->member != (head); \ +#define list_for_each_entry_safe(pos, n, head, member) \ + for (pos = list_entry((head)->next, typeof(*pos), member), \ + n = list_entry(pos->member.next, typeof(*pos), member); \ + &pos->member != (head); \ pos = n, n = list_entry(n->member.next, typeof(*n), member)) #endif diff --git a/nhrpd/netlink.h b/nhrpd/netlink.h index e8dc22adf..74cb81daa 100644 --- a/nhrpd/netlink.h +++ b/nhrpd/netlink.h @@ -17,8 +17,10 @@ extern int netlink_req_fd; void netlink_init(void); int netlink_configure_arp(unsigned int ifindex, int pf); -void netlink_update_binding(struct interface *ifp, union sockunion *proto, union sockunion *nbma); +void netlink_update_binding(struct interface *ifp, union sockunion *proto, + union sockunion *nbma); void netlink_set_nflog_group(int nlgroup); -void netlink_gre_get_info(unsigned int ifindex, uint32_t *gre_key, unsigned int *link_index, struct in_addr *saddr); +void netlink_gre_get_info(unsigned int ifindex, uint32_t *gre_key, + unsigned int *link_index, struct in_addr *saddr); void netlink_gre_set_link(unsigned int ifindex, unsigned int link_index); diff --git a/nhrpd/netlink_arp.c b/nhrpd/netlink_arp.c index 425526ced..af78b3d9e 100644 --- a/nhrpd/netlink_arp.c +++ b/nhrpd/netlink_arp.c @@ -27,23 +27,27 @@ static int netlink_listen_fd = -1; typedef void (*netlink_dispatch_f)(struct nlmsghdr *msg, struct zbuf *zb); -void netlink_update_binding(struct interface *ifp, union sockunion *proto, union sockunion *nbma) +void netlink_update_binding(struct interface *ifp, union sockunion *proto, + union sockunion *nbma) { struct nlmsghdr *n; struct ndmsg *ndm; struct zbuf *zb = zbuf_alloc(512); - n = znl_nlmsg_push(zb, nbma ? RTM_NEWNEIGH : RTM_DELNEIGH, NLM_F_REQUEST | NLM_F_REPLACE | NLM_F_CREATE); + n = znl_nlmsg_push(zb, nbma ? RTM_NEWNEIGH : RTM_DELNEIGH, + NLM_F_REQUEST | NLM_F_REPLACE | NLM_F_CREATE); ndm = znl_push(zb, sizeof(*ndm)); - *ndm = (struct ndmsg) { + *ndm = (struct ndmsg){ .ndm_family = sockunion_family(proto), .ndm_ifindex = ifp->ifindex, .ndm_type = RTN_UNICAST, .ndm_state = nbma ? NUD_REACHABLE : NUD_FAILED, }; - znl_rta_push(zb, NDA_DST, sockunion_get_addr(proto), family2addrsize(sockunion_family(proto))); + znl_rta_push(zb, NDA_DST, sockunion_get_addr(proto), + family2addrsize(sockunion_family(proto))); if (nbma) - znl_rta_push(zb, NDA_LLADDR, sockunion_get_addr(nbma), family2addrsize(sockunion_family(nbma))); + znl_rta_push(zb, NDA_LLADDR, sockunion_get_addr(nbma), + family2addrsize(sockunion_family(nbma))); znl_nlmsg_complete(zb, n); zbuf_send(zb, netlink_req_fd); zbuf_recv(zb, netlink_req_fd); @@ -63,14 +67,16 @@ static void netlink_neigh_msg(struct nlmsghdr *msg, struct zbuf *zb) int state; ndm = znl_pull(zb, sizeof(*ndm)); - if (!ndm) return; + if (!ndm) + return; sockunion_family(&addr) = AF_UNSPEC; while ((rta = znl_rta_pull(zb, &payload)) != NULL) { len = zbuf_used(&payload); switch (rta->rta_type) { case NDA_DST: - sockunion_set(&addr, ndm->ndm_family, zbuf_pulln(&payload, len), len); + sockunion_set(&addr, ndm->ndm_family, + zbuf_pulln(&payload, len), len); break; } } @@ -85,19 +91,20 @@ static void netlink_neigh_msg(struct nlmsghdr *msg, struct zbuf *zb) if (msg->nlmsg_type == RTM_GETNEIGH) { debugf(NHRP_DEBUG_KERNEL, "Netlink: who-has %s dev %s", - sockunion2str(&addr, buf, sizeof buf), - ifp->name); + sockunion2str(&addr, buf, sizeof buf), ifp->name); if (c->cur.type >= NHRP_CACHE_CACHED) { nhrp_cache_set_used(c, 1); - netlink_update_binding(ifp, &addr, &c->cur.peer->vc->remote.nbma); + netlink_update_binding(ifp, &addr, + &c->cur.peer->vc->remote.nbma); } } else { debugf(NHRP_DEBUG_KERNEL, "Netlink: update %s dev %s nud %x", - sockunion2str(&addr, buf, sizeof buf), - ifp->name, ndm->ndm_state); + sockunion2str(&addr, buf, sizeof buf), ifp->name, + ndm->ndm_state); - state = (msg->nlmsg_type == RTM_NEWNEIGH) ? ndm->ndm_state : NUD_FAILED; + state = (msg->nlmsg_type == RTM_NEWNEIGH) ? ndm->ndm_state + : NUD_FAILED; nhrp_cache_set_used(c, state == NUD_REACHABLE); } } @@ -112,8 +119,9 @@ static int netlink_route_recv(struct thread *t) zbuf_init(&zb, buf, sizeof(buf), 0); while (zbuf_recv(&zb, fd) > 0) { while ((n = znl_nlmsg_pull(&zb, &payload)) != 0) { - debugf(NHRP_DEBUG_KERNEL, "Netlink: Received msg_type %u, msg_flags %u", - n->nlmsg_type, n->nlmsg_flags); + debugf(NHRP_DEBUG_KERNEL, + "Netlink: Received msg_type %u, msg_flags %u", + n->nlmsg_type, n->nlmsg_flags); switch (n->nlmsg_type) { case RTM_GETNEIGH: case RTM_NEWNEIGH: @@ -136,9 +144,10 @@ static void netlink_log_register(int fd, int group) struct nfulnl_msg_config_cmd cmd; struct zbuf *zb = zbuf_alloc(512); - n = znl_nlmsg_push(zb, (NFNL_SUBSYS_ULOG<<8) | NFULNL_MSG_CONFIG, NLM_F_REQUEST | NLM_F_ACK); + n = znl_nlmsg_push(zb, (NFNL_SUBSYS_ULOG << 8) | NFULNL_MSG_CONFIG, + NLM_F_REQUEST | NLM_F_ACK); nf = znl_push(zb, sizeof(*nf)); - *nf = (struct nfgenmsg) { + *nf = (struct nfgenmsg){ .nfgen_family = AF_UNSPEC, .version = NFNETLINK_V0, .res_id = htons(group), @@ -161,7 +170,8 @@ static void netlink_log_indication(struct nlmsghdr *msg, struct zbuf *zb) uint32_t *in_ndx = NULL; nf = znl_pull(zb, sizeof(*nf)); - if (!nf) return; + if (!nf) + return; memset(&pktpl, 0, sizeof(pktpl)); while ((rta = znl_rta_pull(zb, &rtapl)) != NULL) { @@ -175,10 +185,10 @@ static void netlink_log_indication(struct nlmsghdr *msg, struct zbuf *zb) case NFULA_PAYLOAD: pktpl = rtapl; break; - /* NFULA_HWHDR exists and is supposed to contain source - * hardware address. However, for ip_gre it seems to be - * the nexthop destination address if the packet matches - * route. */ + /* NFULA_HWHDR exists and is supposed to contain source + * hardware address. However, for ip_gre it seems to be + * the nexthop destination address if the packet matches + * route. */ } } @@ -204,10 +214,11 @@ static int netlink_log_recv(struct thread *t) zbuf_init(&zb, buf, sizeof(buf), 0); while (zbuf_recv(&zb, fd) > 0) { while ((n = znl_nlmsg_pull(&zb, &payload)) != 0) { - debugf(NHRP_DEBUG_KERNEL, "Netlink-log: Received msg_type %u, msg_flags %u", - n->nlmsg_type, n->nlmsg_flags); + debugf(NHRP_DEBUG_KERNEL, + "Netlink-log: Received msg_type %u, msg_flags %u", + n->nlmsg_type, n->nlmsg_flags); switch (n->nlmsg_type) { - case (NFNL_SUBSYS_ULOG<<8) | NFULNL_MSG_PACKET: + case (NFNL_SUBSYS_ULOG << 8) | NFULNL_MSG_PACKET: netlink_log_indication(n, &payload); break; } @@ -229,7 +240,7 @@ void netlink_set_nflog_group(int nlgroup) } netlink_nflog_group = nlgroup; if (nlgroup) { - netlink_log_fd = znl_open(NETLINK_NETFILTER, 0); + netlink_log_fd = znl_open(NETLINK_NETFILTER, 0); if (netlink_log_fd < 0) return; @@ -249,8 +260,7 @@ void netlink_init(void) if (netlink_listen_fd < 0) return; - thread_add_read(master, netlink_route_recv, 0, netlink_listen_fd, - NULL); + thread_add_read(master, netlink_route_recv, 0, netlink_listen_fd, NULL); } int netlink_configure_arp(unsigned int ifindex, int pf) @@ -263,11 +273,12 @@ int netlink_configure_arp(unsigned int ifindex, int pf) n = znl_nlmsg_push(zb, RTM_SETNEIGHTBL, NLM_F_REQUEST | NLM_F_REPLACE); ndtm = znl_push(zb, sizeof(*ndtm)); - *ndtm = (struct ndtmsg) { + *ndtm = (struct ndtmsg){ .ndtm_family = pf, }; - znl_rta_push(zb, NDTA_NAME, pf == AF_INET ? "arp_cache" : "ndisc_cache", 10); + znl_rta_push(zb, NDTA_NAME, pf == AF_INET ? "arp_cache" : "ndisc_cache", + 10); rta = znl_rta_nested_push(zb, NDTA_PARMS); znl_rta_push_u32(zb, NDTPA_IFINDEX, ifindex); diff --git a/nhrpd/netlink_gre.c b/nhrpd/netlink_gre.c index 93998dc5f..75ecaa70c 100644 --- a/nhrpd/netlink_gre.c +++ b/nhrpd/netlink_gre.c @@ -20,7 +20,8 @@ #include "netlink.h" #include "znl.h" -static int __netlink_gre_get_data(struct zbuf *zb, struct zbuf *data, int ifindex) +static int __netlink_gre_get_data(struct zbuf *zb, struct zbuf *data, + int ifindex) { struct nlmsghdr *n; struct ifinfomsg *ifi; @@ -31,17 +32,18 @@ static int __netlink_gre_get_data(struct zbuf *zb, struct zbuf *data, int ifinde n = znl_nlmsg_push(zb, RTM_GETLINK, NLM_F_REQUEST); ifi = znl_push(zb, sizeof(*ifi)); - *ifi = (struct ifinfomsg) { + *ifi = (struct ifinfomsg){ .ifi_index = ifindex, }; znl_nlmsg_complete(zb, n); - if (zbuf_send(zb, netlink_req_fd) < 0 || - zbuf_recv(zb, netlink_req_fd) < 0) + if (zbuf_send(zb, netlink_req_fd) < 0 + || zbuf_recv(zb, netlink_req_fd) < 0) return -1; n = znl_nlmsg_pull(zb, &payload); - if (!n) return -1; + if (!n) + return -1; if (n->nlmsg_type != RTM_NEWLINK) return -1; @@ -50,8 +52,9 @@ static int __netlink_gre_get_data(struct zbuf *zb, struct zbuf *data, int ifinde if (!ifi) return -1; - debugf(NHRP_DEBUG_KERNEL, "netlink-link-gre: ifindex %u, receive msg_type %u, msg_flags %u", - ifi->ifi_index, n->nlmsg_type, n->nlmsg_flags); + debugf(NHRP_DEBUG_KERNEL, + "netlink-link-gre: ifindex %u, receive msg_type %u, msg_flags %u", + ifi->ifi_index, n->nlmsg_type, n->nlmsg_flags); if (ifi->ifi_index != ifindex) return -1; @@ -59,19 +62,22 @@ static int __netlink_gre_get_data(struct zbuf *zb, struct zbuf *data, int ifinde while ((rta = znl_rta_pull(&payload, &rtapayload)) != NULL) if (rta->rta_type == IFLA_LINKINFO) break; - if (!rta) return -1; + if (!rta) + return -1; payload = rtapayload; while ((rta = znl_rta_pull(&payload, &rtapayload)) != NULL) if (rta->rta_type == IFLA_INFO_DATA) break; - if (!rta) return -1; + if (!rta) + return -1; *data = rtapayload; return 0; } -void netlink_gre_get_info(unsigned int ifindex, uint32_t *gre_key, unsigned int *link_index, struct in_addr *saddr) +void netlink_gre_get_info(unsigned int ifindex, uint32_t *gre_key, + unsigned int *link_index, struct in_addr *saddr) { struct zbuf *zb = zbuf_alloc(8192), data, rtapl; struct rtattr *rta; @@ -115,7 +121,7 @@ void netlink_gre_set_link(unsigned int ifindex, unsigned int link_index) n = znl_nlmsg_push(zb, RTM_NEWLINK, NLM_F_REQUEST); ifi = znl_push(zb, sizeof(*ifi)); - *ifi = (struct ifinfomsg) { + *ifi = (struct ifinfomsg){ .ifi_index = ifindex, }; rta_info = znl_rta_nested_push(zb, IFLA_LINKINFO); diff --git a/nhrpd/nhrp_cache.c b/nhrpd/nhrp_cache.c index 3ff1a342d..ffc8b5a9b 100644 --- a/nhrpd/nhrp_cache.c +++ b/nhrpd/nhrp_cache.c @@ -19,15 +19,15 @@ DEFINE_MTYPE_STATIC(NHRPD, NHRP_CACHE, "NHRP cache entry") unsigned long nhrp_cache_counts[NHRP_CACHE_NUM_TYPES]; -const char * const nhrp_cache_type_str[] = { - [NHRP_CACHE_INVALID] = "invalid", - [NHRP_CACHE_INCOMPLETE] = "incomplete", - [NHRP_CACHE_NEGATIVE] = "negative", - [NHRP_CACHE_CACHED] = "cached", - [NHRP_CACHE_DYNAMIC] = "dynamic", - [NHRP_CACHE_NHS] = "nhs", - [NHRP_CACHE_STATIC] = "static", - [NHRP_CACHE_LOCAL] = "local", +const char *const nhrp_cache_type_str[] = { + [NHRP_CACHE_INVALID] = "invalid", + [NHRP_CACHE_INCOMPLETE] = "incomplete", + [NHRP_CACHE_NEGATIVE] = "negative", + [NHRP_CACHE_CACHED] = "cached", + [NHRP_CACHE_DYNAMIC] = "dynamic", + [NHRP_CACHE_NHS] = "nhs", + [NHRP_CACHE_STATIC] = "static", + [NHRP_CACHE_LOCAL] = "local", }; static unsigned int nhrp_cache_protocol_key(void *peer_data) @@ -49,12 +49,13 @@ static void *nhrp_cache_alloc(void *data) p = XMALLOC(MTYPE_NHRP_CACHE, sizeof(struct nhrp_cache)); if (p) { - *p = (struct nhrp_cache) { + *p = (struct nhrp_cache){ .cur.type = NHRP_CACHE_INVALID, .new.type = NHRP_CACHE_INVALID, .remote_addr = key->remote_addr, .ifp = key->ifp, - .notifier_list = NOTIFIER_LIST_INITIALIZER(&p->notifier_list), + .notifier_list = + NOTIFIER_LIST_INITIALIZER(&p->notifier_list), }; nhrp_cache_counts[p->cur.type]++; } @@ -75,15 +76,16 @@ static void nhrp_cache_free(struct nhrp_cache *c) XFREE(MTYPE_NHRP_CACHE, c); } -struct nhrp_cache *nhrp_cache_get(struct interface *ifp, union sockunion *remote_addr, int create) +struct nhrp_cache *nhrp_cache_get(struct interface *ifp, + union sockunion *remote_addr, int create) { struct nhrp_interface *nifp = ifp->info; struct nhrp_cache key; if (!nifp->cache_hash) { - nifp->cache_hash = hash_create(nhrp_cache_protocol_key, - nhrp_cache_protocol_cmp, - "NHRP Cache"); + nifp->cache_hash = + hash_create(nhrp_cache_protocol_key, + nhrp_cache_protocol_cmp, "NHRP Cache"); if (!nifp->cache_hash) return NULL; } @@ -91,7 +93,8 @@ struct nhrp_cache *nhrp_cache_get(struct interface *ifp, union sockunion *remote key.remote_addr = *remote_addr; key.ifp = ifp; - return hash_get(nifp->cache_hash, &key, create ? nhrp_cache_alloc : NULL); + return hash_get(nifp->cache_hash, &key, + create ? nhrp_cache_alloc : NULL); } static int nhrp_cache_do_free(struct thread *t) @@ -119,8 +122,10 @@ static void nhrp_cache_update_route(struct nhrp_cache *c) sockunion2hostprefix(&c->remote_addr, &pfx); if (p && nhrp_peer_check(p, 1)) { - netlink_update_binding(p->ifp, &c->remote_addr, &p->vc->remote.nbma); - nhrp_route_announce(1, c->cur.type, &pfx, c->ifp, NULL, c->cur.mtu); + netlink_update_binding(p->ifp, &c->remote_addr, + &p->vc->remote.nbma); + nhrp_route_announce(1, c->cur.type, &pfx, c->ifp, NULL, + c->cur.mtu); if (c->cur.type >= NHRP_CACHE_DYNAMIC) { nhrp_route_update_nhrp(&pfx, c->ifp); c->nhrp_route_installed = 1; @@ -140,15 +145,18 @@ static void nhrp_cache_update_route(struct nhrp_cache *c) if (c->route_installed) { sockunion2hostprefix(&c->remote_addr, &pfx); notifier_call(&c->notifier_list, NOTIFY_CACHE_DOWN); - nhrp_route_announce(0, c->cur.type, &pfx, NULL, NULL, 0); + nhrp_route_announce(0, c->cur.type, &pfx, NULL, NULL, + 0); c->route_installed = 0; } } } -static void nhrp_cache_peer_notifier(struct notifier_block *n, unsigned long cmd) +static void nhrp_cache_peer_notifier(struct notifier_block *n, + unsigned long cmd) { - struct nhrp_cache *c = container_of(n, struct nhrp_cache, peer_notifier); + struct nhrp_cache *c = + container_of(n, struct nhrp_cache, peer_notifier); switch (cmd) { case NOTIFY_PEER_UP: @@ -183,8 +191,8 @@ static void nhrp_cache_update_timers(struct nhrp_cache *c) switch (c->cur.type) { case NHRP_CACHE_INVALID: if (!c->t_auth) - thread_add_timer_msec(master, nhrp_cache_do_free, c, - 10, &c->t_timeout); + thread_add_timer_msec(master, nhrp_cache_do_free, c, 10, + &c->t_timeout); break; default: if (c->cur.expires) @@ -200,15 +208,16 @@ static void nhrp_cache_authorize_binding(struct nhrp_reqid *r, void *arg) struct nhrp_cache *c = container_of(r, struct nhrp_cache, eventid); char buf[SU_ADDRSTRLEN]; - debugf(NHRP_DEBUG_COMMON, "cache: %s %s: %s", - c->ifp->name, sockunion2str(&c->remote_addr, buf, sizeof buf), - (const char *) arg); + debugf(NHRP_DEBUG_COMMON, "cache: %s %s: %s", c->ifp->name, + sockunion2str(&c->remote_addr, buf, sizeof buf), + (const char *)arg); nhrp_reqid_free(&nhrp_event_reqid, r); if (arg && strcmp(arg, "accept") == 0) { if (c->cur.peer) { - netlink_update_binding(c->cur.peer->ifp, &c->remote_addr, NULL); + netlink_update_binding(c->cur.peer->ifp, + &c->remote_addr, NULL); nhrp_peer_notify_del(c->cur.peer, &c->peer_notifier); nhrp_peer_unref(c->cur.peer); } @@ -218,7 +227,8 @@ static void nhrp_cache_authorize_binding(struct nhrp_reqid *r, void *arg) c->cur.peer = nhrp_peer_ref(c->cur.peer); nhrp_cache_reset_new(c); if (c->cur.peer) - nhrp_peer_notify_add(c->cur.peer, &c->peer_notifier, nhrp_cache_peer_notifier); + nhrp_peer_notify_add(c->cur.peer, &c->peer_notifier, + nhrp_cache_peer_notifier); nhrp_cache_update_route(c); notifier_call(&c->notifier_list, NOTIFY_CACHE_BINDING_CHANGE); } else { @@ -232,20 +242,23 @@ static int nhrp_cache_do_auth_timeout(struct thread *t) { struct nhrp_cache *c = THREAD_ARG(t); c->t_auth = NULL; - nhrp_cache_authorize_binding(&c->eventid, (void *) "timeout"); + nhrp_cache_authorize_binding(&c->eventid, (void *)"timeout"); return 0; } -static void nhrp_cache_newpeer_notifier(struct notifier_block *n, unsigned long cmd) +static void nhrp_cache_newpeer_notifier(struct notifier_block *n, + unsigned long cmd) { - struct nhrp_cache *c = container_of(n, struct nhrp_cache, newpeer_notifier); + struct nhrp_cache *c = + container_of(n, struct nhrp_cache, newpeer_notifier); switch (cmd) { case NOTIFY_PEER_UP: if (nhrp_peer_check(c->new.peer, 1)) { - evmgr_notify("authorize-binding", c, nhrp_cache_authorize_binding); - thread_add_timer(master, nhrp_cache_do_auth_timeout, - c, 10, &c->t_auth); + evmgr_notify("authorize-binding", c, + nhrp_cache_authorize_binding); + thread_add_timer(master, nhrp_cache_do_auth_timeout, c, + 10, &c->t_auth); } break; case NOTIFY_PEER_DOWN: @@ -255,7 +268,9 @@ static void nhrp_cache_newpeer_notifier(struct notifier_block *n, unsigned long } } -int nhrp_cache_update_binding(struct nhrp_cache *c, enum nhrp_cache_type type, int holding_time, struct nhrp_peer *p, uint32_t mtu, union sockunion *nbma_oa) +int nhrp_cache_update_binding(struct nhrp_cache *c, enum nhrp_cache_type type, + int holding_time, struct nhrp_peer *p, + uint32_t mtu, union sockunion *nbma_oa) { if (c->cur.type > type || c->new.type > type) { nhrp_peer_unref(p); @@ -269,7 +284,8 @@ int nhrp_cache_update_binding(struct nhrp_cache *c, enum nhrp_cache_type type, i mtu = 0; /* Opennhrp announces nbma mtu, but we use protocol mtu. * This heuristic tries to fix up it. */ - if (mtu > 1420) mtu = (mtu & -16) - 80; + if (mtu > 1420) + mtu = (mtu & -16) - 80; break; default: mtu = 0; @@ -278,30 +294,37 @@ int nhrp_cache_update_binding(struct nhrp_cache *c, enum nhrp_cache_type type, i nhrp_cache_reset_new(c); if (c->cur.type == type && c->cur.peer == p && c->cur.mtu == mtu) { - if (holding_time > 0) c->cur.expires = monotime(NULL) + holding_time; - if (nbma_oa) c->cur.remote_nbma_natoa = *nbma_oa; - else memset(&c->cur.remote_nbma_natoa, 0, sizeof c->cur.remote_nbma_natoa); + if (holding_time > 0) + c->cur.expires = monotime(NULL) + holding_time; + if (nbma_oa) + c->cur.remote_nbma_natoa = *nbma_oa; + else + memset(&c->cur.remote_nbma_natoa, 0, + sizeof c->cur.remote_nbma_natoa); nhrp_peer_unref(p); } else { c->new.type = type; c->new.peer = p; c->new.mtu = mtu; - if (nbma_oa) c->new.remote_nbma_natoa = *nbma_oa; + if (nbma_oa) + c->new.remote_nbma_natoa = *nbma_oa; if (holding_time > 0) c->new.expires = monotime(NULL) + holding_time; else if (holding_time < 0) nhrp_cache_reset_new(c); - if (c->new.type == NHRP_CACHE_INVALID || - c->new.type >= NHRP_CACHE_STATIC || - c->map) { - nhrp_cache_authorize_binding(&c->eventid, (void *) "accept"); + if (c->new.type == NHRP_CACHE_INVALID + || c->new.type >= NHRP_CACHE_STATIC || c->map) { + nhrp_cache_authorize_binding(&c->eventid, + (void *)"accept"); } else { - nhrp_peer_notify_add(c->new.peer, &c->newpeer_notifier, nhrp_cache_newpeer_notifier); - nhrp_cache_newpeer_notifier(&c->newpeer_notifier, NOTIFY_PEER_UP); - thread_add_timer(master, nhrp_cache_do_auth_timeout, - c, 60, &c->t_auth); + nhrp_peer_notify_add(c->new.peer, &c->newpeer_notifier, + nhrp_cache_newpeer_notifier); + nhrp_cache_newpeer_notifier(&c->newpeer_notifier, + NOTIFY_PEER_UP); + thread_add_timer(master, nhrp_cache_do_auth_timeout, c, + 60, &c->t_auth); } } nhrp_cache_update_timers(c); @@ -327,19 +350,20 @@ static void nhrp_cache_iterator(struct hash_backet *b, void *ctx) ic->cb(b->data, ic->ctx); } -void nhrp_cache_foreach(struct interface *ifp, void (*cb)(struct nhrp_cache *, void *), void *ctx) +void nhrp_cache_foreach(struct interface *ifp, + void (*cb)(struct nhrp_cache *, void *), void *ctx) { struct nhrp_interface *nifp = ifp->info; struct nhrp_cache_iterator_ctx ic = { - .cb = cb, - .ctx = ctx, + .cb = cb, .ctx = ctx, }; if (nifp->cache_hash) hash_iterate(nifp->cache_hash, nhrp_cache_iterator, &ic); } -void nhrp_cache_notify_add(struct nhrp_cache *c, struct notifier_block *n, notifier_fn_t fn) +void nhrp_cache_notify_add(struct nhrp_cache *c, struct notifier_block *n, + notifier_fn_t fn) { notifier_add(n, &c->notifier_list, fn); } diff --git a/nhrpd/nhrp_event.c b/nhrpd/nhrp_event.c index 4ee58a43e..e7adc971e 100644 --- a/nhrpd/nhrp_event.c +++ b/nhrpd/nhrp_event.c @@ -24,7 +24,7 @@ struct event_manager { struct zbuf ibuf; struct zbuf_queue obuf; int fd; - uint8_t ibuf_data[4*1024]; + uint8_t ibuf_data[4 * 1024]; }; static int evmgr_reconnect(struct thread *t); @@ -53,7 +53,7 @@ static void evmgr_recv_message(struct event_manager *evmgr, struct zbuf *zb) while (zbuf_may_pull_until(zb, "\n", &zl)) { len = zbuf_used(&zl) - 1; - if (len >= sizeof(buf)-1) + if (len >= sizeof(buf) - 1) continue; memcpy(buf, zbuf_pulln(&zl, len), len); buf[len] = 0; @@ -64,10 +64,13 @@ static void evmgr_recv_message(struct event_manager *evmgr, struct zbuf *zb) if (sscanf(buf, "result=%63s", result) != 1) continue; } - debugf(NHRP_DEBUG_EVENT, "evmgr: received: eventid=%d result=%s", eventid, result); + debugf(NHRP_DEBUG_EVENT, "evmgr: received: eventid=%d result=%s", + eventid, result); if (eventid && result[0]) { - struct nhrp_reqid *r = nhrp_reqid_lookup(&nhrp_event_reqid, eventid); - if (r) r->cb(r, result); + struct nhrp_reqid *r = + nhrp_reqid_lookup(&nhrp_event_reqid, eventid); + if (r) + r->cb(r, result); } } @@ -78,7 +81,7 @@ static int evmgr_read(struct thread *t) struct zbuf msg; evmgr->t_read = NULL; - if (zbuf_read(ibuf, evmgr->fd, (size_t) -1) < 0) { + if (zbuf_read(ibuf, evmgr->fd, (size_t)-1) < 0) { evmgr_connection_error(evmgr); return 0; } @@ -114,8 +117,9 @@ static void evmgr_hexdump(struct zbuf *zb, const uint8_t *val, size_t vallen) size_t i; char *ptr; - ptr = zbuf_pushn(zb, 2*vallen); - if (!ptr) return; + ptr = zbuf_pushn(zb, 2 * vallen); + if (!ptr) + return; for (i = 0; i < vallen; i++) { uint8_t b = val[i]; @@ -134,13 +138,15 @@ static void evmgr_put(struct zbuf *zb, const char *fmt, ...) va_start(va, fmt); for (pos = fmt; (nxt = strchr(pos, '%')) != NULL; pos = nxt + 2) { - zbuf_put(zb, pos, nxt-pos); + zbuf_put(zb, pos, nxt - pos); switch (nxt[1]) { case '%': zbuf_put8(zb, '%'); break; case 'u': - zb->tail += snprintf((char *) zb->tail, zbuf_tailroom(zb), "%u", va_arg(va, uint32_t)); + zb->tail += + snprintf((char *)zb->tail, zbuf_tailroom(zb), + "%u", va_arg(va, uint32_t)); break; case 's': str = va_arg(va, const char *); @@ -148,8 +154,9 @@ static void evmgr_put(struct zbuf *zb, const char *fmt, ...) break; case 'U': su = va_arg(va, const union sockunion *); - if (sockunion2str(su, (char *) zb->tail, zbuf_tailroom(zb))) - zb->tail += strlen((char *) zb->tail); + if (sockunion2str(su, (char *)zb->tail, + zbuf_tailroom(zb))) + zb->tail += strlen((char *)zb->tail); else zbuf_set_werror(zb); break; @@ -183,12 +190,13 @@ static int evmgr_reconnect(struct thread *t) int fd; evmgr->t_reconnect = NULL; - if (evmgr->fd >= 0 || !nhrp_event_socket_path) return 0; + if (evmgr->fd >= 0 || !nhrp_event_socket_path) + return 0; fd = sock_open_unix(nhrp_event_socket_path); if (fd < 0) { zlog_warn("%s: failure connecting nhrp-event socket: %s", - __PRETTY_FUNCTION__, strerror(errno)); + __PRETTY_FUNCTION__, strerror(errno)); zbufq_reset(&evmgr->obuf); thread_add_timer(master, evmgr_reconnect, evmgr, 10, &evmgr->t_reconnect); @@ -218,7 +226,7 @@ void evmgr_init(void) void evmgr_set_socket(const char *socket) { if (nhrp_event_socket_path) { - free((char *) nhrp_event_socket_path); + free((char *)nhrp_event_socket_path); nhrp_event_socket_path = NULL; } if (socket) @@ -230,7 +238,8 @@ void evmgr_terminate(void) { } -void evmgr_notify(const char *name, struct nhrp_cache *c, void (*cb)(struct nhrp_reqid *, void *)) +void evmgr_notify(const char *name, struct nhrp_cache *c, + void (*cb)(struct nhrp_reqid *, void *)) { struct event_manager *evmgr = &evmgr_connection; struct nhrp_vc *vc; @@ -239,51 +248,47 @@ void evmgr_notify(const char *name, struct nhrp_cache *c, void (*cb)(struct nhrp afi_t afi = family2afi(sockunion_family(&c->remote_addr)); if (!nhrp_event_socket_path) { - cb(&c->eventid, (void*) "accept"); + cb(&c->eventid, (void *)"accept"); return; } debugf(NHRP_DEBUG_EVENT, "evmgr: sending event %s", name); vc = c->new.peer ? c->new.peer->vc : NULL; - zb = zbuf_alloc(1024 + (vc ? (vc->local.certlen + vc->remote.certlen) * 2 : 0)); + zb = zbuf_alloc( + 1024 + (vc ? (vc->local.certlen + vc->remote.certlen) * 2 : 0)); if (cb) { nhrp_reqid_free(&nhrp_event_reqid, &c->eventid); - evmgr_put(zb, - "eventid=%u\n", - nhrp_reqid_alloc(&nhrp_event_reqid, &c->eventid, cb)); + evmgr_put(zb, "eventid=%u\n", + nhrp_reqid_alloc(&nhrp_event_reqid, &c->eventid, cb)); } evmgr_put(zb, - "event=%s\n" - "type=%s\n" - "old_type=%s\n" - "num_nhs=%u\n" - "interface=%s\n" - "local_addr=%U\n", - name, - nhrp_cache_type_str[c->new.type], - nhrp_cache_type_str[c->cur.type], - (unsigned int) nhrp_cache_counts[NHRP_CACHE_NHS], - c->ifp->name, - &nifp->afi[afi].addr); + "event=%s\n" + "type=%s\n" + "old_type=%s\n" + "num_nhs=%u\n" + "interface=%s\n" + "local_addr=%U\n", + name, nhrp_cache_type_str[c->new.type], + nhrp_cache_type_str[c->cur.type], + (unsigned int)nhrp_cache_counts[NHRP_CACHE_NHS], c->ifp->name, + &nifp->afi[afi].addr); if (vc) { evmgr_put(zb, - "vc_initiated=%s\n" - "local_nbma=%U\n" - "local_cert=%H\n" - "remote_addr=%U\n" - "remote_nbma=%U\n" - "remote_cert=%H\n", - c->new.peer->requested ? "yes" : "no", - &vc->local.nbma, - vc->local.cert, vc->local.certlen, - &c->remote_addr, &vc->remote.nbma, - vc->remote.cert, vc->remote.certlen); + "vc_initiated=%s\n" + "local_nbma=%U\n" + "local_cert=%H\n" + "remote_addr=%U\n" + "remote_nbma=%U\n" + "remote_cert=%H\n", + c->new.peer->requested ? "yes" : "no", + &vc->local.nbma, vc->local.cert, vc->local.certlen, + &c->remote_addr, &vc->remote.nbma, vc->remote.cert, + vc->remote.certlen); } evmgr_submit(evmgr, zb); } - diff --git a/nhrpd/nhrp_interface.c b/nhrpd/nhrp_interface.c index 67e3f41b3..fba045dae 100644 --- a/nhrpd/nhrp_interface.c +++ b/nhrpd/nhrp_interface.c @@ -25,7 +25,8 @@ static int nhrp_if_new_hook(struct interface *ifp) afi_t afi; nifp = XCALLOC(MTYPE_NHRP_IF, sizeof(struct nhrp_interface)); - if (!nifp) return 0; + if (!nifp) + return 0; ifp->info = nifp; nifp->ifp = ifp; @@ -66,9 +67,11 @@ void nhrp_interface_update_mtu(struct interface *ifp, afi_t afi) new_mtu = 0; if (new_mtu != if_ad->mtu) { - debugf(NHRP_DEBUG_IF, "%s: MTU changed to %d", ifp->name, new_mtu); + debugf(NHRP_DEBUG_IF, "%s: MTU changed to %d", ifp->name, + new_mtu); if_ad->mtu = new_mtu; - notifier_call(&nifp->notifier_list, NOTIFY_INTERFACE_MTU_CHANGED); + notifier_call(&nifp->notifier_list, + NOTIFY_INTERFACE_MTU_CHANGED); } } @@ -76,18 +79,21 @@ static void nhrp_interface_update_source(struct interface *ifp) { struct nhrp_interface *nifp = ifp->info; - if (!nifp->source || !nifp->nbmaifp || - (ifindex_t)nifp->linkidx == nifp->nbmaifp->ifindex) + if (!nifp->source || !nifp->nbmaifp + || (ifindex_t)nifp->linkidx == nifp->nbmaifp->ifindex) return; nifp->linkidx = nifp->nbmaifp->ifindex; - debugf(NHRP_DEBUG_IF, "%s: bound device index changed to %d", ifp->name, nifp->linkidx); + debugf(NHRP_DEBUG_IF, "%s: bound device index changed to %d", ifp->name, + nifp->linkidx); netlink_gre_set_link(ifp->ifindex, nifp->linkidx); } -static void nhrp_interface_interface_notifier(struct notifier_block *n, unsigned long cmd) +static void nhrp_interface_interface_notifier(struct notifier_block *n, + unsigned long cmd) { - struct nhrp_interface *nifp = container_of(n, struct nhrp_interface, nbmanifp_notifier); + struct nhrp_interface *nifp = + container_of(n, struct nhrp_interface, nbmanifp_notifier); struct interface *nbmaifp = nifp->nbmaifp; struct nhrp_interface *nbmanifp = nbmaifp->info; char buf[SU_ADDRSTRLEN]; @@ -100,10 +106,11 @@ static void nhrp_interface_interface_notifier(struct notifier_block *n, unsigned case NOTIFY_INTERFACE_ADDRESS_CHANGED: nifp->nbma = nbmanifp->afi[AFI_IP].addr; nhrp_interface_update(nifp->ifp); - notifier_call(&nifp->notifier_list, NOTIFY_INTERFACE_NBMA_CHANGED); + notifier_call(&nifp->notifier_list, + NOTIFY_INTERFACE_NBMA_CHANGED); debugf(NHRP_DEBUG_IF, "%s: NBMA change: address %s", - nifp->ifp->name, - sockunion2str(&nifp->nbma, buf, sizeof buf)); + nifp->ifp->name, + sockunion2str(&nifp->nbma, buf, sizeof buf)); break; } } @@ -117,19 +124,22 @@ static void nhrp_interface_update_nbma(struct interface *ifp) sockunion_family(&nbma) = AF_UNSPEC; if (nifp->source) - nbmaifp = if_lookup_by_name(nifp->source, VRF_DEFAULT); + nbmaifp = if_lookup_by_name(nifp->source, VRF_DEFAULT); switch (ifp->ll_type) { case ZEBRA_LLT_IPGRE: { - struct in_addr saddr = {0}; - netlink_gre_get_info(ifp->ifindex, &nifp->grekey, &nifp->linkidx, &saddr); - debugf(NHRP_DEBUG_IF, "%s: GRE: %x %x %x", ifp->name, nifp->grekey, nifp->linkidx, saddr.s_addr); - if (saddr.s_addr) - sockunion_set(&nbma, AF_INET, (u_char *) &saddr.s_addr, sizeof(saddr.s_addr)); - else if (!nbmaifp && nifp->linkidx != IFINDEX_INTERNAL) - nbmaifp = if_lookup_by_index(nifp->linkidx, VRF_DEFAULT); - } - break; + struct in_addr saddr = {0}; + netlink_gre_get_info(ifp->ifindex, &nifp->grekey, + &nifp->linkidx, &saddr); + debugf(NHRP_DEBUG_IF, "%s: GRE: %x %x %x", ifp->name, + nifp->grekey, nifp->linkidx, saddr.s_addr); + if (saddr.s_addr) + sockunion_set(&nbma, AF_INET, (u_char *)&saddr.s_addr, + sizeof(saddr.s_addr)); + else if (!nbmaifp && nifp->linkidx != IFINDEX_INTERNAL) + nbmaifp = + if_lookup_by_index(nifp->linkidx, VRF_DEFAULT); + } break; default: break; } @@ -142,8 +152,11 @@ static void nhrp_interface_update_nbma(struct interface *ifp) notifier_del(&nifp->nbmanifp_notifier); nifp->nbmaifp = nbmaifp; if (nbmaifp) { - notifier_add(&nifp->nbmanifp_notifier, &nbmanifp->notifier_list, nhrp_interface_interface_notifier); - debugf(NHRP_DEBUG_IF, "%s: bound to %s", ifp->name, nbmaifp->name); + notifier_add(&nifp->nbmanifp_notifier, + &nbmanifp->notifier_list, + nhrp_interface_interface_notifier); + debugf(NHRP_DEBUG_IF, "%s: bound to %s", ifp->name, + nbmaifp->name); } } @@ -158,13 +171,15 @@ static void nhrp_interface_update_nbma(struct interface *ifp) nifp->nbma = nbma; nhrp_interface_update(nifp->ifp); debugf(NHRP_DEBUG_IF, "%s: NBMA address changed", ifp->name); - notifier_call(&nifp->notifier_list, NOTIFY_INTERFACE_NBMA_CHANGED); + notifier_call(&nifp->notifier_list, + NOTIFY_INTERFACE_NBMA_CHANGED); } nhrp_interface_update(ifp); } -static void nhrp_interface_update_address(struct interface *ifp, afi_t afi, int force) +static void nhrp_interface_update_address(struct interface *ifp, afi_t afi, + int force) { const int family = afi2family(afi); struct nhrp_interface *nifp = ifp->info; @@ -184,11 +199,13 @@ static void nhrp_interface_update_address(struct interface *ifp, afi_t afi, int best = c; continue; } - if ((best->flags & ZEBRA_IFA_SECONDARY) && !(c->flags & ZEBRA_IFA_SECONDARY)) { + if ((best->flags & ZEBRA_IFA_SECONDARY) + && !(c->flags & ZEBRA_IFA_SECONDARY)) { best = c; continue; } - if (!(best->flags & ZEBRA_IFA_SECONDARY) && (c->flags & ZEBRA_IFA_SECONDARY)) + if (!(best->flags & ZEBRA_IFA_SECONDARY) + && (c->flags & ZEBRA_IFA_SECONDARY)) continue; if (best->address->prefixlen > c->address->prefixlen) { best = c; @@ -199,9 +216,10 @@ static void nhrp_interface_update_address(struct interface *ifp, afi_t afi, int } /* On NHRP interfaces a host prefix is required */ - if (best && if_ad->configured && best->address->prefixlen != 8 * prefix_blen(best->address)) { + if (best && if_ad->configured + && best->address->prefixlen != 8 * prefix_blen(best->address)) { zlog_notice("%s: %s is not a host prefix", ifp->name, - prefix2str(best->address, buf, sizeof buf)); + prefix2str(best->address, buf, sizeof buf)); best = NULL; } @@ -216,17 +234,21 @@ static void nhrp_interface_update_address(struct interface *ifp, afi_t afi, int if (sockunion_family(&if_ad->addr) != AF_UNSPEC) { nc = nhrp_cache_get(ifp, &if_ad->addr, 0); - if (nc) nhrp_cache_update_binding(nc, NHRP_CACHE_LOCAL, -1, NULL, 0, NULL); + if (nc) + nhrp_cache_update_binding(nc, NHRP_CACHE_LOCAL, -1, + NULL, 0, NULL); } - debugf(NHRP_DEBUG_KERNEL, "%s: IPv%d address changed to %s", - ifp->name, afi == AFI_IP ? 4 : 6, - best ? prefix2str(best->address, buf, sizeof buf) : "(none)"); + debugf(NHRP_DEBUG_KERNEL, "%s: IPv%d address changed to %s", ifp->name, + afi == AFI_IP ? 4 : 6, + best ? prefix2str(best->address, buf, sizeof buf) : "(none)"); if_ad->addr = addr; if (if_ad->configured && sockunion_family(&if_ad->addr) != AF_UNSPEC) { nc = nhrp_cache_get(ifp, &addr, 1); - if (nc) nhrp_cache_update_binding(nc, NHRP_CACHE_LOCAL, 0, NULL, 0, NULL); + if (nc) + nhrp_cache_update_binding(nc, NHRP_CACHE_LOCAL, 0, NULL, + 0, NULL); } notifier_call(&nifp->notifier_list, NOTIFY_INTERFACE_ADDRESS_CHANGED); @@ -244,9 +266,9 @@ void nhrp_interface_update(struct interface *ifp) for (afi = 0; afi < AFI_MAX; afi++) { if_ad = &nifp->afi[afi]; - if (sockunion_family(&nifp->nbma) == AF_UNSPEC || - ifp->ifindex == IFINDEX_INTERNAL || !if_is_up(ifp) || - !if_ad->network_id) { + if (sockunion_family(&nifp->nbma) == AF_UNSPEC + || ifp->ifindex == IFINDEX_INTERNAL || !if_is_up(ifp) + || !if_ad->network_id) { if (if_ad->configured) { if_ad->configured = 0; nhrp_interface_update_address(ifp, afi, 1); @@ -255,7 +277,8 @@ void nhrp_interface_update(struct interface *ifp) } if (!if_ad->configured) { - os_configure_dmvpn(ifp->ifindex, ifp->name, afi2family(afi)); + os_configure_dmvpn(ifp->ifindex, ifp->name, + afi2family(afi)); if_ad->configured = 1; nhrp_interface_update_address(ifp, afi, 1); } @@ -265,11 +288,14 @@ void nhrp_interface_update(struct interface *ifp) if (enabled != nifp->enabled) { nifp->enabled = enabled; - notifier_call(&nifp->notifier_list, enabled ? NOTIFY_INTERFACE_UP : NOTIFY_INTERFACE_DOWN); + notifier_call(&nifp->notifier_list, + enabled ? NOTIFY_INTERFACE_UP + : NOTIFY_INTERFACE_DOWN); } } -int nhrp_interface_add(int cmd, struct zclient *client, zebra_size_t length, vrf_id_t vrf_id) +int nhrp_interface_add(int cmd, struct zclient *client, zebra_size_t length, + vrf_id_t vrf_id) { struct interface *ifp; @@ -279,16 +305,16 @@ int nhrp_interface_add(int cmd, struct zclient *client, zebra_size_t length, vrf return 0; debugf(NHRP_DEBUG_IF, "if-add: %s, ifindex: %u, hw_type: %d %s", - ifp->name, ifp->ifindex, - ifp->ll_type, if_link_type_str(ifp->ll_type)); + ifp->name, ifp->ifindex, ifp->ll_type, + if_link_type_str(ifp->ll_type)); nhrp_interface_update_nbma(ifp); return 0; } -int nhrp_interface_delete(int cmd, struct zclient *client, - zebra_size_t length, vrf_id_t vrf_id) +int nhrp_interface_delete(int cmd, struct zclient *client, zebra_size_t length, + vrf_id_t vrf_id) { struct interface *ifp; struct stream *s; @@ -305,8 +331,8 @@ int nhrp_interface_delete(int cmd, struct zclient *client, return 0; } -int nhrp_interface_up(int cmd, struct zclient *client, - zebra_size_t length, vrf_id_t vrf_id) +int nhrp_interface_up(int cmd, struct zclient *client, zebra_size_t length, + vrf_id_t vrf_id) { struct interface *ifp; @@ -320,8 +346,8 @@ int nhrp_interface_up(int cmd, struct zclient *client, return 0; } -int nhrp_interface_down(int cmd, struct zclient *client, - zebra_size_t length, vrf_id_t vrf_id) +int nhrp_interface_down(int cmd, struct zclient *client, zebra_size_t length, + vrf_id_t vrf_id) { struct interface *ifp; @@ -344,11 +370,11 @@ int nhrp_interface_address_add(int cmd, struct zclient *client, if (ifc == NULL) return 0; - debugf(NHRP_DEBUG_IF, "if-addr-add: %s: %s", - ifc->ifp->name, - prefix2str(ifc->address, buf, sizeof buf)); + debugf(NHRP_DEBUG_IF, "if-addr-add: %s: %s", ifc->ifp->name, + prefix2str(ifc->address, buf, sizeof buf)); - nhrp_interface_update_address(ifc->ifp, family2afi(PREFIX_FAMILY(ifc->address)), 0); + nhrp_interface_update_address( + ifc->ifp, family2afi(PREFIX_FAMILY(ifc->address)), 0); return 0; } @@ -363,17 +389,18 @@ int nhrp_interface_address_delete(int cmd, struct zclient *client, if (ifc == NULL) return 0; - debugf(NHRP_DEBUG_IF, "if-addr-del: %s: %s", - ifc->ifp->name, - prefix2str(ifc->address, buf, sizeof buf)); + debugf(NHRP_DEBUG_IF, "if-addr-del: %s: %s", ifc->ifp->name, + prefix2str(ifc->address, buf, sizeof buf)); - nhrp_interface_update_address(ifc->ifp, family2afi(PREFIX_FAMILY(ifc->address)), 0); + nhrp_interface_update_address( + ifc->ifp, family2afi(PREFIX_FAMILY(ifc->address)), 0); connected_free(ifc); return 0; } -void nhrp_interface_notify_add(struct interface *ifp, struct notifier_block *n, notifier_fn_t fn) +void nhrp_interface_notify_add(struct interface *ifp, struct notifier_block *n, + notifier_fn_t fn) { struct nhrp_interface *nifp = ifp->info; notifier_add(n, &nifp->notifier_list, fn); @@ -384,15 +411,19 @@ void nhrp_interface_notify_del(struct interface *ifp, struct notifier_block *n) notifier_del(n); } -void nhrp_interface_set_protection(struct interface *ifp, const char *profile, const char *fallback_profile) +void nhrp_interface_set_protection(struct interface *ifp, const char *profile, + const char *fallback_profile) { struct nhrp_interface *nifp = ifp->info; - if (nifp->ipsec_profile) free(nifp->ipsec_profile); + if (nifp->ipsec_profile) + free(nifp->ipsec_profile); nifp->ipsec_profile = profile ? strdup(profile) : NULL; - if (nifp->ipsec_fallback_profile) free(nifp->ipsec_fallback_profile); - nifp->ipsec_fallback_profile = fallback_profile ? strdup(fallback_profile) : NULL; + if (nifp->ipsec_fallback_profile) + free(nifp->ipsec_fallback_profile); + nifp->ipsec_fallback_profile = + fallback_profile ? strdup(fallback_profile) : NULL; notifier_call(&nifp->notifier_list, NOTIFY_INTERFACE_ADDRESS_CHANGED); } @@ -401,7 +432,8 @@ void nhrp_interface_set_source(struct interface *ifp, const char *ifname) { struct nhrp_interface *nifp = ifp->info; - if (nifp->source) free(nifp->source); + if (nifp->source) + free(nifp->source); nifp->source = ifname ? strdup(ifname) : NULL; nhrp_interface_update_nbma(ifp); diff --git a/nhrpd/nhrp_main.c b/nhrpd/nhrp_main.c index 767907aa5..ba1e00ded 100644 --- a/nhrpd/nhrp_main.c +++ b/nhrpd/nhrp_main.c @@ -32,15 +32,13 @@ struct thread_master *master; struct timeval current_time; /* nhrpd options. */ -struct option longopts[] = { - { 0 } -}; +struct option longopts[] = {{0}}; /* nhrpd privileges */ -static zebra_capabilities_t _caps_p [] = { - ZCAP_NET_RAW, - ZCAP_NET_ADMIN, - ZCAP_DAC_OVERRIDE, /* for now needed to write to /proc/sys/net/ipv4/<if>/send_redirect */ +static zebra_capabilities_t _caps_p[] = { + ZCAP_NET_RAW, ZCAP_NET_ADMIN, + ZCAP_DAC_OVERRIDE, /* for now needed to write to + /proc/sys/net/ipv4/<if>/send_redirect */ }; struct zebra_privs_t nhrpd_privs = { @@ -61,7 +59,8 @@ static void parse_arguments(int argc, char **argv) while (1) { opt = frr_getopt(argc, argv, 0); - if(opt < 0) break; + if (opt < 0) + break; switch (opt) { case 0: @@ -98,21 +97,27 @@ static void nhrp_request_stop(void) } static struct quagga_signal_t sighandlers[] = { - { .signal = SIGUSR1, .handler = &nhrp_sigusr1, }, - { .signal = SIGINT, .handler = &nhrp_request_stop, }, - { .signal = SIGTERM, .handler = &nhrp_request_stop, }, + { + .signal = SIGUSR1, + .handler = &nhrp_sigusr1, + }, + { + .signal = SIGINT, + .handler = &nhrp_request_stop, + }, + { + .signal = SIGTERM, + .handler = &nhrp_request_stop, + }, }; -FRR_DAEMON_INFO(nhrpd, NHRP, - .vty_port = NHRP_VTY_PORT, +FRR_DAEMON_INFO(nhrpd, NHRP, .vty_port = NHRP_VTY_PORT, - .proghelp = "Implementation of the NHRP routing protocol.", + .proghelp = "Implementation of the NHRP routing protocol.", - .signals = sighandlers, - .n_signals = array_size(sighandlers), + .signals = sighandlers, .n_signals = array_size(sighandlers), - .privs = &nhrpd_privs, -) + .privs = &nhrpd_privs, ) int main(int argc, char **argv) { diff --git a/nhrpd/nhrp_nhs.c b/nhrpd/nhrp_nhs.c index 6fbd6ca22..a7a8c2019 100644 --- a/nhrpd/nhrp_nhs.c +++ b/nhrpd/nhrp_nhs.c @@ -23,7 +23,8 @@ static int nhrp_reg_send_req(struct thread *t); static void nhrp_reg_reply(struct nhrp_reqid *reqid, void *arg) { struct nhrp_packet_parser *p = arg; - struct nhrp_registration *r = container_of(reqid, struct nhrp_registration, reqid); + struct nhrp_registration *r = + container_of(reqid, struct nhrp_registration, reqid); struct nhrp_nhs *nhs = r->nhs; struct interface *ifp = nhs->ifp; struct nhrp_interface *nifp = ifp->info; @@ -45,13 +46,16 @@ static void nhrp_reg_reply(struct nhrp_reqid *reqid, void *arg) debugf(NHRP_DEBUG_COMMON, "NHS: Reg.reply received"); ok = 1; - while ((cie = nhrp_cie_pull(&p->payload, p->hdr, &cie_nbma, &cie_proto)) != NULL) { - proto = sockunion_family(&cie_proto) != AF_UNSPEC ? &cie_proto : &p->src_proto; + while ((cie = nhrp_cie_pull(&p->payload, p->hdr, &cie_nbma, &cie_proto)) + != NULL) { + proto = sockunion_family(&cie_proto) != AF_UNSPEC + ? &cie_proto + : &p->src_proto; debugf(NHRP_DEBUG_COMMON, "NHS: CIE registration: %s: %d", - sockunion2str(proto, buf, sizeof(buf)), - cie->code); - if (!((cie->code == NHRP_CODE_SUCCESS) || - (cie->code == NHRP_CODE_ADMINISTRATIVELY_PROHIBITED && nhs->hub))) + sockunion2str(proto, buf, sizeof(buf)), cie->code); + if (!((cie->code == NHRP_CODE_SUCCESS) + || (cie->code == NHRP_CODE_ADMINISTRATIVELY_PROHIBITED + && nhs->hub))) ok = 0; } @@ -64,11 +68,15 @@ static void nhrp_reg_reply(struct nhrp_reqid *reqid, void *arg) switch (htons(ext->type) & ~NHRP_EXTENSION_FLAG_COMPULSORY) { case NHRP_EXTENSION_NAT_ADDRESS: /* NHS adds second CIE if NAT is detected */ - if (nhrp_cie_pull(&extpl, p->hdr, &cie_nbma, &cie_proto) && - nhrp_cie_pull(&extpl, p->hdr, &cie_nbma, &cie_proto)) { + if (nhrp_cie_pull(&extpl, p->hdr, &cie_nbma, &cie_proto) + && nhrp_cie_pull(&extpl, p->hdr, &cie_nbma, + &cie_proto)) { nifp->nat_nbma = cie_nbma; - debugf(NHRP_DEBUG_IF, "%s: NAT detected, real NBMA address: %s", - ifp->name, sockunion2str(&nifp->nbma, buf, sizeof(buf))); + debugf(NHRP_DEBUG_IF, + "%s: NAT detected, real NBMA address: %s", + ifp->name, + sockunion2str(&nifp->nbma, buf, + sizeof(buf))); } break; } @@ -86,7 +94,9 @@ static void nhrp_reg_reply(struct nhrp_reqid *reqid, void *arg) r->proto_addr = p->dst_proto; c = nhrp_cache_get(ifp, &p->dst_proto, 1); - if (c) nhrp_cache_update_binding(c, NHRP_CACHE_NHS, holdtime, nhrp_peer_ref(r->peer), 0, NULL); + if (c) + nhrp_cache_update_binding(c, NHRP_CACHE_NHS, holdtime, + nhrp_peer_ref(r->peer), 0, NULL); } static int nhrp_reg_timeout(struct thread *t) @@ -99,21 +109,24 @@ static int nhrp_reg_timeout(struct thread *t) if (r->timeout >= 16 && sockunion_family(&r->proto_addr) != AF_UNSPEC) { nhrp_reqid_free(&nhrp_packet_reqid, &r->reqid); c = nhrp_cache_get(r->nhs->ifp, &r->proto_addr, 0); - if (c) nhrp_cache_update_binding(c, NHRP_CACHE_NHS, -1, NULL, 0, NULL); + if (c) + nhrp_cache_update_binding(c, NHRP_CACHE_NHS, -1, NULL, + 0, NULL); sockunion_family(&r->proto_addr) = AF_UNSPEC; } r->timeout <<= 1; - if (r->timeout > 64) r->timeout = 2; - thread_add_timer_msec(master, nhrp_reg_send_req, r, 10, - &r->t_register); + if (r->timeout > 64) + r->timeout = 2; + thread_add_timer_msec(master, nhrp_reg_send_req, r, 10, &r->t_register); return 0; } static void nhrp_reg_peer_notify(struct notifier_block *n, unsigned long cmd) { - struct nhrp_registration *r = container_of(n, struct nhrp_registration, peer_notifier); + struct nhrp_registration *r = + container_of(n, struct nhrp_registration, peer_notifier); char buf[SU_ADDRSTRLEN]; switch (cmd) { @@ -122,7 +135,8 @@ static void nhrp_reg_peer_notify(struct notifier_block *n, unsigned long cmd) case NOTIFY_PEER_IFCONFIG_CHANGED: case NOTIFY_PEER_MTU_CHANGED: debugf(NHRP_DEBUG_COMMON, "NHS: Flush timer for %s", - sockunion2str(&r->peer->vc->remote.nbma, buf, sizeof buf)); + sockunion2str(&r->peer->vc->remote.nbma, buf, + sizeof buf)); THREAD_TIMER_OFF(r->t_register); thread_add_timer_msec(master, nhrp_reg_send_req, r, 10, &r->t_register); @@ -147,7 +161,8 @@ static int nhrp_reg_send_req(struct thread *t) r->t_register = NULL; if (!nhrp_peer_check(r->peer, 2)) { debugf(NHRP_DEBUG_COMMON, "NHS: Waiting link for %s", - sockunion2str(&r->peer->vc->remote.nbma, buf1, sizeof buf1)); + sockunion2str(&r->peer->vc->remote.nbma, buf1, + sizeof buf1)); thread_add_timer(master, nhrp_reg_send_req, r, 120, &r->t_register); return 0; @@ -163,19 +178,22 @@ static int nhrp_reg_send_req(struct thread *t) sockunion2str(&if_ad->addr, buf1, sizeof(buf1)); sockunion2str(dst_proto, buf2, sizeof(buf2)); - debugf(NHRP_DEBUG_COMMON, "NHS: Register %s -> %s (timeout %d)", buf1, buf2, r->timeout); + debugf(NHRP_DEBUG_COMMON, "NHS: Register %s -> %s (timeout %d)", buf1, + buf2, r->timeout); /* No protocol address configured for tunnel interface */ if (sockunion_family(&if_ad->addr) == AF_UNSPEC) return 0; zb = zbuf_alloc(1400); - hdr = nhrp_packet_push(zb, NHRP_PACKET_REGISTRATION_REQUEST, &nifp->nbma, &if_ad->addr, dst_proto); + hdr = nhrp_packet_push(zb, NHRP_PACKET_REGISTRATION_REQUEST, + &nifp->nbma, &if_ad->addr, dst_proto); hdr->hop_count = 1; if (!(if_ad->flags & NHRP_IFF_REG_NO_UNIQUE)) hdr->flags |= htons(NHRP_FLAG_REGISTRATION_UNIQUE); - hdr->u.request_id = htonl(nhrp_reqid_alloc(&nhrp_packet_reqid, &r->reqid, nhrp_reg_reply)); + hdr->u.request_id = htonl(nhrp_reqid_alloc(&nhrp_packet_reqid, + &r->reqid, nhrp_reg_reply)); /* FIXME: push CIE for each local protocol address */ cie = nhrp_cie_push(zb, NHRP_CODE_SUCCESS, NULL, NULL); @@ -208,17 +226,20 @@ static void nhrp_reg_delete(struct nhrp_registration *r) XFREE(MTYPE_NHRP_REGISTRATION, r); } -static struct nhrp_registration *nhrp_reg_by_nbma(struct nhrp_nhs *nhs, const union sockunion *nbma_addr) +static struct nhrp_registration * +nhrp_reg_by_nbma(struct nhrp_nhs *nhs, const union sockunion *nbma_addr) { struct nhrp_registration *r; - list_for_each_entry(r, &nhs->reglist_head, reglist_entry) - if (sockunion_same(&r->peer->vc->remote.nbma, nbma_addr)) - return r; + list_for_each_entry( + r, &nhs->reglist_head, + reglist_entry) if (sockunion_same(&r->peer->vc->remote.nbma, + nbma_addr)) return r; return NULL; } -static void nhrp_nhs_resolve_cb(struct resolver_query *q, int n, union sockunion *addrs) +static void nhrp_nhs_resolve_cb(struct resolver_query *q, int n, + union sockunion *addrs) { struct nhrp_nhs *nhs = container_of(q, struct nhrp_nhs, dns_resolve); struct nhrp_interface *nifp = nhs->ifp->info; @@ -236,8 +257,8 @@ static void nhrp_nhs_resolve_cb(struct resolver_query *q, int n, union sockunion thread_add_timer(master, nhrp_nhs_resolve, nhs, 2 * 60 * 60, &nhs->t_resolve); - list_for_each_entry(reg, &nhs->reglist_head, reglist_entry) - reg->mark = 1; + list_for_each_entry(reg, &nhs->reglist_head, reglist_entry) reg->mark = + 1; nhs->hub = 0; for (i = 0; i < n; i++) { @@ -258,12 +279,14 @@ static void nhrp_nhs_resolve_cb(struct resolver_query *q, int n, union sockunion reg->timeout = 1; list_init(®->reglist_entry); list_add_tail(®->reglist_entry, &nhs->reglist_head); - nhrp_peer_notify_add(reg->peer, ®->peer_notifier, nhrp_reg_peer_notify); + nhrp_peer_notify_add(reg->peer, ®->peer_notifier, + nhrp_reg_peer_notify); thread_add_timer_msec(master, nhrp_reg_send_req, reg, 50, ®->t_register); } - list_for_each_entry_safe(reg, regn, &nhs->reglist_head, reglist_entry) { + list_for_each_entry_safe(reg, regn, &nhs->reglist_head, reglist_entry) + { if (reg->mark) nhrp_reg_delete(reg); } @@ -273,24 +296,27 @@ static int nhrp_nhs_resolve(struct thread *t) { struct nhrp_nhs *nhs = THREAD_ARG(t); - resolver_resolve(&nhs->dns_resolve, AF_INET, nhs->nbma_fqdn, nhrp_nhs_resolve_cb); + resolver_resolve(&nhs->dns_resolve, AF_INET, nhs->nbma_fqdn, + nhrp_nhs_resolve_cb); return 0; } -int nhrp_nhs_add(struct interface *ifp, afi_t afi, union sockunion *proto_addr, const char *nbma_fqdn) +int nhrp_nhs_add(struct interface *ifp, afi_t afi, union sockunion *proto_addr, + const char *nbma_fqdn) { struct nhrp_interface *nifp = ifp->info; struct nhrp_nhs *nhs; - if (sockunion_family(proto_addr) != AF_UNSPEC && - sockunion_family(proto_addr) != afi2family(afi)) + if (sockunion_family(proto_addr) != AF_UNSPEC + && sockunion_family(proto_addr) != afi2family(afi)) return NHRP_ERR_PROTOCOL_ADDRESS_MISMATCH; - list_for_each_entry(nhs, &nifp->afi[afi].nhslist_head, nhslist_entry) { - if (sockunion_family(&nhs->proto_addr) != AF_UNSPEC && - sockunion_family(proto_addr) != AF_UNSPEC && - sockunion_same(&nhs->proto_addr, proto_addr)) + list_for_each_entry(nhs, &nifp->afi[afi].nhslist_head, nhslist_entry) + { + if (sockunion_family(&nhs->proto_addr) != AF_UNSPEC + && sockunion_family(proto_addr) != AF_UNSPEC + && sockunion_same(&nhs->proto_addr, proto_addr)) return NHRP_ERR_ENTRY_EXISTS; if (strcmp(nhs->nbma_fqdn, nbma_fqdn) == 0) @@ -298,9 +324,10 @@ int nhrp_nhs_add(struct interface *ifp, afi_t afi, union sockunion *proto_addr, } nhs = XMALLOC(MTYPE_NHRP_NHS, sizeof(struct nhrp_nhs)); - if (!nhs) return NHRP_ERR_NO_MEMORY; + if (!nhs) + return NHRP_ERR_NO_MEMORY; - *nhs = (struct nhrp_nhs) { + *nhs = (struct nhrp_nhs){ .afi = afi, .ifp = ifp, .proto_addr = *proto_addr, @@ -314,17 +341,20 @@ int nhrp_nhs_add(struct interface *ifp, afi_t afi, union sockunion *proto_addr, return NHRP_OK; } -int nhrp_nhs_del(struct interface *ifp, afi_t afi, union sockunion *proto_addr, const char *nbma_fqdn) +int nhrp_nhs_del(struct interface *ifp, afi_t afi, union sockunion *proto_addr, + const char *nbma_fqdn) { struct nhrp_interface *nifp = ifp->info; struct nhrp_nhs *nhs, *nnhs; int ret = NHRP_ERR_ENTRY_NOT_FOUND; - if (sockunion_family(proto_addr) != AF_UNSPEC && - sockunion_family(proto_addr) != afi2family(afi)) + if (sockunion_family(proto_addr) != AF_UNSPEC + && sockunion_family(proto_addr) != afi2family(afi)) return NHRP_ERR_PROTOCOL_ADDRESS_MISMATCH; - list_for_each_entry_safe(nhs, nnhs, &nifp->afi[afi].nhslist_head, nhslist_entry) { + list_for_each_entry_safe(nhs, nnhs, &nifp->afi[afi].nhslist_head, + nhslist_entry) + { if (!sockunion_same(&nhs->proto_addr, proto_addr)) continue; if (strcmp(nhs->nbma_fqdn, nbma_fqdn) != 0) @@ -345,7 +375,7 @@ int nhrp_nhs_free(struct nhrp_nhs *nhs) nhrp_reg_delete(r); THREAD_OFF(nhs->t_resolve); list_del(&nhs->nhslist_entry); - free((void*) nhs->nbma_fqdn); + free((void *)nhs->nbma_fqdn); XFREE(MTYPE_NHRP_NHS, nhs); return 0; } @@ -361,22 +391,27 @@ void nhrp_nhs_terminate(void) FOR_ALL_INTERFACES (vrf, ifp) { nifp = ifp->info; for (afi = 0; afi < AFI_MAX; afi++) { - list_for_each_entry_safe(nhs, tmp, &nifp->afi[afi].nhslist_head, nhslist_entry) - nhrp_nhs_free(nhs); + list_for_each_entry_safe( + nhs, tmp, &nifp->afi[afi].nhslist_head, + nhslist_entry) nhrp_nhs_free(nhs); } } } -void nhrp_nhs_foreach(struct interface *ifp, afi_t afi, void (*cb)(struct nhrp_nhs *, struct nhrp_registration *, void *), void *ctx) +void nhrp_nhs_foreach(struct interface *ifp, afi_t afi, + void (*cb)(struct nhrp_nhs *, struct nhrp_registration *, + void *), + void *ctx) { struct nhrp_interface *nifp = ifp->info; struct nhrp_nhs *nhs; struct nhrp_registration *reg; - list_for_each_entry(nhs, &nifp->afi[afi].nhslist_head, nhslist_entry) { + list_for_each_entry(nhs, &nifp->afi[afi].nhslist_head, nhslist_entry) + { if (!list_empty(&nhs->reglist_head)) { - list_for_each_entry(reg, &nhs->reglist_head, reglist_entry) - cb(nhs, reg, ctx); + list_for_each_entry(reg, &nhs->reglist_head, + reglist_entry) cb(nhs, reg, ctx); } else cb(nhs, 0, ctx); } diff --git a/nhrpd/nhrp_packet.c b/nhrpd/nhrp_packet.c index 36281d517..c27ebe1d9 100644 --- a/nhrpd/nhrp_packet.c +++ b/nhrpd/nhrp_packet.c @@ -21,8 +21,10 @@ struct nhrp_reqid_pool nhrp_packet_reqid; 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; } @@ -30,26 +32,29 @@ static uint16_t family2proto(int family) 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, @@ -59,42 +64,43 @@ struct nhrp_packet_header *nhrp_packet_push( }; 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; @@ -114,24 +120,24 @@ void nhrp_packet_complete(struct zbuf *zb, struct nhrp_packet_header *hdr) 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) { @@ -140,37 +146,38 @@ struct nhrp_cie_header *nhrp_cie_push( } 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) + 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); + 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; } @@ -178,49 +185,64 @@ struct nhrp_cie_header *nhrp_cie_pull( 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)]; @@ -233,18 +255,21 @@ int nhrp_ext_reply(struct zbuf *zb, struct nhrp_packet_header *hdr, struct inter 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 @@ -273,7 +298,8 @@ static int nhrp_packet_recvraw(struct thread *t) 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); @@ -292,10 +318,12 @@ static int nhrp_packet_recvraw(struct thread *t) } 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); diff --git a/nhrpd/nhrp_peer.c b/nhrpd/nhrp_peer.c index 2bcddc080..8952a282e 100644 --- a/nhrpd/nhrp_peer.c +++ b/nhrpd/nhrp_peer.c @@ -85,10 +85,12 @@ static void __nhrp_peer_check(struct nhrp_peer *p) nhrp_peer_ref(p); p->online = online; if (online) { - notifier_call(&p->notifier_list, NOTIFY_PEER_UP); + notifier_call(&p->notifier_list, + NOTIFY_PEER_UP); } else { p->requested = p->fallback_requested = 0; - notifier_call(&p->notifier_list, NOTIFY_PEER_DOWN); + notifier_call(&p->notifier_list, + NOTIFY_PEER_DOWN); } nhrp_peer_unref(p); } @@ -130,7 +132,8 @@ static void nhrp_peer_ifp_notify(struct notifier_block *n, unsigned long cmd) if (vc && p->vc != vc) { nhrp_vc_notify_del(p->vc, &p->vc_notifier); p->vc = vc; - nhrp_vc_notify_add(p->vc, &p->vc_notifier, nhrp_peer_vc_notify); + nhrp_vc_notify_add(p->vc, &p->vc_notifier, + nhrp_peer_vc_notify); __nhrp_peer_check(p); } /* fallthru */ /* to post config update */ @@ -163,47 +166,53 @@ static void *nhrp_peer_create(void *data) p = XMALLOC(MTYPE_NHRP_PEER, sizeof(*p)); if (p) { - *p = (struct nhrp_peer) { + *p = (struct nhrp_peer){ .ref = 0, .ifp = key->ifp, .vc = key->vc, - .notifier_list = NOTIFIER_LIST_INITIALIZER(&p->notifier_list), + .notifier_list = + NOTIFIER_LIST_INITIALIZER(&p->notifier_list), }; nhrp_vc_notify_add(p->vc, &p->vc_notifier, nhrp_peer_vc_notify); - nhrp_interface_notify_add(p->ifp, &p->ifp_notifier, nhrp_peer_ifp_notify); + nhrp_interface_notify_add(p->ifp, &p->ifp_notifier, + nhrp_peer_ifp_notify); } return p; } -struct nhrp_peer *nhrp_peer_get(struct interface *ifp, const union sockunion *remote_nbma) +struct nhrp_peer *nhrp_peer_get(struct interface *ifp, + const union sockunion *remote_nbma) { struct nhrp_interface *nifp = ifp->info; struct nhrp_peer key, *p; struct nhrp_vc *vc; if (!nifp->peer_hash) { - nifp->peer_hash = hash_create(nhrp_peer_key, - nhrp_peer_cmp, + nifp->peer_hash = hash_create(nhrp_peer_key, nhrp_peer_cmp, "NHRP Peer Hash"); - if (!nifp->peer_hash) return NULL; + if (!nifp->peer_hash) + return NULL; } vc = nhrp_vc_get(&nifp->nbma, remote_nbma, 1); - if (!vc) return NULL; + if (!vc) + return NULL; key.ifp = ifp; key.vc = vc; p = hash_get(nifp->peer_hash, &key, nhrp_peer_create); nhrp_peer_ref(p); - if (p->ref == 1) __nhrp_peer_check(p); + if (p->ref == 1) + __nhrp_peer_check(p); return p; } struct nhrp_peer *nhrp_peer_ref(struct nhrp_peer *p) { - if (p) p->ref++; + if (p) + p->ref++; return p; } @@ -227,10 +236,11 @@ static int nhrp_peer_request_timeout(struct thread *t) if (p->online) return 0; - if (nifp->ipsec_fallback_profile && !p->prio && !p->fallback_requested) { + if (nifp->ipsec_fallback_profile && !p->prio + && !p->fallback_requested) { p->fallback_requested = 1; - vici_request_vc(nifp->ipsec_fallback_profile, - &vc->local.nbma, &vc->remote.nbma, p->prio); + vici_request_vc(nifp->ipsec_fallback_profile, &vc->local.nbma, + &vc->remote.nbma, p->prio); thread_add_timer(master, nhrp_peer_request_timeout, p, 30, &p->t_fallback); } else { @@ -259,7 +269,8 @@ int nhrp_peer_check(struct nhrp_peer *p, int establish) p->prio = establish > 1; p->requested = 1; - vici_request_vc(nifp->ipsec_profile, &vc->local.nbma, &vc->remote.nbma, p->prio); + vici_request_vc(nifp->ipsec_profile, &vc->local.nbma, &vc->remote.nbma, + p->prio); thread_add_timer(master, nhrp_peer_request_timeout, p, (nifp->ipsec_fallback_profile && !p->prio) ? 15 : 30, &p->t_fallback); @@ -267,7 +278,8 @@ int nhrp_peer_check(struct nhrp_peer *p, int establish) return 0; } -void nhrp_peer_notify_add(struct nhrp_peer *p, struct notifier_block *n, notifier_fn_t fn) +void nhrp_peer_notify_add(struct nhrp_peer *p, struct notifier_block *n, + notifier_fn_t fn) { notifier_add(n, &p->notifier_list, fn); } @@ -288,13 +300,12 @@ void nhrp_peer_send(struct nhrp_peer *p, struct zbuf *zb) return; debugf(NHRP_DEBUG_KERNEL, "PACKET: Send %s -> %s", - sockunion2str(&p->vc->local.nbma, buf[0], sizeof buf[0]), - sockunion2str(&p->vc->remote.nbma, buf[1], sizeof buf[1])); + sockunion2str(&p->vc->local.nbma, buf[0], sizeof buf[0]), + sockunion2str(&p->vc->remote.nbma, buf[1], sizeof buf[1])); - os_sendmsg(zb->head, zbuf_used(zb), - p->ifp->ifindex, - sockunion_get_addr(&p->vc->remote.nbma), - sockunion_get_addrlen(&p->vc->remote.nbma)); + os_sendmsg(zb->head, zbuf_used(zb), p->ifp->ifindex, + sockunion_get_addr(&p->vc->remote.nbma), + sockunion_get_addrlen(&p->vc->remote.nbma)); zbuf_reset(zb); } @@ -313,16 +324,17 @@ static void nhrp_handle_resolution_req(struct nhrp_packet_parser *p) return; } - if (p->if_ad->network_id && - p->route_type == NHRP_ROUTE_OFF_NBMA && - p->route_prefix.prefixlen < 8) { - debugf(NHRP_DEBUG_COMMON, "Shortcut to more generic than /8 dropped"); + if (p->if_ad->network_id && p->route_type == NHRP_ROUTE_OFF_NBMA + && p->route_prefix.prefixlen < 8) { + debugf(NHRP_DEBUG_COMMON, + "Shortcut to more generic than /8 dropped"); return; } debugf(NHRP_DEBUG_COMMON, "Parsing and replying to Resolution Req"); - if (nhrp_route_address(p->ifp, &p->src_proto, NULL, &peer) != NHRP_ROUTE_NBMA_NEXTHOP) + if (nhrp_route_address(p->ifp, &p->src_proto, NULL, &peer) + != NHRP_ROUTE_NBMA_NEXTHOP) return; #if 0 @@ -337,15 +349,20 @@ static void nhrp_handle_resolution_req(struct nhrp_packet_parser *p) /* Create reply */ zb = zbuf_alloc(1500); - hdr = nhrp_packet_push(zb, NHRP_PACKET_RESOLUTION_REPLY, &p->src_nbma, &p->src_proto, &p->dst_proto); + hdr = nhrp_packet_push(zb, NHRP_PACKET_RESOLUTION_REPLY, &p->src_nbma, + &p->src_proto, &p->dst_proto); /* Copied information from request */ - hdr->flags = p->hdr->flags & htons(NHRP_FLAG_RESOLUTION_SOURCE_IS_ROUTER|NHRP_FLAG_RESOLUTION_SOURCE_STABLE); - hdr->flags |= htons(NHRP_FLAG_RESOLUTION_DESTINATION_STABLE | NHRP_FLAG_RESOLUTION_AUTHORATIVE); + hdr->flags = + p->hdr->flags & htons(NHRP_FLAG_RESOLUTION_SOURCE_IS_ROUTER + | NHRP_FLAG_RESOLUTION_SOURCE_STABLE); + hdr->flags |= htons(NHRP_FLAG_RESOLUTION_DESTINATION_STABLE + | NHRP_FLAG_RESOLUTION_AUTHORATIVE); hdr->u.request_id = p->hdr->u.request_id; /* CIE payload */ - cie = nhrp_cie_push(zb, NHRP_CODE_SUCCESS, &nifp->nbma, &p->if_ad->addr); + cie = nhrp_cie_push(zb, NHRP_CODE_SUCCESS, &nifp->nbma, + &p->if_ad->addr); cie->holding_time = htons(p->if_ad->holdtime); cie->mtu = htons(p->if_ad->mtu); if (p->if_ad->network_id && p->route_type == NHRP_ROUTE_OFF_NBMA) @@ -359,10 +376,14 @@ static void nhrp_handle_resolution_req(struct nhrp_packet_parser *p) case NHRP_EXTENSION_NAT_ADDRESS: if (sockunion_family(&nifp->nat_nbma) == AF_UNSPEC) break; - ext = nhrp_ext_push(zb, hdr, NHRP_EXTENSION_NAT_ADDRESS); - if (!ext) goto err; - cie = nhrp_cie_push(zb, NHRP_CODE_SUCCESS, &nifp->nat_nbma, &p->if_ad->addr); - if (!cie) goto err; + ext = nhrp_ext_push(zb, hdr, + NHRP_EXTENSION_NAT_ADDRESS); + if (!ext) + goto err; + cie = nhrp_cie_push(zb, NHRP_CODE_SUCCESS, + &nifp->nat_nbma, &p->if_ad->addr); + if (!cie) + goto err; nhrp_ext_complete(zb, ext); break; default: @@ -387,7 +408,8 @@ static void nhrp_handle_registration_request(struct nhrp_packet_parser *p) struct nhrp_cie_header *cie; struct nhrp_extension_header *ext; struct nhrp_cache *c; - union sockunion cie_nbma, cie_proto, *proto_addr, *nbma_addr, *nbma_natoa; + union sockunion cie_nbma, cie_proto, *proto_addr, *nbma_addr, + *nbma_natoa; int holdtime, prefix_len, hostprefix_len, natted = 0; size_t paylen; void *pay; @@ -400,26 +422,31 @@ static void nhrp_handle_registration_request(struct nhrp_packet_parser *p) /* Create reply */ zb = zbuf_alloc(1500); - hdr = nhrp_packet_push(zb, NHRP_PACKET_REGISTRATION_REPLY, - &p->src_nbma, &p->src_proto, &p->if_ad->addr); + hdr = nhrp_packet_push(zb, NHRP_PACKET_REGISTRATION_REPLY, &p->src_nbma, + &p->src_proto, &p->if_ad->addr); /* Copied information from request */ - hdr->flags = p->hdr->flags & htons(NHRP_FLAG_REGISTRATION_UNIQUE | NHRP_FLAG_REGISTRATION_NAT); + hdr->flags = p->hdr->flags & htons(NHRP_FLAG_REGISTRATION_UNIQUE + | NHRP_FLAG_REGISTRATION_NAT); hdr->u.request_id = p->hdr->u.request_id; /* Copy payload CIEs */ paylen = zbuf_used(&p->payload); pay = zbuf_pushn(zb, paylen); - if (!pay) goto err; + if (!pay) + goto err; memcpy(pay, zbuf_pulln(&p->payload, paylen), paylen); zbuf_init(&payload, pay, paylen, paylen); - while ((cie = nhrp_cie_pull(&payload, hdr, &cie_nbma, &cie_proto)) != NULL) { + while ((cie = nhrp_cie_pull(&payload, hdr, &cie_nbma, &cie_proto)) + != NULL) { prefix_len = cie->prefix_length; if (prefix_len == 0 || prefix_len >= hostprefix_len) prefix_len = hostprefix_len; - if (prefix_len != hostprefix_len && !(p->hdr->flags & htons(NHRP_FLAG_REGISTRATION_UNIQUE))) { + if (prefix_len != hostprefix_len + && !(p->hdr->flags + & htons(NHRP_FLAG_REGISTRATION_UNIQUE))) { cie->code = NHRP_CODE_BINDING_NON_UNIQUE; continue; } @@ -430,15 +457,20 @@ static void nhrp_handle_registration_request(struct nhrp_packet_parser *p) continue; } - proto_addr = (sockunion_family(&cie_proto) == AF_UNSPEC) ? &p->src_proto : &cie_proto; - nbma_addr = (sockunion_family(&cie_nbma) == AF_UNSPEC) ? &p->src_nbma : &cie_nbma; + proto_addr = (sockunion_family(&cie_proto) == AF_UNSPEC) + ? &p->src_proto + : &cie_proto; + nbma_addr = (sockunion_family(&cie_nbma) == AF_UNSPEC) + ? &p->src_nbma + : &cie_nbma; nbma_natoa = NULL; if (natted) { nbma_natoa = nbma_addr; } holdtime = htons(cie->holding_time); - if (!holdtime) holdtime = p->if_ad->holdtime; + if (!holdtime) + holdtime = p->if_ad->holdtime; c = nhrp_cache_get(ifp, proto_addr, 1); if (!c) { @@ -446,7 +478,9 @@ static void nhrp_handle_registration_request(struct nhrp_packet_parser *p) continue; } - if (!nhrp_cache_update_binding(c, NHRP_CACHE_DYNAMIC, holdtime, nhrp_peer_ref(p->peer), htons(cie->mtu), nbma_natoa)) { + if (!nhrp_cache_update_binding(c, NHRP_CACHE_DYNAMIC, holdtime, + nhrp_peer_ref(p->peer), + htons(cie->mtu), nbma_natoa)) { cie->code = NHRP_CODE_ADMINISTRATIVELY_PROHIBITED; continue; } @@ -458,13 +492,15 @@ static void nhrp_handle_registration_request(struct nhrp_packet_parser *p) while ((ext = nhrp_ext_pull(&p->extensions, &payload)) != NULL) { switch (htons(ext->type) & ~NHRP_EXTENSION_FLAG_COMPULSORY) { case NHRP_EXTENSION_NAT_ADDRESS: - ext = nhrp_ext_push(zb, hdr, NHRP_EXTENSION_NAT_ADDRESS); - if (!ext) goto err; + ext = nhrp_ext_push(zb, hdr, + NHRP_EXTENSION_NAT_ADDRESS); + if (!ext) + goto err; zbuf_copy(zb, &payload, zbuf_used(&payload)); if (natted) { nhrp_cie_push(zb, NHRP_CODE_SUCCESS, - &p->peer->vc->remote.nbma, - &p->src_proto); + &p->peer->vc->remote.nbma, + &p->src_proto); } nhrp_ext_complete(zb, ext); break; @@ -481,32 +517,44 @@ err: zbuf_free(zb); } -static int parse_ether_packet(struct zbuf *zb, uint16_t protocol_type, union sockunion *src, union sockunion *dst) +static int parse_ether_packet(struct zbuf *zb, uint16_t protocol_type, + union sockunion *src, union sockunion *dst) { switch (protocol_type) { case ETH_P_IP: { - struct iphdr *iph = zbuf_pull(zb, struct iphdr); - if (iph) { - if (src) sockunion_set(src, AF_INET, (uint8_t*) &iph->saddr, sizeof(iph->saddr)); - if (dst) sockunion_set(dst, AF_INET, (uint8_t*) &iph->daddr, sizeof(iph->daddr)); - } + struct iphdr *iph = zbuf_pull(zb, struct iphdr); + if (iph) { + if (src) + sockunion_set(src, AF_INET, + (uint8_t *)&iph->saddr, + sizeof(iph->saddr)); + if (dst) + sockunion_set(dst, AF_INET, + (uint8_t *)&iph->daddr, + sizeof(iph->daddr)); } - break; + } break; case ETH_P_IPV6: { - struct ipv6hdr *iph = zbuf_pull(zb, struct ipv6hdr); - if (iph) { - if (src) sockunion_set(src, AF_INET6, (uint8_t*) &iph->saddr, sizeof(iph->saddr)); - if (dst) sockunion_set(dst, AF_INET6, (uint8_t*) &iph->daddr, sizeof(iph->daddr)); - } + struct ipv6hdr *iph = zbuf_pull(zb, struct ipv6hdr); + if (iph) { + if (src) + sockunion_set(src, AF_INET6, + (uint8_t *)&iph->saddr, + sizeof(iph->saddr)); + if (dst) + sockunion_set(dst, AF_INET6, + (uint8_t *)&iph->daddr, + sizeof(iph->daddr)); } - break; + } break; default: return 0; } return 1; } -void nhrp_peer_send_indication(struct interface *ifp, uint16_t protocol_type, struct zbuf *pkt) +void nhrp_peer_send_indication(struct interface *ifp, uint16_t protocol_type, + struct zbuf *pkt) { union sockunion dst; struct zbuf *zb, payload; @@ -516,7 +564,8 @@ void nhrp_peer_send_indication(struct interface *ifp, uint16_t protocol_type, st struct nhrp_peer *p; char buf[2][SU_ADDRSTRLEN]; - if (!nifp->enabled) return; + if (!nifp->enabled) + return; payload = *pkt; if (!parse_ether_packet(&payload, protocol_type, &dst, NULL)) @@ -527,20 +576,23 @@ void nhrp_peer_send_indication(struct interface *ifp, uint16_t protocol_type, st if_ad = &nifp->afi[family2afi(sockunion_family(&dst))]; if (!(if_ad->flags & NHRP_IFF_REDIRECT)) { - debugf(NHRP_DEBUG_COMMON, "Send Traffic Indication to %s about packet to %s ignored", - sockunion2str(&p->vc->remote.nbma, buf[0], sizeof buf[0]), - sockunion2str(&dst, buf[1], sizeof buf[1])); + debugf(NHRP_DEBUG_COMMON, + "Send Traffic Indication to %s about packet to %s ignored", + sockunion2str(&p->vc->remote.nbma, buf[0], + sizeof buf[0]), + sockunion2str(&dst, buf[1], sizeof buf[1])); return; } - debugf(NHRP_DEBUG_COMMON, "Send Traffic Indication to %s (online=%d) about packet to %s", - sockunion2str(&p->vc->remote.nbma, buf[0], sizeof buf[0]), - p->online, - sockunion2str(&dst, buf[1], sizeof buf[1])); + debugf(NHRP_DEBUG_COMMON, + "Send Traffic Indication to %s (online=%d) about packet to %s", + sockunion2str(&p->vc->remote.nbma, buf[0], sizeof buf[0]), + p->online, sockunion2str(&dst, buf[1], sizeof buf[1])); /* Create reply */ zb = zbuf_alloc(1500); - hdr = nhrp_packet_push(zb, NHRP_PACKET_TRAFFIC_INDICATION, &nifp->nbma, &if_ad->addr, &dst); + hdr = nhrp_packet_push(zb, NHRP_PACKET_TRAFFIC_INDICATION, &nifp->nbma, + &if_ad->addr, &dst); hdr->hop_count = 0; /* Payload is the packet causing indication */ @@ -560,11 +612,13 @@ static void nhrp_handle_error_ind(struct nhrp_packet_parser *pp) char buf[2][SU_ADDRSTRLEN]; hdr = nhrp_packet_pull(&origmsg, &src_nbma, &src_proto, &dst_proto); - if (!hdr) return; + if (!hdr) + return; - debugf(NHRP_DEBUG_COMMON, "Error Indication from %s about packet to %s ignored", - sockunion2str(&pp->src_proto, buf[0], sizeof buf[0]), - sockunion2str(&dst_proto, buf[1], sizeof buf[1])); + debugf(NHRP_DEBUG_COMMON, + "Error Indication from %s about packet to %s ignored", + sockunion2str(&pp->src_proto, buf[0], sizeof buf[0]), + sockunion2str(&dst_proto, buf[1], sizeof buf[1])); reqid = nhrp_reqid_lookup(&nhrp_packet_reqid, htonl(hdr->u.request_id)); if (reqid) @@ -576,13 +630,16 @@ static void nhrp_handle_traffic_ind(struct nhrp_packet_parser *p) union sockunion dst; char buf[2][SU_ADDRSTRLEN]; - if (!parse_ether_packet(&p->payload, htons(p->hdr->protocol_type), NULL, &dst)) + if (!parse_ether_packet(&p->payload, htons(p->hdr->protocol_type), NULL, + &dst)) return; - debugf(NHRP_DEBUG_COMMON, "Traffic Indication from %s about packet to %s: %s", - sockunion2str(&p->src_proto, buf[0], sizeof buf[0]), - sockunion2str(&dst, buf[1], sizeof buf[1]), - (p->if_ad->flags & NHRP_IFF_SHORTCUT) ? "trying shortcut" : "ignored"); + debugf(NHRP_DEBUG_COMMON, + "Traffic Indication from %s about packet to %s: %s", + sockunion2str(&p->src_proto, buf[0], sizeof buf[0]), + sockunion2str(&dst, buf[1], sizeof buf[1]), + (p->if_ad->flags & NHRP_IFF_SHORTCUT) ? "trying shortcut" + : "ignored"); if (p->if_ad->flags & NHRP_IFF_SHORTCUT) nhrp_shortcut_initiate(&dst); @@ -599,50 +656,57 @@ static struct { enum packet_type_t type; const char *name; void (*handler)(struct nhrp_packet_parser *); -} packet_types[] = { - [0] = { - .type = PACKET_UNKNOWN, - .name = "UNKNOWN", - }, - [NHRP_PACKET_RESOLUTION_REQUEST] = { - .type = PACKET_REQUEST, - .name = "Resolution-Request", - .handler = nhrp_handle_resolution_req, - }, - [NHRP_PACKET_RESOLUTION_REPLY] = { - .type = PACKET_REPLY, - .name = "Resolution-Reply", - }, - [NHRP_PACKET_REGISTRATION_REQUEST] = { - .type = PACKET_REQUEST, - .name = "Registration-Request", - .handler = nhrp_handle_registration_request, - }, - [NHRP_PACKET_REGISTRATION_REPLY] = { - .type = PACKET_REPLY, - .name = "Registration-Reply", - }, - [NHRP_PACKET_PURGE_REQUEST] = { - .type = PACKET_REQUEST, - .name = "Purge-Request", - }, - [NHRP_PACKET_PURGE_REPLY] = { - .type = PACKET_REPLY, - .name = "Purge-Reply", - }, - [NHRP_PACKET_ERROR_INDICATION] = { - .type = PACKET_INDICATION, - .name = "Error-Indication", - .handler = nhrp_handle_error_ind, - }, - [NHRP_PACKET_TRAFFIC_INDICATION] = { - .type = PACKET_INDICATION, - .name = "Traffic-Indication", - .handler = nhrp_handle_traffic_ind, - } -}; - -static void nhrp_peer_forward(struct nhrp_peer *p, struct nhrp_packet_parser *pp) +} packet_types[] = {[0] = + { + .type = PACKET_UNKNOWN, + .name = "UNKNOWN", + }, + [NHRP_PACKET_RESOLUTION_REQUEST] = + { + .type = PACKET_REQUEST, + .name = "Resolution-Request", + .handler = nhrp_handle_resolution_req, + }, + [NHRP_PACKET_RESOLUTION_REPLY] = + { + .type = PACKET_REPLY, + .name = "Resolution-Reply", + }, + [NHRP_PACKET_REGISTRATION_REQUEST] = + { + .type = PACKET_REQUEST, + .name = "Registration-Request", + .handler = nhrp_handle_registration_request, + }, + [NHRP_PACKET_REGISTRATION_REPLY] = + { + .type = PACKET_REPLY, + .name = "Registration-Reply", + }, + [NHRP_PACKET_PURGE_REQUEST] = + { + .type = PACKET_REQUEST, + .name = "Purge-Request", + }, + [NHRP_PACKET_PURGE_REPLY] = + { + .type = PACKET_REPLY, + .name = "Purge-Reply", + }, + [NHRP_PACKET_ERROR_INDICATION] = + { + .type = PACKET_INDICATION, + .name = "Error-Indication", + .handler = nhrp_handle_error_ind, + }, + [NHRP_PACKET_TRAFFIC_INDICATION] = { + .type = PACKET_INDICATION, + .name = "Traffic-Indication", + .handler = nhrp_handle_traffic_ind, + }}; + +static void nhrp_peer_forward(struct nhrp_peer *p, + struct nhrp_packet_parser *pp) { struct zbuf *zb, extpl; struct nhrp_packet_header *hdr; @@ -658,7 +722,8 @@ static void nhrp_peer_forward(struct nhrp_peer *p, struct nhrp_packet_parser *pp /* Create forward packet - copy header */ zb = zbuf_alloc(1500); - hdr = nhrp_packet_push(zb, pp->hdr->type, &pp->src_nbma, &pp->src_proto, &pp->dst_proto); + hdr = nhrp_packet_push(zb, pp->hdr->type, &pp->src_nbma, &pp->src_proto, + &pp->dst_proto); hdr->flags = pp->hdr->flags; hdr->hop_count = pp->hdr->hop_count - 1; hdr->u.request_id = pp->hdr->u.request_id; @@ -675,22 +740,29 @@ static void nhrp_peer_forward(struct nhrp_peer *p, struct nhrp_packet_parser *pp break; dst = nhrp_ext_push(zb, hdr, htons(ext->type)); - if (!dst) goto err; + if (!dst) + goto err; switch (type) { case NHRP_EXTENSION_FORWARD_TRANSIT_NHS: case NHRP_EXTENSION_REVERSE_TRANSIT_NHS: zbuf_put(zb, extpl.head, len); - if ((type == NHRP_EXTENSION_REVERSE_TRANSIT_NHS) == - (packet_types[hdr->type].type == PACKET_REPLY)) { + if ((type == NHRP_EXTENSION_REVERSE_TRANSIT_NHS) + == (packet_types[hdr->type].type == PACKET_REPLY)) { /* Check NHS list for forwarding loop */ - while ((cie = nhrp_cie_pull(&extpl, pp->hdr, &cie_nbma, &cie_protocol)) != NULL) { - if (sockunion_same(&p->vc->remote.nbma, &cie_nbma)) + while ((cie = nhrp_cie_pull(&extpl, pp->hdr, + &cie_nbma, + &cie_protocol)) + != NULL) { + if (sockunion_same(&p->vc->remote.nbma, + &cie_nbma)) goto err; } /* Append our selves to the list */ - cie = nhrp_cie_push(zb, NHRP_CODE_SUCCESS, &nifp->nbma, &if_ad->addr); - if (!cie) goto err; + cie = nhrp_cie_push(zb, NHRP_CODE_SUCCESS, + &nifp->nbma, &if_ad->addr); + if (!cie) + goto err; cie->holding_time = htons(if_ad->holdtime); } break; @@ -699,7 +771,7 @@ static void nhrp_peer_forward(struct nhrp_peer *p, struct nhrp_packet_parser *pp /* FIXME: RFC says to just copy, but not * append our selves to the transit NHS list */ goto err; - /* fallthru */ + /* fallthru */ case NHRP_EXTENSION_RESPONDER_ADDRESS: /* Supported compulsory extensions, and any * non-compulsory that is not explicitly handled, @@ -730,26 +802,25 @@ static void nhrp_packet_debug(struct zbuf *zb, const char *dir) if (likely(!(debug_flags & NHRP_DEBUG_COMMON))) return; - zbuf_init(&zhdr, zb->buf, zb->tail-zb->buf, zb->tail-zb->buf); + zbuf_init(&zhdr, zb->buf, zb->tail - zb->buf, zb->tail - zb->buf); hdr = nhrp_packet_pull(&zhdr, &src_nbma, &src_proto, &dst_proto); sockunion2str(&src_proto, buf[0], sizeof buf[0]); sockunion2str(&dst_proto, buf[1], sizeof buf[1]); reply = packet_types[hdr->type].type == PACKET_REPLY; - debugf(NHRP_DEBUG_COMMON, "%s %s(%d) %s -> %s", - dir, - packet_types[hdr->type].name ? : "Unknown", - hdr->type, - reply ? buf[1] : buf[0], - reply ? buf[0] : buf[1]); + debugf(NHRP_DEBUG_COMMON, "%s %s(%d) %s -> %s", dir, + packet_types[hdr->type].name ?: "Unknown", hdr->type, + reply ? buf[1] : buf[0], reply ? buf[0] : buf[1]); } static int proto2afi(uint16_t proto) { switch (proto) { - case ETH_P_IP: return AFI_IP; - case ETH_P_IPV6: return AFI_IP6; + case ETH_P_IP: + return AFI_IP; + case ETH_P_IPV6: + return AFI_IP6; } return AF_UNSPEC; } @@ -776,8 +847,8 @@ void nhrp_peer_recv(struct nhrp_peer *p, struct zbuf *zb) afi_t nbma_afi, proto_afi; debugf(NHRP_DEBUG_KERNEL, "PACKET: Recv %s -> %s", - sockunion2str(&vc->remote.nbma, buf[0], sizeof buf[0]), - sockunion2str(&vc->local.nbma, buf[1], sizeof buf[1])); + sockunion2str(&vc->remote.nbma, buf[0], sizeof buf[0]), + sockunion2str(&vc->local.nbma, buf[1], sizeof buf[1])); if (!p->online) { info = "peer not online"; @@ -803,16 +874,16 @@ void nhrp_peer_recv(struct nhrp_peer *p, struct zbuf *zb) nbma_afi = htons(hdr->afnum); proto_afi = proto2afi(htons(hdr->protocol_type)); - if (hdr->type > NHRP_PACKET_MAX || - hdr->version != NHRP_VERSION_RFC2332 || - nbma_afi >= AFI_MAX || proto_afi == AF_UNSPEC || - packet_types[hdr->type].type == PACKET_UNKNOWN || - htons(hdr->packet_size) > realsize) { - zlog_info("From %s: error: packet type %d, version %d, AFI %d, proto %x, size %d (real size %d)", - sockunion2str(&vc->remote.nbma, buf[0], sizeof buf[0]), - (int) hdr->type, (int) hdr->version, - (int) nbma_afi, (int) htons(hdr->protocol_type), - (int) htons(hdr->packet_size), (int) realsize); + if (hdr->type > NHRP_PACKET_MAX || hdr->version != NHRP_VERSION_RFC2332 + || nbma_afi >= AFI_MAX || proto_afi == AF_UNSPEC + || packet_types[hdr->type].type == PACKET_UNKNOWN + || htons(hdr->packet_size) > realsize) { + zlog_info( + "From %s: error: packet type %d, version %d, AFI %d, proto %x, size %d (real size %d)", + sockunion2str(&vc->remote.nbma, buf[0], sizeof buf[0]), + (int)hdr->type, (int)hdr->version, (int)nbma_afi, + (int)htons(hdr->protocol_type), + (int)htons(hdr->packet_size), (int)realsize); goto drop; } pp.if_ad = &((struct nhrp_interface *)ifp->info)->afi[proto_afi]; @@ -842,18 +913,22 @@ void nhrp_peer_recv(struct nhrp_peer *p, struct zbuf *zb) * pre-handled. */ /* Figure out if this is local */ - target_addr = (packet_types[hdr->type].type == PACKET_REPLY) ? &pp.src_proto : &pp.dst_proto; + target_addr = (packet_types[hdr->type].type == PACKET_REPLY) + ? &pp.src_proto + : &pp.dst_proto; if (sockunion_same(&pp.src_proto, &pp.dst_proto)) pp.route_type = NHRP_ROUTE_LOCAL; else - pp.route_type = nhrp_route_address(pp.ifp, target_addr, &pp.route_prefix, &peer); + pp.route_type = nhrp_route_address(pp.ifp, target_addr, + &pp.route_prefix, &peer); switch (pp.route_type) { case NHRP_ROUTE_LOCAL: nhrp_packet_debug(zb, "!LOCAL"); if (packet_types[hdr->type].type == PACKET_REPLY) { - reqid = nhrp_reqid_lookup(&nhrp_packet_reqid, htonl(hdr->u.request_id)); + reqid = nhrp_reqid_lookup(&nhrp_packet_reqid, + htonl(hdr->u.request_id)); if (reqid) { reqid->cb(reqid, &pp); break; @@ -878,10 +953,12 @@ void nhrp_peer_recv(struct nhrp_peer *p, struct zbuf *zb) drop: if (info) { - zlog_info("From %s: error: %s", - sockunion2str(&vc->remote.nbma, buf[0], sizeof buf[0]), - info); + zlog_info( + "From %s: error: %s", + sockunion2str(&vc->remote.nbma, buf[0], sizeof buf[0]), + info); } - if (peer) nhrp_peer_unref(peer); + if (peer) + nhrp_peer_unref(peer); zbuf_free(zb); } diff --git a/nhrpd/nhrp_protocol.h b/nhrpd/nhrp_protocol.h index d5f120ea0..3b94c814d 100644 --- a/nhrpd/nhrp_protocol.h +++ b/nhrpd/nhrp_protocol.h @@ -79,51 +79,51 @@ /* NHRP Packet Structures */ struct nhrp_packet_header { /* Fixed header */ - uint16_t afnum; - uint16_t protocol_type; - uint8_t snap[5]; - uint8_t hop_count; - uint16_t packet_size; - uint16_t checksum; - uint16_t extension_offset; - uint8_t version; - uint8_t type; - uint8_t src_nbma_address_len; - uint8_t src_nbma_subaddress_len; + uint16_t afnum; + uint16_t protocol_type; + uint8_t snap[5]; + uint8_t hop_count; + uint16_t packet_size; + uint16_t checksum; + uint16_t extension_offset; + uint8_t version; + uint8_t type; + uint8_t src_nbma_address_len; + uint8_t src_nbma_subaddress_len; /* Mandatory header */ - uint8_t src_protocol_address_len; - uint8_t dst_protocol_address_len; - uint16_t flags; + uint8_t src_protocol_address_len; + uint8_t dst_protocol_address_len; + uint16_t flags; union { - uint32_t request_id; + uint32_t request_id; struct { - uint16_t code; - uint16_t offset; + uint16_t code; + uint16_t offset; } error; } u; } __attribute__((packed)); struct nhrp_cie_header { - uint8_t code; - uint8_t prefix_length; - uint16_t unused; - uint16_t mtu; - uint16_t holding_time; - uint8_t nbma_address_len; - uint8_t nbma_subaddress_len; - uint8_t protocol_address_len; - uint8_t preference; + uint8_t code; + uint8_t prefix_length; + uint16_t unused; + uint16_t mtu; + uint16_t holding_time; + uint8_t nbma_address_len; + uint8_t nbma_subaddress_len; + uint8_t protocol_address_len; + uint8_t preference; } __attribute__((packed)); struct nhrp_extension_header { - uint16_t type; - uint16_t length; + uint16_t type; + uint16_t length; } __attribute__((packed)); struct nhrp_cisco_authentication_extension { - uint32_t type; - uint8_t secret[8]; + uint32_t type; + uint8_t secret[8]; } __attribute__((packed)); #endif diff --git a/nhrpd/nhrp_route.c b/nhrpd/nhrp_route.c index 8178a8b4b..044529a5c 100644 --- a/nhrpd/nhrp_route.c +++ b/nhrpd/nhrp_route.c @@ -25,7 +25,8 @@ struct route_info { struct interface *nhrp_ifp; }; -static struct route_node *nhrp_route_update_get(const struct prefix *p, int create) +static struct route_node *nhrp_route_update_get(const struct prefix *p, + int create) { struct route_node *rn; afi_t afi = family2afi(PREFIX_FAMILY(p)); @@ -36,7 +37,8 @@ static struct route_node *nhrp_route_update_get(const struct prefix *p, int crea if (create) { rn = route_node_get(zebra_rib[afi], p); if (!rn->info) { - rn->info = XCALLOC(MTYPE_NHRP_ROUTE, sizeof(struct route_info)); + rn->info = XCALLOC(MTYPE_NHRP_ROUTE, + sizeof(struct route_info)); route_lock_node(rn); } return rn; @@ -49,7 +51,8 @@ static void nhrp_route_update_put(struct route_node *rn) { struct route_info *ri = rn->info; - if (!ri->ifp && !ri->nhrp_ifp && sockunion_family(&ri->via) == AF_UNSPEC) { + if (!ri->ifp && !ri->nhrp_ifp + && sockunion_family(&ri->via) == AF_UNSPEC) { XFREE(MTYPE_NHRP_ROUTE, rn->info); rn->info = NULL; route_unlock_node(rn); @@ -57,12 +60,15 @@ static void nhrp_route_update_put(struct route_node *rn) route_unlock_node(rn); } -static void nhrp_route_update_zebra(const struct prefix *p, union sockunion *nexthop, struct interface *ifp) +static void nhrp_route_update_zebra(const struct prefix *p, + union sockunion *nexthop, + struct interface *ifp) { struct route_node *rn; struct route_info *ri; - rn = nhrp_route_update_get(p, (sockunion_family(nexthop) != AF_UNSPEC) || ifp); + rn = nhrp_route_update_get( + p, (sockunion_family(nexthop) != AF_UNSPEC) || ifp); if (rn) { ri = rn->info; ri->via = *nexthop; @@ -84,7 +90,9 @@ void nhrp_route_update_nhrp(const struct prefix *p, struct interface *ifp) } } -void nhrp_route_announce(int add, enum nhrp_cache_type type, const struct prefix *p, struct interface *ifp, const union sockunion *nexthop, uint32_t mtu) +void nhrp_route_announce(int add, enum nhrp_cache_type type, + const struct prefix *p, struct interface *ifp, + const union sockunion *nexthop, uint32_t mtu) { struct zapi_route api; struct zapi_nexthop *api_nh; @@ -158,10 +166,13 @@ void nhrp_route_announce(int add, enum nhrp_cache_type type, const struct prefix char buf[2][PREFIX_STRLEN]; prefix2str(&api.prefix, buf[0], sizeof(buf[0])); - zlog_debug("Zebra send: route %s %s nexthop %s metric %u" + zlog_debug( + "Zebra send: route %s %s nexthop %s metric %u" " count %d dev %s", add ? "add" : "del", buf[0], - nexthop ? inet_ntop(api.prefix.family, &api_nh->gate, buf[1], sizeof(buf[1])) : "<onlink>", + nexthop ? inet_ntop(api.prefix.family, &api_nh->gate, + buf[1], sizeof(buf[1])) + : "<onlink>", api.metric, api.nexthop_num, ifp ? ifp->name : "none"); } @@ -169,7 +180,8 @@ void nhrp_route_announce(int add, enum nhrp_cache_type type, const struct prefix &api); } -int nhrp_route_read(int cmd, struct zclient *zclient, zebra_size_t length, vrf_id_t vrf_id) +int nhrp_route_read(int cmd, struct zclient *zclient, zebra_size_t length, + vrf_id_t vrf_id) { struct zapi_route api; struct zapi_nexthop *api_nh; @@ -200,15 +212,15 @@ int nhrp_route_read(int cmd, struct zclient *zclient, zebra_size_t length, vrf_i } if (api_nh->ifindex != IFINDEX_INTERNAL) - ifp = if_lookup_by_index(api_nh->ifindex, VRF_DEFAULT); + ifp = if_lookup_by_index(api_nh->ifindex, VRF_DEFAULT); } added = (cmd == ZEBRA_REDISTRIBUTE_ROUTE_ADD); debugf(NHRP_DEBUG_ROUTE, "if-route-%s: %s via %s dev %s", - added ? "add" : "del", - prefix2str(&api.prefix, buf[0], sizeof buf[0]), - sockunion2str(&nexthop_addr, buf[1], sizeof buf[1]), - ifp ? ifp->name : "(none)"); + added ? "add" : "del", + prefix2str(&api.prefix, buf[0], sizeof buf[0]), + sockunion2str(&nexthop_addr, buf[1], sizeof buf[1]), + ifp ? ifp->name : "(none)"); nhrp_route_update_zebra(&api.prefix, &nexthop_addr, ifp); nhrp_shortcut_prefix_change(&api.prefix, !added); @@ -216,7 +228,8 @@ int nhrp_route_read(int cmd, struct zclient *zclient, zebra_size_t length, vrf_i return 0; } -int nhrp_route_get_nexthop(const union sockunion *addr, struct prefix *p, union sockunion *via, struct interface **ifp) +int nhrp_route_get_nexthop(const union sockunion *addr, struct prefix *p, + union sockunion *via, struct interface **ifp) { struct route_node *rn; struct route_info *ri; @@ -227,30 +240,38 @@ int nhrp_route_get_nexthop(const union sockunion *addr, struct prefix *p, union sockunion2hostprefix(addr, &lookup); rn = route_node_match(zebra_rib[afi], &lookup); - if (!rn) return 0; + if (!rn) + return 0; ri = rn->info; if (ri->nhrp_ifp) { debugf(NHRP_DEBUG_ROUTE, "lookup %s: nhrp_if=%s", - prefix2str(&lookup, buf, sizeof buf), - ri->nhrp_ifp->name); + prefix2str(&lookup, buf, sizeof buf), + ri->nhrp_ifp->name); - if (via) sockunion_family(via) = AF_UNSPEC; - if (ifp) *ifp = ri->nhrp_ifp; + if (via) + sockunion_family(via) = AF_UNSPEC; + if (ifp) + *ifp = ri->nhrp_ifp; } else { debugf(NHRP_DEBUG_ROUTE, "lookup %s: zebra route dev %s", - prefix2str(&lookup, buf, sizeof buf), - ri->ifp ? ri->ifp->name : "(none)"); + prefix2str(&lookup, buf, sizeof buf), + ri->ifp ? ri->ifp->name : "(none)"); - if (via) *via = ri->via; - if (ifp) *ifp = ri->ifp; + if (via) + *via = ri->via; + if (ifp) + *ifp = ri->ifp; } - if (p) *p = rn->p; + if (p) + *p = rn->p; route_unlock_node(rn); return 1; } -enum nhrp_route_type nhrp_route_address(struct interface *in_ifp, union sockunion *addr, struct prefix *p, struct nhrp_peer **peer) +enum nhrp_route_type nhrp_route_address(struct interface *in_ifp, + union sockunion *addr, struct prefix *p, + struct nhrp_peer **peer) { struct interface *ifp = in_ifp; struct nhrp_interface *nifp; @@ -266,7 +287,8 @@ enum nhrp_route_type nhrp_route_address(struct interface *in_ifp, union sockunio c = nhrp_cache_get(ifp, addr, 0); if (c && c->cur.type == NHRP_CACHE_LOCAL) { - if (p) memset(p, 0, sizeof(*p)); + if (p) + memset(p, 0, sizeof(*p)); return NHRP_ROUTE_LOCAL; } } @@ -277,7 +299,8 @@ enum nhrp_route_type nhrp_route_address(struct interface *in_ifp, union sockunio if (ifp) { /* Departing from nbma network? */ nifp = ifp->info; - if (network_id && network_id != nifp->afi[afi].network_id) + if (network_id + && network_id != nifp->afi[afi].network_id) return NHRP_ROUTE_OFF_NBMA; } if (sockunion_family(&via[i]) == AF_UNSPEC) @@ -290,10 +313,12 @@ enum nhrp_route_type nhrp_route_address(struct interface *in_ifp, union sockunio if (ifp) { c = nhrp_cache_get(ifp, addr, 0); if (c && c->cur.type >= NHRP_CACHE_DYNAMIC) { - if (p) memset(p, 0, sizeof(*p)); + if (p) + memset(p, 0, sizeof(*p)); if (c->cur.type == NHRP_CACHE_LOCAL) return NHRP_ROUTE_LOCAL; - if (peer) *peer = nhrp_peer_ref(c->cur.peer); + if (peer) + *peer = nhrp_peer_ref(c->cur.peer); return NHRP_ROUTE_NBMA_NEXTHOP; } } @@ -301,14 +326,13 @@ enum nhrp_route_type nhrp_route_address(struct interface *in_ifp, union sockunio return NHRP_ROUTE_BLACKHOLE; } -static void -nhrp_zebra_connected (struct zclient *zclient) +static void nhrp_zebra_connected(struct zclient *zclient) { zclient_send_reg_requests(zclient, VRF_DEFAULT); zebra_redistribute_send(ZEBRA_REDISTRIBUTE_ADD, zclient, AFI_IP, - ZEBRA_ROUTE_ALL, 0, VRF_DEFAULT); + ZEBRA_ROUTE_ALL, 0, VRF_DEFAULT); zebra_redistribute_send(ZEBRA_REDISTRIBUTE_ADD, zclient, AFI_IP6, - ZEBRA_ROUTE_ALL, 0, VRF_DEFAULT); + ZEBRA_ROUTE_ALL, 0, VRF_DEFAULT); } void nhrp_zebra_init(void) diff --git a/nhrpd/nhrp_shortcut.c b/nhrpd/nhrp_shortcut.c index 4faa9d786..9ed251706 100644 --- a/nhrpd/nhrp_shortcut.c +++ b/nhrpd/nhrp_shortcut.c @@ -28,7 +28,7 @@ static void nhrp_shortcut_check_use(struct nhrp_shortcut *s) if (s->expiring && s->cache && s->cache->used) { debugf(NHRP_DEBUG_ROUTE, "Shortcut %s used and expiring", - prefix2str(s->p, buf, sizeof buf)); + prefix2str(s->p, buf, sizeof buf)); nhrp_shortcut_send_resolution_req(s); } } @@ -38,22 +38,25 @@ static int nhrp_shortcut_do_expire(struct thread *t) struct nhrp_shortcut *s = THREAD_ARG(t); s->t_timer = NULL; - thread_add_timer(master, nhrp_shortcut_do_purge, s, - s->holding_time / 3, &s->t_timer); + thread_add_timer(master, nhrp_shortcut_do_purge, s, s->holding_time / 3, + &s->t_timer); s->expiring = 1; nhrp_shortcut_check_use(s); return 0; } -static void nhrp_shortcut_cache_notify(struct notifier_block *n, unsigned long cmd) +static void nhrp_shortcut_cache_notify(struct notifier_block *n, + unsigned long cmd) { - struct nhrp_shortcut *s = container_of(n, struct nhrp_shortcut, cache_notifier); + struct nhrp_shortcut *s = + container_of(n, struct nhrp_shortcut, cache_notifier); switch (cmd) { case NOTIFY_CACHE_UP: if (!s->route_installed) { - nhrp_route_announce(1, s->type, s->p, NULL, &s->cache->remote_addr, 0); + nhrp_route_announce(1, s->type, s->p, NULL, + &s->cache->remote_addr, 0); s->route_installed = 1; } break; @@ -63,7 +66,8 @@ static void nhrp_shortcut_cache_notify(struct notifier_block *n, unsigned long c case NOTIFY_CACHE_DOWN: case NOTIFY_CACHE_DELETE: if (s->route_installed) { - nhrp_route_announce(0, NHRP_CACHE_INVALID, s->p, NULL, NULL, 0); + nhrp_route_announce(0, NHRP_CACHE_INVALID, s->p, NULL, + NULL, 0); s->route_installed = 0; } if (cmd == NOTIFY_CACHE_DELETE) @@ -72,7 +76,9 @@ static void nhrp_shortcut_cache_notify(struct notifier_block *n, unsigned long c } } -static void nhrp_shortcut_update_binding(struct nhrp_shortcut *s, enum nhrp_cache_type type, struct nhrp_cache *c, int holding_time) +static void nhrp_shortcut_update_binding(struct nhrp_shortcut *s, + enum nhrp_cache_type type, + struct nhrp_cache *c, int holding_time) { s->type = type; if (c != s->cache) { @@ -82,15 +88,19 @@ static void nhrp_shortcut_update_binding(struct nhrp_shortcut *s, enum nhrp_cach } s->cache = c; if (s->cache) { - nhrp_cache_notify_add(s->cache, &s->cache_notifier, nhrp_shortcut_cache_notify); + nhrp_cache_notify_add(s->cache, &s->cache_notifier, + nhrp_shortcut_cache_notify); if (s->cache->route_installed) { - /* Force renewal of Zebra announce on prefix change */ + /* Force renewal of Zebra announce on prefix + * change */ s->route_installed = 0; - nhrp_shortcut_cache_notify(&s->cache_notifier, NOTIFY_CACHE_UP); + nhrp_shortcut_cache_notify(&s->cache_notifier, + NOTIFY_CACHE_UP); } } if (!s->cache || !s->cache->route_installed) - nhrp_shortcut_cache_notify(&s->cache_notifier, NOTIFY_CACHE_DOWN); + nhrp_shortcut_cache_notify(&s->cache_notifier, + NOTIFY_CACHE_DOWN); } if (s->type == NHRP_CACHE_NEGATIVE && !s->route_installed) { nhrp_route_announce(1, s->type, s->p, NULL, NULL, 0); @@ -119,7 +129,7 @@ static void nhrp_shortcut_delete(struct nhrp_shortcut *s) nhrp_reqid_free(&nhrp_packet_reqid, &s->reqid); debugf(NHRP_DEBUG_ROUTE, "Shortcut %s purged", - prefix2str(s->p, buf, sizeof buf)); + prefix2str(s->p, buf, sizeof buf)); nhrp_shortcut_update_binding(s, NHRP_CACHE_INVALID, NULL, 0); @@ -153,12 +163,13 @@ static struct nhrp_shortcut *nhrp_shortcut_get(struct prefix *p) rn = route_node_get(shortcut_rib[afi], p); if (!rn->info) { - s = rn->info = XCALLOC(MTYPE_NHRP_SHORTCUT, sizeof(struct nhrp_shortcut)); + s = rn->info = XCALLOC(MTYPE_NHRP_SHORTCUT, + sizeof(struct nhrp_shortcut)); s->type = NHRP_CACHE_INVALID; s->p = &rn->p; debugf(NHRP_DEBUG_ROUTE, "Shortcut %s created", - prefix2str(s->p, buf, sizeof buf)); + prefix2str(s->p, buf, sizeof buf)); } else { s = rn->info; route_unlock_node(rn); @@ -166,15 +177,18 @@ static struct nhrp_shortcut *nhrp_shortcut_get(struct prefix *p) return s; } -static void nhrp_shortcut_recv_resolution_rep(struct nhrp_reqid *reqid, void *arg) +static void nhrp_shortcut_recv_resolution_rep(struct nhrp_reqid *reqid, + void *arg) { struct nhrp_packet_parser *pp = arg; - struct nhrp_shortcut *s = container_of(reqid, struct nhrp_shortcut, reqid); + struct nhrp_shortcut *s = + container_of(reqid, struct nhrp_shortcut, reqid); struct nhrp_shortcut *ps; struct nhrp_extension_header *ext; struct nhrp_cie_header *cie; struct nhrp_cache *c = NULL; - union sockunion *proto, cie_proto, *nbma, *nbma_natoa, cie_nbma, nat_nbma; + union sockunion *proto, cie_proto, *nbma, *nbma_natoa, cie_nbma, + nat_nbma; struct prefix prefix, route_prefix; struct zbuf extpl; char bufp[PREFIX_STRLEN], buf[3][SU_ADDRSTRLEN]; @@ -185,12 +199,16 @@ static void nhrp_shortcut_recv_resolution_rep(struct nhrp_reqid *reqid, void *ar thread_add_timer(master, nhrp_shortcut_do_purge, s, 1, &s->t_timer); if (pp->hdr->type != NHRP_PACKET_RESOLUTION_REPLY) { - if (pp->hdr->type == NHRP_PACKET_ERROR_INDICATION && - pp->hdr->u.error.code == NHRP_ERROR_PROTOCOL_ADDRESS_UNREACHABLE) { - debugf(NHRP_DEBUG_COMMON, "Shortcut: Resolution: Protocol address unreachable"); - nhrp_shortcut_update_binding(s, NHRP_CACHE_NEGATIVE, NULL, holding_time); + if (pp->hdr->type == NHRP_PACKET_ERROR_INDICATION + && pp->hdr->u.error.code + == NHRP_ERROR_PROTOCOL_ADDRESS_UNREACHABLE) { + debugf(NHRP_DEBUG_COMMON, + "Shortcut: Resolution: Protocol address unreachable"); + nhrp_shortcut_update_binding(s, NHRP_CACHE_NEGATIVE, + NULL, holding_time); } else { - debugf(NHRP_DEBUG_COMMON, "Shortcut: Resolution failed"); + debugf(NHRP_DEBUG_COMMON, + "Shortcut: Resolution failed"); } return; } @@ -208,19 +226,22 @@ static void nhrp_shortcut_recv_resolution_rep(struct nhrp_reqid *reqid, void *ar /* Minor sanity check */ prefix2sockunion(s->p, &cie_proto); if (!sockunion_same(&cie_proto, &pp->dst_proto)) { - debugf(NHRP_DEBUG_COMMON, "Shortcut: Warning dst_proto altered from %s to %s", - sockunion2str(&cie_proto, buf[0], sizeof buf[0]), - sockunion2str(&pp->dst_proto, buf[1], sizeof buf[1])); + debugf(NHRP_DEBUG_COMMON, + "Shortcut: Warning dst_proto altered from %s to %s", + sockunion2str(&cie_proto, buf[0], sizeof buf[0]), + sockunion2str(&pp->dst_proto, buf[1], sizeof buf[1])); } /* One or more CIEs should be given as reply, we support only one */ cie = nhrp_cie_pull(&pp->payload, pp->hdr, &cie_nbma, &cie_proto); if (!cie || cie->code != NHRP_CODE_SUCCESS) { - debugf(NHRP_DEBUG_COMMON, "Shortcut: CIE code %d", cie ? cie->code : -1); + debugf(NHRP_DEBUG_COMMON, "Shortcut: CIE code %d", + cie ? cie->code : -1); return; } - proto = sockunion_family(&cie_proto) != AF_UNSPEC ? &cie_proto : &pp->dst_proto; + proto = sockunion_family(&cie_proto) != AF_UNSPEC ? &cie_proto + : &pp->dst_proto; if (cie->holding_time) holding_time = htons(cie->holding_time); @@ -228,19 +249,22 @@ static void nhrp_shortcut_recv_resolution_rep(struct nhrp_reqid *reqid, void *ar prefix.prefixlen = cie->prefix_length; /* Sanity check prefix length */ - if (prefix.prefixlen >= 8*prefix_blen(&prefix) || prefix.prefixlen == 0) { - prefix.prefixlen = 8*prefix_blen(&prefix); - } else if (nhrp_route_address(NULL, &pp->dst_proto, &route_prefix, NULL) == NHRP_ROUTE_NBMA_NEXTHOP) { + if (prefix.prefixlen >= 8 * prefix_blen(&prefix) + || prefix.prefixlen == 0) { + prefix.prefixlen = 8 * prefix_blen(&prefix); + } else if (nhrp_route_address(NULL, &pp->dst_proto, &route_prefix, NULL) + == NHRP_ROUTE_NBMA_NEXTHOP) { if (prefix.prefixlen < route_prefix.prefixlen) prefix.prefixlen = route_prefix.prefixlen; } - debugf(NHRP_DEBUG_COMMON, "Shortcut: %s is at proto %s cie-nbma %s nat-nbma %s cie-holdtime %d", - prefix2str(&prefix, bufp, sizeof bufp), - sockunion2str(proto, buf[0], sizeof buf[0]), - sockunion2str(&cie_nbma, buf[1], sizeof buf[1]), - sockunion2str(&nat_nbma, buf[2], sizeof buf[2]), - htons(cie->holding_time)); + debugf(NHRP_DEBUG_COMMON, + "Shortcut: %s is at proto %s cie-nbma %s nat-nbma %s cie-holdtime %d", + prefix2str(&prefix, bufp, sizeof bufp), + sockunion2str(proto, buf[0], sizeof buf[0]), + sockunion2str(&cie_nbma, buf[1], sizeof buf[1]), + sockunion2str(&nat_nbma, buf[2], sizeof buf[2]), + htons(cie->holding_time)); /* Update cache entry for the protocol to nbma binding */ if (sockunion_family(&nat_nbma) != AF_UNSPEC) { @@ -253,10 +277,10 @@ static void nhrp_shortcut_recv_resolution_rep(struct nhrp_reqid *reqid, void *ar if (sockunion_family(nbma)) { c = nhrp_cache_get(pp->ifp, proto, 1); if (c) { - nhrp_cache_update_binding( - c, NHRP_CACHE_CACHED, holding_time, - nhrp_peer_get(pp->ifp, nbma), - htons(cie->mtu), nbma_natoa); + nhrp_cache_update_binding(c, NHRP_CACHE_CACHED, + holding_time, + nhrp_peer_get(pp->ifp, nbma), + htons(cie->mtu), nbma_natoa); } } @@ -265,7 +289,8 @@ static void nhrp_shortcut_recv_resolution_rep(struct nhrp_reqid *reqid, void *ar ps = nhrp_shortcut_get(&prefix); if (ps) { ps->addr = s->addr; - nhrp_shortcut_update_binding(ps, NHRP_CACHE_CACHED, c, holding_time); + nhrp_shortcut_update_binding(ps, NHRP_CACHE_CACHED, c, + holding_time); } } @@ -280,7 +305,8 @@ static void nhrp_shortcut_send_resolution_req(struct nhrp_shortcut *s) struct nhrp_interface *nifp; struct nhrp_peer *peer; - if (nhrp_route_address(NULL, &s->addr, NULL, &peer) != NHRP_ROUTE_NBMA_NEXTHOP) + if (nhrp_route_address(NULL, &s->addr, NULL, &peer) + != NHRP_ROUTE_NBMA_NEXTHOP) return; if (s->type == NHRP_CACHE_INVALID || s->type == NHRP_CACHE_NEGATIVE) @@ -291,12 +317,16 @@ static void nhrp_shortcut_send_resolution_req(struct nhrp_shortcut *s) /* Create request */ zb = zbuf_alloc(1500); - hdr = nhrp_packet_push(zb, NHRP_PACKET_RESOLUTION_REQUEST, - &nifp->nbma, &nifp->afi[family2afi(sockunion_family(&s->addr))].addr, &s->addr); - hdr->u.request_id = htonl(nhrp_reqid_alloc(&nhrp_packet_reqid, &s->reqid, nhrp_shortcut_recv_resolution_rep)); - hdr->flags = htons(NHRP_FLAG_RESOLUTION_SOURCE_IS_ROUTER | - NHRP_FLAG_RESOLUTION_AUTHORATIVE | - NHRP_FLAG_RESOLUTION_SOURCE_STABLE); + hdr = nhrp_packet_push( + zb, NHRP_PACKET_RESOLUTION_REQUEST, &nifp->nbma, + &nifp->afi[family2afi(sockunion_family(&s->addr))].addr, + &s->addr); + hdr->u.request_id = + htonl(nhrp_reqid_alloc(&nhrp_packet_reqid, &s->reqid, + nhrp_shortcut_recv_resolution_rep)); + hdr->flags = htons(NHRP_FLAG_RESOLUTION_SOURCE_IS_ROUTER + | NHRP_FLAG_RESOLUTION_AUTHORATIVE + | NHRP_FLAG_RESOLUTION_SOURCE_STABLE); /* RFC2332 - One or zero CIEs, if CIE is present contains: * - Prefix length: widest acceptable prefix we accept (if U set, 0xff) @@ -346,17 +376,21 @@ void nhrp_shortcut_terminate(void) route_table_finish(shortcut_rib[AFI_IP6]); } -void nhrp_shortcut_foreach(afi_t afi, void (*cb)(struct nhrp_shortcut *, void *), void *ctx) +void nhrp_shortcut_foreach(afi_t afi, + void (*cb)(struct nhrp_shortcut *, void *), + void *ctx) { struct route_table *rt = shortcut_rib[afi]; struct route_node *rn; route_table_iter_t iter; - if (!rt) return; + if (!rt) + return; route_table_iter_init(&iter, rt); while ((rn = route_table_iter_next(&iter)) != NULL) { - if (rn->info) cb(rn->info, ctx); + if (rn->info) + cb(rn->info, ctx); } route_table_iter_cleanup(&iter); } @@ -401,9 +435,8 @@ static void nhrp_shortcut_purge_prefix(struct nhrp_shortcut *s, void *ctx) void nhrp_shortcut_prefix_change(const struct prefix *p, int deleted) { struct purge_ctx pctx = { - .p = p, - .deleted = deleted, + .p = p, .deleted = deleted, }; - nhrp_shortcut_foreach(family2afi(PREFIX_FAMILY(p)), nhrp_shortcut_purge_prefix, &pctx); + nhrp_shortcut_foreach(family2afi(PREFIX_FAMILY(p)), + nhrp_shortcut_purge_prefix, &pctx); } - diff --git a/nhrpd/nhrp_vc.c b/nhrpd/nhrp_vc.c index d0915bc7a..c373411d6 100644 --- a/nhrpd/nhrp_vc.c +++ b/nhrpd/nhrp_vc.c @@ -31,18 +31,16 @@ static struct list_head childlist_head[512]; static unsigned int nhrp_vc_key(void *peer_data) { struct nhrp_vc *vc = peer_data; - return jhash_2words( - sockunion_hash(&vc->local.nbma), - sockunion_hash(&vc->remote.nbma), - 0); + return jhash_2words(sockunion_hash(&vc->local.nbma), + sockunion_hash(&vc->remote.nbma), 0); } static int nhrp_vc_cmp(const void *cache_data, const void *key_data) { const struct nhrp_vc *a = cache_data; const struct nhrp_vc *b = key_data; - return sockunion_same(&a->local.nbma, &b->local.nbma) && - sockunion_same(&a->remote.nbma, &b->remote.nbma); + return sockunion_same(&a->local.nbma, &b->local.nbma) + && sockunion_same(&a->remote.nbma, &b->remote.nbma); } static void *nhrp_vc_alloc(void *data) @@ -51,10 +49,11 @@ static void *nhrp_vc_alloc(void *data) vc = XMALLOC(MTYPE_NHRP_VC, sizeof(struct nhrp_vc)); if (vc) { - *vc = (struct nhrp_vc) { + *vc = (struct nhrp_vc){ .local.nbma = key->local.nbma, .remote.nbma = key->remote.nbma, - .notifier_list = NOTIFIER_LIST_INITIALIZER(&vc->notifier_list), + .notifier_list = + NOTIFIER_LIST_INITIALIZER(&vc->notifier_list), }; } @@ -66,7 +65,8 @@ static void nhrp_vc_free(void *data) XFREE(MTYPE_NHRP_VC, data); } -struct nhrp_vc *nhrp_vc_get(const union sockunion *src, const union sockunion *dst, int create) +struct nhrp_vc *nhrp_vc_get(const union sockunion *src, + const union sockunion *dst, int create) { struct nhrp_vc key; key.local.nbma = *src; @@ -105,7 +105,8 @@ int nhrp_vc_ipsec_updown(uint32_t child_id, struct nhrp_vc *vc) uint32_t child_hash = child_id % ZEBRA_NUM_OF(childlist_head); int abort_migration = 0; - list_for_each_entry(lsa, &childlist_head[child_hash], childlist_entry) { + list_for_each_entry(lsa, &childlist_head[child_hash], childlist_entry) + { if (lsa->id == child_id) { sa = lsa; break; @@ -113,17 +114,21 @@ int nhrp_vc_ipsec_updown(uint32_t child_id, struct nhrp_vc *vc) } if (!sa) { - if (!vc) return 0; + if (!vc) + return 0; sa = XMALLOC(MTYPE_NHRP_VC, sizeof(struct child_sa)); - if (!sa) return 0; + if (!sa) + return 0; - *sa = (struct child_sa) { + *sa = (struct child_sa){ .id = child_id, - .childlist_entry = LIST_INITIALIZER(sa->childlist_entry), + .childlist_entry = + LIST_INITIALIZER(sa->childlist_entry), .vc = NULL, }; - list_add_tail(&sa->childlist_entry, &childlist_head[child_hash]); + list_add_tail(&sa->childlist_entry, + &childlist_head[child_hash]); } if (sa->vc == vc) @@ -138,15 +143,17 @@ int nhrp_vc_ipsec_updown(uint32_t child_id, struct nhrp_vc *vc) /* Notify old VC of migration */ sa->vc->abort_migration = 0; debugf(NHRP_DEBUG_COMMON, "IPsec NBMA change of %s to %s", - sockunion2str(&sa->vc->remote.nbma, buf[0], sizeof buf[0]), - sockunion2str(&vc->remote.nbma, buf[1], sizeof buf[1])); + sockunion2str(&sa->vc->remote.nbma, buf[0], + sizeof buf[0]), + sockunion2str(&vc->remote.nbma, buf[1], sizeof buf[1])); nhrp_vc_update(sa->vc, NOTIFY_VC_IPSEC_UPDATE_NBMA); abort_migration = sa->vc->abort_migration; } if (sa->vc) { /* Deattach old VC */ sa->vc->ipsec--; - if (!sa->vc->ipsec) nhrp_vc_ipsec_reset(sa->vc); + if (!sa->vc->ipsec) + nhrp_vc_ipsec_reset(sa->vc); nhrp_vc_update(sa->vc, NOTIFY_VC_IPSEC_CHANGED); } @@ -160,7 +167,8 @@ int nhrp_vc_ipsec_updown(uint32_t child_id, struct nhrp_vc *vc) return abort_migration; } -void nhrp_vc_notify_add(struct nhrp_vc *vc, struct notifier_block *n, notifier_fn_t action) +void nhrp_vc_notify_add(struct nhrp_vc *vc, struct notifier_block *n, + notifier_fn_t action) { notifier_add(n, &vc->notifier_list, action); } @@ -186,8 +194,7 @@ static void nhrp_vc_iterator(struct hash_backet *b, void *ctx) void nhrp_vc_foreach(void (*cb)(struct nhrp_vc *, void *), void *ctx) { struct nhrp_vc_iterator_ctx ic = { - .cb = cb, - .ctx = ctx, + .cb = cb, .ctx = ctx, }; hash_iterate(nhrp_vc_hash, nhrp_vc_iterator, &ic); } @@ -196,9 +203,7 @@ void nhrp_vc_init(void) { size_t i; - nhrp_vc_hash = hash_create(nhrp_vc_key, - nhrp_vc_cmp, - "NHRP VC hash"); + nhrp_vc_hash = hash_create(nhrp_vc_key, nhrp_vc_cmp, "NHRP VC hash"); for (i = 0; i < ZEBRA_NUM_OF(childlist_head); i++) list_init(&childlist_head[i]); } @@ -209,7 +214,8 @@ void nhrp_vc_reset(void) size_t i; for (i = 0; i < ZEBRA_NUM_OF(childlist_head); i++) { - list_for_each_entry_safe(sa, n, &childlist_head[i], childlist_entry) + list_for_each_entry_safe(sa, n, &childlist_head[i], + childlist_entry) nhrp_vc_ipsec_updown(sa->id, 0); } } diff --git a/nhrpd/nhrp_vty.c b/nhrpd/nhrp_vty.c index e0d0268e4..cfedc1c6b 100644 --- a/nhrpd/nhrp_vty.c +++ b/nhrpd/nhrp_vty.c @@ -17,56 +17,53 @@ #include "netlink.h" static struct cmd_node zebra_node = { - .node = ZEBRA_NODE, + .node = ZEBRA_NODE, .prompt = "%s(config-router)# ", - .vtysh = 1, + .vtysh = 1, }; static struct cmd_node nhrp_interface_node = { - .node = INTERFACE_NODE, + .node = INTERFACE_NODE, .prompt = "%s(config-if)# ", - .vtysh = 1, + .vtysh = 1, }; #define NHRP_DEBUG_FLAGS_CMD "<all|common|event|interface|kernel|route|vici>" -#define NHRP_DEBUG_FLAGS_STR \ - "All messages\n" \ - "Common messages (default)\n" \ - "Event manager messages\n" \ - "Interface messages\n" \ - "Kernel messages\n" \ - "Route messages\n" \ +#define NHRP_DEBUG_FLAGS_STR \ + "All messages\n" \ + "Common messages (default)\n" \ + "Event manager messages\n" \ + "Interface messages\n" \ + "Kernel messages\n" \ + "Route messages\n" \ "VICI messages\n" static const struct message debug_flags_desc[] = { - { NHRP_DEBUG_ALL, "all" }, - { NHRP_DEBUG_COMMON, "common" }, - { NHRP_DEBUG_IF, "interface" }, - { NHRP_DEBUG_KERNEL, "kernel" }, - { NHRP_DEBUG_ROUTE, "route" }, - { NHRP_DEBUG_VICI, "vici" }, - { NHRP_DEBUG_EVENT, "event" }, - { 0 } -}; + {NHRP_DEBUG_ALL, "all"}, {NHRP_DEBUG_COMMON, "common"}, + {NHRP_DEBUG_IF, "interface"}, {NHRP_DEBUG_KERNEL, "kernel"}, + {NHRP_DEBUG_ROUTE, "route"}, {NHRP_DEBUG_VICI, "vici"}, + {NHRP_DEBUG_EVENT, "event"}, {0}}; static const struct message interface_flags_desc[] = { - { NHRP_IFF_SHORTCUT, "shortcut" }, - { NHRP_IFF_REDIRECT, "redirect" }, - { NHRP_IFF_REG_NO_UNIQUE, "registration no-unique" }, - { 0 } -}; + {NHRP_IFF_SHORTCUT, "shortcut"}, + {NHRP_IFF_REDIRECT, "redirect"}, + {NHRP_IFF_REG_NO_UNIQUE, "registration no-unique"}, + {0}}; static int nhrp_vty_return(struct vty *vty, int ret) { - static const char * const errmsgs[] = { - [NHRP_ERR_FAIL] = "Command failed", - [NHRP_ERR_NO_MEMORY] = "Out of memory", - [NHRP_ERR_UNSUPPORTED_INTERFACE] = "NHRP not supported on this interface", - [NHRP_ERR_NHRP_NOT_ENABLED] = "NHRP not enabled (set 'nhrp network-id' first)", - [NHRP_ERR_ENTRY_EXISTS] = "Entry exists already", - [NHRP_ERR_ENTRY_NOT_FOUND] = "Entry not found", - [NHRP_ERR_PROTOCOL_ADDRESS_MISMATCH] = "Protocol address family does not match command (ip/ipv6 mismatch)", + static const char *const errmsgs[] = { + [NHRP_ERR_FAIL] = "Command failed", + [NHRP_ERR_NO_MEMORY] = "Out of memory", + [NHRP_ERR_UNSUPPORTED_INTERFACE] = + "NHRP not supported on this interface", + [NHRP_ERR_NHRP_NOT_ENABLED] = + "NHRP not enabled (set 'nhrp network-id' first)", + [NHRP_ERR_ENTRY_EXISTS] = "Entry exists already", + [NHRP_ERR_ENTRY_NOT_FOUND] = "Entry not found", + [NHRP_ERR_PROTOCOL_ADDRESS_MISMATCH] = + "Protocol address family does not match command (ip/ipv6 mismatch)", }; const char *str = NULL; char buf[256]; @@ -83,14 +80,14 @@ static int nhrp_vty_return(struct vty *vty, int ret) snprintf(buf, sizeof(buf), "Unknown error %d", ret); } - vty_out (vty, "%% %s\n", str); + vty_out(vty, "%% %s\n", str); - return CMD_WARNING_CONFIG_FAILED;; + return CMD_WARNING_CONFIG_FAILED; + ; } -static int toggle_flag( - struct vty *vty, const struct message *flag_desc, - const char *name, int on_off, unsigned *flags) +static int toggle_flag(struct vty *vty, const struct message *flag_desc, + const char *name, int on_off, unsigned *flags) { int i; @@ -104,8 +101,9 @@ static int toggle_flag( return CMD_SUCCESS; } - vty_out (vty, "%% Invalid value %s\n", name); - return CMD_WARNING_CONFIG_FAILED;; + vty_out(vty, "%% Invalid value %s\n", name); + return CMD_WARNING_CONFIG_FAILED; + ; } #ifndef NO_DEBUG @@ -118,7 +116,7 @@ DEFUN_NOSH(show_debugging_nhrp, show_debugging_nhrp_cmd, { int i; - vty_out (vty, "NHRP debugging status:\n"); + vty_out(vty, "NHRP debugging status:\n"); for (i = 0; debug_flags_desc[i].str != NULL; i++) { if (debug_flags_desc[i].key == NHRP_DEBUG_ALL) @@ -126,7 +124,7 @@ DEFUN_NOSH(show_debugging_nhrp, show_debugging_nhrp_cmd, if (!(debug_flags_desc[i].key & debug_flags)) continue; - vty_out (vty, " NHRP %s debugging is on\n", + vty_out(vty, " NHRP %s debugging is on\n", debug_flags_desc[i].str); } @@ -139,7 +137,8 @@ DEFUN(debug_nhrp, debug_nhrp_cmd, "NHRP information\n" NHRP_DEBUG_FLAGS_STR) { - return toggle_flag(vty, debug_flags_desc, argv[2]->text, 1, &debug_flags); + return toggle_flag(vty, debug_flags_desc, argv[2]->text, 1, + &debug_flags); } DEFUN(no_debug_nhrp, no_debug_nhrp_cmd, @@ -149,7 +148,8 @@ DEFUN(no_debug_nhrp, no_debug_nhrp_cmd, "NHRP information\n" NHRP_DEBUG_FLAGS_STR) { - return toggle_flag(vty, debug_flags_desc, argv[3]->text, 0, &debug_flags); + return toggle_flag(vty, debug_flags_desc, argv[3]->text, 0, + &debug_flags); } #endif /* NO_DEBUG */ @@ -158,7 +158,7 @@ static int nhrp_config_write(struct vty *vty) { #ifndef NO_DEBUG if (debug_flags == NHRP_DEBUG_ALL) { - vty_out (vty, "debug nhrp all\n"); + vty_out(vty, "debug nhrp all\n"); } else { int i; @@ -167,20 +167,18 @@ static int nhrp_config_write(struct vty *vty) continue; if (!(debug_flags & debug_flags_desc[i].key)) continue; - vty_out (vty, "debug nhrp %s\n", - debug_flags_desc[i].str); + vty_out(vty, "debug nhrp %s\n", + debug_flags_desc[i].str); } } - vty_out (vty, "!\n"); + vty_out(vty, "!\n"); #endif /* NO_DEBUG */ if (nhrp_event_socket_path) { - vty_out (vty, "nhrp event socket %s\n", - nhrp_event_socket_path); + vty_out(vty, "nhrp event socket %s\n", nhrp_event_socket_path); } if (netlink_nflog_group) { - vty_out (vty, "nhrp nflog-group %d\n", - netlink_nflog_group); + vty_out(vty, "nhrp nflog-group %d\n", netlink_nflog_group); } return 0; @@ -199,7 +197,8 @@ static afi_t cmd_to_afi(const struct cmd_token *tok) static const char *afi_to_cmd(afi_t afi) { - if (afi == AFI_IP6) return "ipv6"; + if (afi == AFI_IP6) + return "ipv6"; return "ip"; } @@ -264,7 +263,7 @@ DEFUN(tunnel_protection, tunnel_protection_cmd, VTY_DECLVAR_CONTEXT(interface, ifp); nhrp_interface_set_protection(ifp, argv[4]->arg, - argc > 6 ? argv[6]->arg : NULL); + argc > 6 ? argv[6]->arg : NULL); return CMD_SUCCESS; } @@ -348,7 +347,8 @@ DEFUN(if_nhrp_flags, if_nhrp_flags_cmd, struct nhrp_interface *nifp = ifp->info; afi_t afi = cmd_to_afi(argv[0]); - return toggle_flag(vty, interface_flags_desc, argv[2]->text, 1, &nifp->afi[afi].flags); + return toggle_flag(vty, interface_flags_desc, argv[2]->text, 1, + &nifp->afi[afi].flags); } DEFUN(if_no_nhrp_flags, if_no_nhrp_flags_cmd, @@ -363,7 +363,8 @@ DEFUN(if_no_nhrp_flags, if_no_nhrp_flags_cmd, struct nhrp_interface *nifp = ifp->info; afi_t afi = cmd_to_afi(argv[1]); - return toggle_flag(vty, interface_flags_desc, argv[3]->text, 0, &nifp->afi[afi].flags); + return toggle_flag(vty, interface_flags_desc, argv[3]->text, 0, + &nifp->afi[afi].flags); } DEFUN(if_nhrp_reg_flags, if_nhrp_reg_flags_cmd, @@ -378,7 +379,8 @@ DEFUN(if_nhrp_reg_flags, if_nhrp_reg_flags_cmd, afi_t afi = cmd_to_afi(argv[0]); char name[256]; snprintf(name, sizeof(name), "registration %s", argv[3]->text); - return toggle_flag(vty, interface_flags_desc, name, 1, &nifp->afi[afi].flags); + return toggle_flag(vty, interface_flags_desc, name, 1, + &nifp->afi[afi].flags); } DEFUN(if_no_nhrp_reg_flags, if_no_nhrp_reg_flags_cmd, @@ -394,7 +396,8 @@ DEFUN(if_no_nhrp_reg_flags, if_no_nhrp_reg_flags_cmd, afi_t afi = cmd_to_afi(argv[1]); char name[256]; snprintf(name, sizeof(name), "registration %s", argv[4]->text); - return toggle_flag(vty, interface_flags_desc, name, 0, &nifp->afi[afi].flags); + return toggle_flag(vty, interface_flags_desc, name, 0, + &nifp->afi[afi].flags); } DEFUN(if_nhrp_holdtime, if_nhrp_holdtime_cmd, @@ -446,8 +449,8 @@ DEFUN(if_nhrp_mtu, if_nhrp_mtu_cmd, if (argv[3]->arg[0] == 'o') { nifp->afi[AFI_IP].configured_mtu = -1; } else { - nifp->afi[AFI_IP].configured_mtu = strtoul(argv[3]->arg, NULL, - 10); + nifp->afi[AFI_IP].configured_mtu = + strtoul(argv[3]->arg, NULL, 10); } nhrp_interface_update_mtu(ifp, AFI_IP); @@ -486,8 +489,8 @@ DEFUN(if_nhrp_map, if_nhrp_map_cmd, union sockunion proto_addr, nbma_addr; struct nhrp_cache *c; - if (str2sockunion(argv[3]->arg, &proto_addr) < 0 || - afi2family(afi) != sockunion_family(&proto_addr)) + if (str2sockunion(argv[3]->arg, &proto_addr) < 0 + || afi2family(afi) != sockunion_family(&proto_addr)) return nhrp_vty_return(vty, NHRP_ERR_PROTOCOL_ADDRESS_MISMATCH); c = nhrp_cache_get(ifp, &proto_addr, 1); @@ -496,12 +499,14 @@ DEFUN(if_nhrp_map, if_nhrp_map_cmd, c->map = 1; if (strmatch(argv[4]->text, "local")) { - nhrp_cache_update_binding(c, NHRP_CACHE_LOCAL, 0, NULL, 0, NULL); - } else{ + nhrp_cache_update_binding(c, NHRP_CACHE_LOCAL, 0, NULL, 0, + NULL); + } else { if (str2sockunion(argv[4]->arg, &nbma_addr) < 0) return nhrp_vty_return(vty, NHRP_ERR_FAIL); nhrp_cache_update_binding(c, NHRP_CACHE_STATIC, 0, - nhrp_peer_get(ifp, &nbma_addr), 0, NULL); + nhrp_peer_get(ifp, &nbma_addr), 0, + NULL); } return CMD_SUCCESS; @@ -516,13 +521,13 @@ DEFUN(if_no_nhrp_map, if_no_nhrp_map_cmd, "IPv4 protocol address\n" "IPv6 protocol address\n") { - VTY_DECLVAR_CONTEXT(interface,ifp); + VTY_DECLVAR_CONTEXT(interface, ifp); afi_t afi = cmd_to_afi(argv[1]); union sockunion proto_addr; struct nhrp_cache *c; - if (str2sockunion(argv[4]->arg, &proto_addr) < 0 || - afi2family(afi) != sockunion_family(&proto_addr)) + if (str2sockunion(argv[4]->arg, &proto_addr) < 0 + || afi2family(afi) != sockunion_family(&proto_addr)) return nhrp_vty_return(vty, NHRP_ERR_PROTOCOL_ADDRESS_MISMATCH); c = nhrp_cache_get(ifp, &proto_addr, 0); @@ -598,50 +603,41 @@ static void show_ip_nhrp_cache(struct nhrp_cache *c, void *pctx) return; if (!ctx->count) { - vty_out (vty, "%-8s %-8s %-24s %-24s %-6s %s\n", - "Iface", - "Type", - "Protocol", - "NBMA", - "Flags", - "Identity"); + vty_out(vty, "%-8s %-8s %-24s %-24s %-6s %s\n", "Iface", "Type", + "Protocol", "NBMA", "Flags", "Identity"); } ctx->count++; - vty_out(ctx->vty, "%-8s %-8s %-24s %-24s %c%c%c %s\n", - c->ifp->name, + vty_out(ctx->vty, "%-8s %-8s %-24s %-24s %c%c%c %s\n", c->ifp->name, nhrp_cache_type_str[c->cur.type], sockunion2str(&c->remote_addr, buf[0], sizeof buf[0]), - c->cur.peer ? sockunion2str(&c->cur.peer->vc->remote.nbma, buf[1], sizeof buf[1]) : "-", - c->used ? 'U' : ' ', - c->t_timeout ? 'T' : ' ', + c->cur.peer ? sockunion2str(&c->cur.peer->vc->remote.nbma, + buf[1], sizeof buf[1]) + : "-", + c->used ? 'U' : ' ', c->t_timeout ? 'T' : ' ', c->t_auth ? 'A' : ' ', c->cur.peer ? c->cur.peer->vc->remote.id : "-"); } -static void show_ip_nhrp_nhs(struct nhrp_nhs *n, struct nhrp_registration *reg, void *pctx) +static void show_ip_nhrp_nhs(struct nhrp_nhs *n, struct nhrp_registration *reg, + void *pctx) { struct info_ctx *ctx = pctx; struct vty *vty = ctx->vty; char buf[2][SU_ADDRSTRLEN]; if (!ctx->count) { - vty_out (vty, "%-8s %-24s %-16s %-16s\n", - "Iface", - "FQDN", - "NBMA", - "Protocol"); + vty_out(vty, "%-8s %-24s %-16s %-16s\n", "Iface", "FQDN", + "NBMA", "Protocol"); } ctx->count++; - vty_out (vty, "%-8s %-24s %-16s %-16s\n", - n->ifp->name, - n->nbma_fqdn, - (reg && reg->peer) ? sockunion2str(®->peer->vc->remote.nbma, - buf[0], sizeof buf[0]) - : "-", - sockunion2str(reg ? ®->proto_addr : &n->proto_addr, - buf[1], sizeof buf[1])); + vty_out(vty, "%-8s %-24s %-16s %-16s\n", n->ifp->name, n->nbma_fqdn, + (reg && reg->peer) ? sockunion2str(®->peer->vc->remote.nbma, + buf[0], sizeof buf[0]) + : "-", + sockunion2str(reg ? ®->proto_addr : &n->proto_addr, buf[1], + sizeof buf[1])); } static void show_ip_nhrp_shortcut(struct nhrp_shortcut *s, void *pctx) @@ -652,17 +648,13 @@ static void show_ip_nhrp_shortcut(struct nhrp_shortcut *s, void *pctx) char buf1[PREFIX_STRLEN], buf2[SU_ADDRSTRLEN]; if (!ctx->count) { - vty_out (vty, "%-8s %-24s %-24s %s\n", - "Type", - "Prefix", - "Via", + vty_out(vty, "%-8s %-24s %-24s %s\n", "Type", "Prefix", "Via", "Identity"); } ctx->count++; c = s->cache; - vty_out(ctx->vty, "%-8s %-24s %-24s %s\n", - nhrp_cache_type_str[s->type], + vty_out(ctx->vty, "%-8s %-24s %-24s %s\n", nhrp_cache_type_str[s->type], prefix2str(s->p, buf1, sizeof buf1), c ? sockunion2str(&c->remote_addr, buf2, sizeof buf2) : "", (c && c->cur.peer) ? c->cur.peer->vc->remote.id : ""); @@ -677,26 +669,25 @@ static void show_ip_opennhrp_cache(struct nhrp_cache *c, void *pctx) return; vty_out(ctx->vty, - "Type: %s\n" - "Flags:%s%s\n" - "Protocol-Address: %s/%zu\n", - nhrp_cache_type_str[c->cur.type], - (c->cur.peer && c->cur.peer->online) ? " up": "", - c->used ? " used": "", - sockunion2str(&c->remote_addr, buf, sizeof buf), - 8 * family2addrsize(sockunion_family(&c->remote_addr))); + "Type: %s\n" + "Flags:%s%s\n" + "Protocol-Address: %s/%zu\n", + nhrp_cache_type_str[c->cur.type], + (c->cur.peer && c->cur.peer->online) ? " up" : "", + c->used ? " used" : "", + sockunion2str(&c->remote_addr, buf, sizeof buf), + 8 * family2addrsize(sockunion_family(&c->remote_addr))); if (c->cur.peer) { - vty_out(ctx->vty, - "NBMA-Address: %s\n", - sockunion2str(&c->cur.peer->vc->remote.nbma, - buf, sizeof buf)); + vty_out(ctx->vty, "NBMA-Address: %s\n", + sockunion2str(&c->cur.peer->vc->remote.nbma, buf, + sizeof buf)); } if (sockunion_family(&c->cur.remote_nbma_natoa) != AF_UNSPEC) { - vty_out(ctx->vty, - "NBMA-NAT-OA-Address: %s\n", - sockunion2str(&c->cur.remote_nbma_natoa, buf, sizeof buf)); + vty_out(ctx->vty, "NBMA-NAT-OA-Address: %s\n", + sockunion2str(&c->cur.remote_nbma_natoa, buf, + sizeof buf)); } vty_out(ctx->vty, "\n\n"); @@ -715,8 +706,7 @@ DEFUN(show_ip_nhrp, show_ip_nhrp_cmd, struct vrf *vrf = vrf_lookup_by_id(VRF_DEFAULT); struct interface *ifp; struct info_ctx ctx = { - .vty = vty, - .afi = cmd_to_afi(argv[1]), + .vty = vty, .afi = cmd_to_afi(argv[1]), }; if (argc <= 3 || argv[3]->text[0] == 'c') { @@ -728,14 +718,14 @@ DEFUN(show_ip_nhrp, show_ip_nhrp_cmd, } else if (argv[3]->text[0] == 's') { nhrp_shortcut_foreach(ctx.afi, show_ip_nhrp_shortcut, &ctx); } else { - vty_out (vty, "Status: ok\n\n"); + vty_out(vty, "Status: ok\n\n"); ctx.count++; FOR_ALL_INTERFACES (vrf, ifp) nhrp_cache_foreach(ifp, show_ip_opennhrp_cache, &ctx); } if (!ctx.count) { - vty_out (vty, "%% No entries\n"); + vty_out(vty, "%% No entries\n"); return CMD_WARNING; } @@ -747,11 +737,10 @@ static void show_dmvpn_entry(struct nhrp_vc *vc, void *ctx) struct vty *vty = ctx; char buf[2][SU_ADDRSTRLEN]; - vty_out (vty, "%-24s %-24s %c %-4d %-24s\n", + vty_out(vty, "%-24s %-24s %c %-4d %-24s\n", sockunion2str(&vc->local.nbma, buf[0], sizeof buf[0]), sockunion2str(&vc->remote.nbma, buf[1], sizeof buf[1]), - notifier_active(&vc->notifier_list) ? 'n' : ' ', - vc->ipsec, + notifier_active(&vc->notifier_list) ? 'n' : ' ', vc->ipsec, vc->remote.id); } @@ -760,12 +749,8 @@ DEFUN(show_dmvpn, show_dmvpn_cmd, SHOW_STR "DMVPN information\n") { - vty_out (vty, "%-24s %-24s %-6s %-4s %-24s\n", - "Src", - "Dst", - "Flags", - "SAs", - "Identity"); + vty_out(vty, "%-24s %-24s %-6s %-4s %-24s\n", "Src", "Dst", "Flags", + "SAs", "Identity"); nhrp_vc_foreach(show_dmvpn_entry, vty); @@ -799,9 +784,7 @@ DEFUN(clear_nhrp, clear_nhrp_cmd, struct vrf *vrf = vrf_lookup_by_id(VRF_DEFAULT); struct interface *ifp; struct info_ctx ctx = { - .vty = vty, - .afi = cmd_to_afi(argv[1]), - .count = 0, + .vty = vty, .afi = cmd_to_afi(argv[1]), .count = 0, }; if (argc <= 3 || argv[3]->text[0] == 'c') { @@ -812,11 +795,11 @@ DEFUN(clear_nhrp, clear_nhrp_cmd, } if (!ctx.count) { - vty_out (vty, "%% No entries\n"); + vty_out(vty, "%% No entries\n"); return CMD_WARNING; } - vty_out (vty, "%% %d entries cleared\n", ctx.count); + vty_out(vty, "%% %d entries cleared\n", ctx.count); return CMD_SUCCESS; } @@ -832,13 +815,17 @@ static void interface_config_write_nhrp_map(struct nhrp_cache *c, void *data) struct vty *vty = ctx->vty; char buf[2][SU_ADDRSTRLEN]; - if (!c->map) return; - if (sockunion_family(&c->remote_addr) != ctx->family) return; + if (!c->map) + return; + if (sockunion_family(&c->remote_addr) != ctx->family) + return; - vty_out (vty, " %s nhrp map %s %s\n", - ctx->aficmd, + vty_out(vty, " %s nhrp map %s %s\n", ctx->aficmd, sockunion2str(&c->remote_addr, buf[0], sizeof buf[0]), - c->cur.type == NHRP_CACHE_LOCAL ? "local" : sockunion2str(&c->cur.peer->vc->remote.nbma, buf[1], sizeof buf[1])); + c->cur.type == NHRP_CACHE_LOCAL + ? "local" + : sockunion2str(&c->cur.peer->vc->remote.nbma, buf[1], + sizeof buf[1])); } static int interface_config_write(struct vty *vty) @@ -856,7 +843,7 @@ static int interface_config_write(struct vty *vty) FOR_ALL_INTERFACES (vrf, ifp) { vty_frame(vty, "interface %s\n", ifp->name); if (ifp->desc) - vty_out (vty, " description %s\n", ifp->desc); + vty_out(vty, " description %s\n", ifp->desc); nifp = ifp->info; if (nifp->ipsec_profile) { @@ -865,11 +852,10 @@ static int interface_config_write(struct vty *vty) if (nifp->ipsec_fallback_profile) vty_out(vty, " fallback-profile %s", nifp->ipsec_fallback_profile); - vty_out (vty, "\n"); + vty_out(vty, "\n"); } if (nifp->source) - vty_out (vty, " tunnel source %s\n", - nifp->source); + vty_out(vty, " tunnel source %s\n", nifp->source); for (afi = 0; afi < AFI_MAX; afi++) { struct nhrp_afi_data *ad = &nifp->afi[afi]; @@ -877,38 +863,45 @@ static int interface_config_write(struct vty *vty) aficmd = afi_to_cmd(afi); if (ad->network_id) - vty_out (vty, " %s nhrp network-id %u\n", - aficmd,ad->network_id); + vty_out(vty, " %s nhrp network-id %u\n", aficmd, + ad->network_id); if (ad->holdtime != NHRPD_DEFAULT_HOLDTIME) - vty_out (vty, " %s nhrp holdtime %u\n", - aficmd,ad->holdtime); + vty_out(vty, " %s nhrp holdtime %u\n", aficmd, + ad->holdtime); if (ad->configured_mtu < 0) - vty_out (vty, " %s nhrp mtu opennhrp\n", - aficmd); + vty_out(vty, " %s nhrp mtu opennhrp\n", aficmd); else if (ad->configured_mtu) - vty_out (vty, " %s nhrp mtu %u\n", - aficmd,ad->configured_mtu); + vty_out(vty, " %s nhrp mtu %u\n", aficmd, + ad->configured_mtu); for (i = 0; interface_flags_desc[i].str != NULL; i++) { if (!(ad->flags & interface_flags_desc[i].key)) continue; - vty_out (vty, " %s nhrp %s\n", - aficmd, interface_flags_desc[i].str); + vty_out(vty, " %s nhrp %s\n", aficmd, + interface_flags_desc[i].str); } - mapctx = (struct write_map_ctx) { + mapctx = (struct write_map_ctx){ .vty = vty, .family = afi2family(afi), .aficmd = aficmd, }; - nhrp_cache_foreach(ifp, interface_config_write_nhrp_map, &mapctx); + nhrp_cache_foreach(ifp, interface_config_write_nhrp_map, + &mapctx); - list_for_each_entry(nhs, &ad->nhslist_head, nhslist_entry) { - vty_out (vty, " %s nhrp nhs %s nbma %s\n", + list_for_each_entry(nhs, &ad->nhslist_head, + nhslist_entry) + { + vty_out(vty, " %s nhrp nhs %s nbma %s\n", aficmd, - sockunion_family(&nhs->proto_addr) == AF_UNSPEC ? "dynamic" : sockunion2str(&nhs->proto_addr, buf, sizeof buf), + sockunion_family(&nhs->proto_addr) + == AF_UNSPEC + ? "dynamic" + : sockunion2str( + &nhs->proto_addr, buf, + sizeof buf), nhs->nbma_fqdn); } } @@ -925,7 +918,7 @@ void nhrp_config_init(void) install_default(ZEBRA_NODE); /* access-list commands */ - access_list_init (); + access_list_init(); /* global commands */ install_element(VIEW_NODE, &show_debugging_nhrp_cmd); diff --git a/nhrpd/nhrpd.h b/nhrpd/nhrpd.h index 2ab40a4d3..8f1c63457 100644 --- a/nhrpd/nhrpd.h +++ b/nhrpd/nhrpd.h @@ -26,17 +26,15 @@ DECLARE_MGROUP(NHRPD) extern struct thread_master *master; -enum { - NHRP_OK = 0, - NHRP_ERR_FAIL, - NHRP_ERR_NO_MEMORY, - NHRP_ERR_UNSUPPORTED_INTERFACE, - NHRP_ERR_NHRP_NOT_ENABLED, - NHRP_ERR_ENTRY_EXISTS, - NHRP_ERR_ENTRY_NOT_FOUND, - NHRP_ERR_PROTOCOL_ADDRESS_MISMATCH, - __NHRP_ERR_MAX -}; +enum { NHRP_OK = 0, + NHRP_ERR_FAIL, + NHRP_ERR_NO_MEMORY, + NHRP_ERR_UNSUPPORTED_INTERFACE, + NHRP_ERR_NHRP_NOT_ENABLED, + NHRP_ERR_ENTRY_EXISTS, + NHRP_ERR_ENTRY_NOT_FOUND, + NHRP_ERR_PROTOCOL_ADDRESS_MISMATCH, + __NHRP_ERR_MAX }; #define NHRP_ERR_MAX (__NHRP_ERR_MAX - 1) struct notifier_block; @@ -52,15 +50,18 @@ struct notifier_list { struct list_head notifier_head; }; -#define NOTIFIER_LIST_INITIALIZER(l) \ - { .notifier_head = LIST_INITIALIZER((l)->notifier_head) } +#define NOTIFIER_LIST_INITIALIZER(l) \ + { \ + .notifier_head = LIST_INITIALIZER((l)->notifier_head) \ + } static inline void notifier_init(struct notifier_list *l) { list_init(&l->notifier_head); } -static inline void notifier_add(struct notifier_block *n, struct notifier_list *l, notifier_fn_t action) +static inline void notifier_add(struct notifier_block *n, + struct notifier_list *l, notifier_fn_t action) { n->action = action; list_add_tail(&n->notifier_entry, &l->notifier_head); @@ -88,7 +89,9 @@ struct resolver_query { }; void resolver_init(void); -void resolver_resolve(struct resolver_query *query, int af, const char *hostname, void (*cb)(struct resolver_query *, int, union sockunion *)); +void resolver_resolve(struct resolver_query *query, int af, + const char *hostname, void (*cb)(struct resolver_query *, + int, union sockunion *)); void nhrp_zebra_init(void); void nhrp_zebra_terminate(void); @@ -198,7 +201,7 @@ enum nhrp_cache_type { NHRP_CACHE_NUM_TYPES }; -extern const char * const nhrp_cache_type_str[]; +extern const char *const nhrp_cache_type_str[]; extern unsigned long nhrp_cache_counts[NHRP_CACHE_NUM_TYPES]; struct nhrp_cache { @@ -249,7 +252,7 @@ struct nhrp_nhs { unsigned hub : 1; afi_t afi; union sockunion proto_addr; - const char *nbma_fqdn; /* IP-address or FQDN */ + const char *nbma_fqdn; /* IP-address or FQDN */ struct thread *t_resolve; struct resolver_query dns_resolve; @@ -311,107 +314,141 @@ void nhrp_interface_init(void); void nhrp_interface_update(struct interface *ifp); void nhrp_interface_update_mtu(struct interface *ifp, afi_t afi); -int nhrp_interface_add(int cmd, struct zclient *client, zebra_size_t length, vrf_id_t vrf_id); -int nhrp_interface_delete(int cmd, struct zclient *client, zebra_size_t length, vrf_id_t vrf_id); -int nhrp_interface_up(int cmd, struct zclient *client, zebra_size_t length, vrf_id_t vrf_id); -int nhrp_interface_down(int cmd, struct zclient *client, zebra_size_t length, vrf_id_t vrf_id); -int nhrp_interface_address_add(int cmd, struct zclient *client, zebra_size_t length, vrf_id_t vrf_id); -int nhrp_interface_address_delete(int cmd, struct zclient *client, zebra_size_t length, vrf_id_t vrf_id); - -void nhrp_interface_notify_add(struct interface *ifp, struct notifier_block *n, notifier_fn_t fn); +int nhrp_interface_add(int cmd, struct zclient *client, zebra_size_t length, + vrf_id_t vrf_id); +int nhrp_interface_delete(int cmd, struct zclient *client, zebra_size_t length, + vrf_id_t vrf_id); +int nhrp_interface_up(int cmd, struct zclient *client, zebra_size_t length, + vrf_id_t vrf_id); +int nhrp_interface_down(int cmd, struct zclient *client, zebra_size_t length, + vrf_id_t vrf_id); +int nhrp_interface_address_add(int cmd, struct zclient *client, + zebra_size_t length, vrf_id_t vrf_id); +int nhrp_interface_address_delete(int cmd, struct zclient *client, + zebra_size_t length, vrf_id_t vrf_id); + +void nhrp_interface_notify_add(struct interface *ifp, struct notifier_block *n, + notifier_fn_t fn); void nhrp_interface_notify_del(struct interface *ifp, struct notifier_block *n); -void nhrp_interface_set_protection(struct interface *ifp, const char *profile, const char *fallback_profile); +void nhrp_interface_set_protection(struct interface *ifp, const char *profile, + const char *fallback_profile); void nhrp_interface_set_source(struct interface *ifp, const char *ifname); -int nhrp_nhs_add(struct interface *ifp, afi_t afi, union sockunion *proto_addr, const char *nbma_fqdn); -int nhrp_nhs_del(struct interface *ifp, afi_t afi, union sockunion *proto_addr, const char *nbma_fqdn); +int nhrp_nhs_add(struct interface *ifp, afi_t afi, union sockunion *proto_addr, + const char *nbma_fqdn); +int nhrp_nhs_del(struct interface *ifp, afi_t afi, union sockunion *proto_addr, + const char *nbma_fqdn); int nhrp_nhs_free(struct nhrp_nhs *nhs); void nhrp_nhs_terminate(void); -void nhrp_nhs_foreach(struct interface *ifp, afi_t afi, void (*cb)(struct nhrp_nhs *, struct nhrp_registration *, void *), void *ctx); +void nhrp_nhs_foreach(struct interface *ifp, afi_t afi, + void (*cb)(struct nhrp_nhs *, struct nhrp_registration *, + void *), + void *ctx); void nhrp_route_update_nhrp(const struct prefix *p, struct interface *ifp); -void nhrp_route_announce(int add, enum nhrp_cache_type type, const struct prefix *p, struct interface *ifp, const union sockunion *nexthop, uint32_t mtu); -int nhrp_route_read(int command, struct zclient *zclient, zebra_size_t length, vrf_id_t vrf_id); -int nhrp_route_get_nexthop(const union sockunion *addr, struct prefix *p, union sockunion *via, struct interface **ifp); -enum nhrp_route_type nhrp_route_address(struct interface *in_ifp, union sockunion *addr, struct prefix *p, struct nhrp_peer **peer); +void nhrp_route_announce(int add, enum nhrp_cache_type type, + const struct prefix *p, struct interface *ifp, + const union sockunion *nexthop, uint32_t mtu); +int nhrp_route_read(int command, struct zclient *zclient, zebra_size_t length, + vrf_id_t vrf_id); +int nhrp_route_get_nexthop(const union sockunion *addr, struct prefix *p, + union sockunion *via, struct interface **ifp); +enum nhrp_route_type nhrp_route_address(struct interface *in_ifp, + union sockunion *addr, struct prefix *p, + struct nhrp_peer **peer); void nhrp_config_init(void); void nhrp_shortcut_init(void); void nhrp_shortcut_terminate(void); void nhrp_shortcut_initiate(union sockunion *addr); -void nhrp_shortcut_foreach(afi_t afi, void (*cb)(struct nhrp_shortcut *, void *), void *ctx); +void nhrp_shortcut_foreach(afi_t afi, + void (*cb)(struct nhrp_shortcut *, void *), + void *ctx); void nhrp_shortcut_purge(struct nhrp_shortcut *s, int force); void nhrp_shortcut_prefix_change(const struct prefix *p, int deleted); -struct nhrp_cache *nhrp_cache_get(struct interface *ifp, union sockunion *remote_addr, int create); -void nhrp_cache_foreach(struct interface *ifp, void (*cb)(struct nhrp_cache *, void *), void *ctx); +struct nhrp_cache *nhrp_cache_get(struct interface *ifp, + union sockunion *remote_addr, int create); +void nhrp_cache_foreach(struct interface *ifp, + void (*cb)(struct nhrp_cache *, void *), void *ctx); void nhrp_cache_set_used(struct nhrp_cache *, int); -int nhrp_cache_update_binding(struct nhrp_cache *, enum nhrp_cache_type type, int holding_time, struct nhrp_peer *p, uint32_t mtu, union sockunion *nbma_natoa); -void nhrp_cache_notify_add(struct nhrp_cache *c, struct notifier_block *, notifier_fn_t); +int nhrp_cache_update_binding(struct nhrp_cache *, enum nhrp_cache_type type, + int holding_time, struct nhrp_peer *p, + uint32_t mtu, union sockunion *nbma_natoa); +void nhrp_cache_notify_add(struct nhrp_cache *c, struct notifier_block *, + notifier_fn_t); void nhrp_cache_notify_del(struct nhrp_cache *c, struct notifier_block *); void nhrp_vc_init(void); void nhrp_vc_terminate(void); -struct nhrp_vc *nhrp_vc_get(const union sockunion *src, const union sockunion *dst, int create); +struct nhrp_vc *nhrp_vc_get(const union sockunion *src, + const union sockunion *dst, int create); int nhrp_vc_ipsec_updown(uint32_t child_id, struct nhrp_vc *vc); -void nhrp_vc_notify_add(struct nhrp_vc *, struct notifier_block *, notifier_fn_t); +void nhrp_vc_notify_add(struct nhrp_vc *, struct notifier_block *, + notifier_fn_t); void nhrp_vc_notify_del(struct nhrp_vc *, struct notifier_block *); void nhrp_vc_foreach(void (*cb)(struct nhrp_vc *, void *), void *ctx); void nhrp_vc_reset(void); void vici_init(void); void vici_terminate(void); -void vici_request_vc(const char *profile, union sockunion *src, union sockunion *dst, int prio); +void vici_request_vc(const char *profile, union sockunion *src, + union sockunion *dst, int prio); extern const char *nhrp_event_socket_path; void evmgr_init(void); void evmgr_terminate(void); void evmgr_set_socket(const char *socket); -void evmgr_notify(const char *name, struct nhrp_cache *c, void (*cb)(struct nhrp_reqid *, void *)); +void evmgr_notify(const char *name, struct nhrp_cache *c, + void (*cb)(struct nhrp_reqid *, void *)); -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); void nhrp_packet_complete(struct zbuf *zb, struct nhrp_packet_header *hdr); uint16_t nhrp_packet_calculate_checksum(const uint8_t *pdu, uint16_t len); -struct nhrp_packet_header *nhrp_packet_pull( - struct zbuf *zb, - union sockunion *src_nbma, - union sockunion *src_proto, - union sockunion *dst_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 *nhrp_cie_pull( - struct zbuf *zb, - struct nhrp_packet_header *hdr, - union sockunion *nbma, - union sockunion *proto); - -struct nhrp_extension_header *nhrp_ext_push(struct zbuf *zb, struct nhrp_packet_header *hdr, uint16_t type); +struct nhrp_packet_header *nhrp_packet_pull(struct zbuf *zb, + union sockunion *src_nbma, + union sockunion *src_proto, + union sockunion *dst_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 *nhrp_cie_pull(struct zbuf *zb, + struct nhrp_packet_header *hdr, + union sockunion *nbma, + union sockunion *proto); + +struct nhrp_extension_header * +nhrp_ext_push(struct zbuf *zb, struct nhrp_packet_header *hdr, uint16_t type); void nhrp_ext_complete(struct zbuf *zb, struct nhrp_extension_header *ext); -struct nhrp_extension_header *nhrp_ext_pull(struct zbuf *zb, struct zbuf *payload); -void nhrp_ext_request(struct zbuf *zb, struct nhrp_packet_header *hdr, struct interface *); -int nhrp_ext_reply(struct zbuf *zb, struct nhrp_packet_header *hdr, struct interface *ifp, struct nhrp_extension_header *ext, struct zbuf *extpayload); - -uint32_t nhrp_reqid_alloc(struct nhrp_reqid_pool *, struct nhrp_reqid *r, void (*cb)(struct nhrp_reqid *, void *)); +struct nhrp_extension_header *nhrp_ext_pull(struct zbuf *zb, + struct zbuf *payload); +void nhrp_ext_request(struct zbuf *zb, struct nhrp_packet_header *hdr, + struct interface *); +int nhrp_ext_reply(struct zbuf *zb, struct nhrp_packet_header *hdr, + struct interface *ifp, struct nhrp_extension_header *ext, + struct zbuf *extpayload); + +uint32_t nhrp_reqid_alloc(struct nhrp_reqid_pool *, struct nhrp_reqid *r, + void (*cb)(struct nhrp_reqid *, void *)); void nhrp_reqid_free(struct nhrp_reqid_pool *, struct nhrp_reqid *r); struct nhrp_reqid *nhrp_reqid_lookup(struct nhrp_reqid_pool *, uint32_t reqid); int nhrp_packet_init(void); -struct nhrp_peer *nhrp_peer_get(struct interface *ifp, const union sockunion *remote_nbma); +struct nhrp_peer *nhrp_peer_get(struct interface *ifp, + const union sockunion *remote_nbma); struct nhrp_peer *nhrp_peer_ref(struct nhrp_peer *p); void nhrp_peer_unref(struct nhrp_peer *p); int nhrp_peer_check(struct nhrp_peer *p, int establish); -void nhrp_peer_notify_add(struct nhrp_peer *p, struct notifier_block *, notifier_fn_t); +void nhrp_peer_notify_add(struct nhrp_peer *p, struct notifier_block *, + notifier_fn_t); void nhrp_peer_notify_del(struct nhrp_peer *p, struct notifier_block *); void nhrp_peer_recv(struct nhrp_peer *p, struct zbuf *zb); void nhrp_peer_send(struct nhrp_peer *p, struct zbuf *zb); diff --git a/nhrpd/os.h b/nhrpd/os.h index 0fbe8b003..dd65d3cbe 100644 --- a/nhrpd/os.h +++ b/nhrpd/os.h @@ -1,5 +1,7 @@ int os_socket(void); -int os_sendmsg(const uint8_t *buf, size_t len, int ifindex, const uint8_t *addr, size_t addrlen); -int os_recvmsg(uint8_t *buf, size_t *len, int *ifindex, uint8_t *addr, size_t *addrlen); +int os_sendmsg(const uint8_t *buf, size_t len, int ifindex, const uint8_t *addr, + size_t addrlen); +int os_recvmsg(uint8_t *buf, size_t *len, int *ifindex, uint8_t *addr, + size_t *addrlen); int os_configure_dmvpn(unsigned int ifindex, const char *ifname, int af); diff --git a/nhrpd/reqid.c b/nhrpd/reqid.c index e5bd45a8f..e8ad518e5 100644 --- a/nhrpd/reqid.c +++ b/nhrpd/reqid.c @@ -14,18 +14,19 @@ static int nhrp_reqid_cmp(const void *data, const void *key) return a->request_id == b->request_id; } -uint32_t nhrp_reqid_alloc(struct nhrp_reqid_pool *p, struct nhrp_reqid *r, void (*cb)(struct nhrp_reqid *, void *)) +uint32_t nhrp_reqid_alloc(struct nhrp_reqid_pool *p, struct nhrp_reqid *r, + void (*cb)(struct nhrp_reqid *, void *)) { if (!p->reqid_hash) { - p->reqid_hash = hash_create(nhrp_reqid_key, - nhrp_reqid_cmp, + p->reqid_hash = hash_create(nhrp_reqid_key, nhrp_reqid_cmp, "NHRP reqid Hash"); p->next_request_id = 1; } if (r->cb != cb) { r->request_id = p->next_request_id; - if (++p->next_request_id == 0) p->next_request_id = 1; + if (++p->next_request_id == 0) + p->next_request_id = 1; r->cb = cb; hash_get(p->reqid_hash, r, hash_alloc_intern); } @@ -43,9 +44,8 @@ void nhrp_reqid_free(struct nhrp_reqid_pool *p, struct nhrp_reqid *r) struct nhrp_reqid *nhrp_reqid_lookup(struct nhrp_reqid_pool *p, uint32_t reqid) { struct nhrp_reqid key; - if (!p->reqid_hash) return 0; + if (!p->reqid_hash) + return 0; key.request_id = reqid; return hash_lookup(p->reqid_hash, &key); } - - diff --git a/nhrpd/resolver.c b/nhrpd/resolver.c index c29be3cbf..6349224ad 100644 --- a/nhrpd/resolver.c +++ b/nhrpd/resolver.c @@ -47,8 +47,7 @@ static int resolver_cb_socket_readable(struct thread *t) ares_process_fd(r->channel, fd, ARES_SOCKET_BAD); if (vector_lookup(r->read_threads, fd) == THREAD_RUNNING) { t = NULL; - thread_add_read(master, resolver_cb_socket_readable, r, fd, - &t); + thread_add_read(master, resolver_cb_socket_readable, r, fd, &t); vector_set_index(r->read_threads, fd, t); } resolver_update_timeouts(r); @@ -78,27 +77,29 @@ static void resolver_update_timeouts(struct resolver_state *r) { struct timeval *tv, tvbuf; - if (r->timeout == THREAD_RUNNING) return; + if (r->timeout == THREAD_RUNNING) + return; THREAD_OFF(r->timeout); tv = ares_timeout(r->channel, NULL, &tvbuf); if (tv) { unsigned int timeoutms = tv->tv_sec * 1000 + tv->tv_usec / 1000; - thread_add_timer_msec(master, resolver_cb_timeout, r, - timeoutms, &r->timeout); + thread_add_timer_msec(master, resolver_cb_timeout, r, timeoutms, + &r->timeout); } } -static void ares_socket_cb(void *data, ares_socket_t fd, int readable, int writable) +static void ares_socket_cb(void *data, ares_socket_t fd, int readable, + int writable) { - struct resolver_state *r = (struct resolver_state *) data; + struct resolver_state *r = (struct resolver_state *)data; struct thread *t; if (readable) { t = vector_lookup_ensure(r->read_threads, fd); if (!t) { - thread_add_read(master, resolver_cb_socket_readable, - r, fd, &t); + thread_add_read(master, resolver_cb_socket_readable, r, + fd, &t); vector_set_index(r->read_threads, fd, t); } } else { @@ -114,8 +115,8 @@ static void ares_socket_cb(void *data, ares_socket_t fd, int readable, int writa if (writable) { t = vector_lookup_ensure(r->write_threads, fd); if (!t) { - thread_add_read(master, resolver_cb_socket_writable, - r, fd, &t); + thread_add_read(master, resolver_cb_socket_writable, r, + fd, &t); vector_set_index(r->write_threads, fd, t); } } else { @@ -136,7 +137,7 @@ void resolver_init(void) state.read_threads = vector_init(1); state.write_threads = vector_init(1); - ares_opts = (struct ares_options) { + ares_opts = (struct ares_options){ .sock_state_cb = &ares_socket_cb, .sock_state_cb_data = &state, .timeout = 2, @@ -144,14 +145,15 @@ void resolver_init(void) }; ares_init_options(&state.channel, &ares_opts, - ARES_OPT_SOCK_STATE_CB | ARES_OPT_TIMEOUT | - ARES_OPT_TRIES); + ARES_OPT_SOCK_STATE_CB | ARES_OPT_TIMEOUT + | ARES_OPT_TRIES); } -static void ares_address_cb(void *arg, int status, int timeouts, struct hostent *he) +static void ares_address_cb(void *arg, int status, int timeouts, + struct hostent *he) { - struct resolver_query *query = (struct resolver_query *) arg; + struct resolver_query *query = (struct resolver_query *)arg; union sockunion addr[16]; size_t i; @@ -167,23 +169,31 @@ static void ares_address_cb(void *arg, int status, int timeouts, struct hostent addr[i].sa.sa_family = he->h_addrtype; switch (he->h_addrtype) { case AF_INET: - memcpy(&addr[i].sin.sin_addr, (uint8_t *) he->h_addr_list[i], he->h_length); + memcpy(&addr[i].sin.sin_addr, + (uint8_t *)he->h_addr_list[i], he->h_length); break; case AF_INET6: - memcpy(&addr[i].sin6.sin6_addr, (uint8_t *) he->h_addr_list[i], he->h_length); + memcpy(&addr[i].sin6.sin6_addr, + (uint8_t *)he->h_addr_list[i], he->h_length); break; } } - debugf(NHRP_DEBUG_COMMON, "[%p] Resolved with %d results", query, (int) i); + debugf(NHRP_DEBUG_COMMON, "[%p] Resolved with %d results", query, + (int)i); query->callback(query, i, &addr[0]); query->callback = NULL; } -void resolver_resolve(struct resolver_query *query, int af, const char *hostname, void (*callback)(struct resolver_query *, int, union sockunion *)) +void resolver_resolve(struct resolver_query *query, int af, + const char *hostname, + void (*callback)(struct resolver_query *, int, + union sockunion *)) { if (query->callback != NULL) { - zlog_err("Trying to resolve '%s', but previous query was not finished yet", hostname); + zlog_err( + "Trying to resolve '%s', but previous query was not finished yet", + hostname); return; } diff --git a/nhrpd/vici.c b/nhrpd/vici.c index fd01a4534..e6111f9d7 100644 --- a/nhrpd/vici.c +++ b/nhrpd/vici.c @@ -27,13 +27,15 @@ struct blob { static int blob_equal(const struct blob *b, const char *str) { - if (!b || b->len != (int) strlen(str)) return 0; + if (!b || b->len != (int)strlen(str)) + return 0; return memcmp(b->ptr, str, b->len) == 0; } static int blob2buf(const struct blob *b, char *buf, size_t n) { - if (!b || b->len >= (int) n) return 0; + if (!b || b->len >= (int)n) + return 0; memcpy(buf, b->ptr, b->len); buf[b->len] = 0; return 1; @@ -76,21 +78,24 @@ static void vici_connection_error(struct vici_conn *vici) thread_add_timer(master, vici_reconnect, vici, 2, &vici->t_reconnect); } -static void vici_parse_message( - struct vici_conn *vici, struct zbuf *msg, - void (*parser)(struct vici_message_ctx *ctx, enum vici_type_t msgtype, const struct blob *key, const struct blob *val), - struct vici_message_ctx *ctx) +static void vici_parse_message(struct vici_conn *vici, struct zbuf *msg, + void (*parser)(struct vici_message_ctx *ctx, + enum vici_type_t msgtype, + const struct blob *key, + const struct blob *val), + struct vici_message_ctx *ctx) { uint8_t *type; - struct blob key = { 0 }; - struct blob val = { 0 }; + struct blob key = {0}; + struct blob val = {0}; while ((type = zbuf_may_pull(msg, uint8_t)) != NULL) { switch (*type) { case VICI_SECTION_START: key.len = zbuf_get8(msg); key.ptr = zbuf_pulln(msg, key.len); - debugf(NHRP_DEBUG_VICI, "VICI: Section start '%.*s'", key.len, key.ptr); + debugf(NHRP_DEBUG_VICI, "VICI: Section start '%.*s'", + key.len, key.ptr); parser(ctx, *type, &key, NULL); ctx->nsections++; break; @@ -104,25 +109,30 @@ static void vici_parse_message( key.ptr = zbuf_pulln(msg, key.len); val.len = zbuf_get_be16(msg); val.ptr = zbuf_pulln(msg, val.len); - debugf(NHRP_DEBUG_VICI, "VICI: Key '%.*s'='%.*s'", key.len, key.ptr, val.len, val.ptr); + debugf(NHRP_DEBUG_VICI, "VICI: Key '%.*s'='%.*s'", + key.len, key.ptr, val.len, val.ptr); parser(ctx, *type, &key, &val); break; case VICI_LIST_START: key.len = zbuf_get8(msg); key.ptr = zbuf_pulln(msg, key.len); - debugf(NHRP_DEBUG_VICI, "VICI: List start '%.*s'", key.len, key.ptr); + debugf(NHRP_DEBUG_VICI, "VICI: List start '%.*s'", + key.len, key.ptr); break; case VICI_LIST_ITEM: val.len = zbuf_get_be16(msg); val.ptr = zbuf_pulln(msg, val.len); - debugf(NHRP_DEBUG_VICI, "VICI: List item: '%.*s'", val.len, val.ptr); + debugf(NHRP_DEBUG_VICI, "VICI: List item: '%.*s'", + val.len, val.ptr); parser(ctx, *type, &key, &val); break; case VICI_LIST_END: debugf(NHRP_DEBUG_VICI, "VICI: List end"); break; default: - debugf(NHRP_DEBUG_VICI, "VICI: Unsupported message component type %d", *type); + debugf(NHRP_DEBUG_VICI, + "VICI: Unsupported message component type %d", + *type); return; } } @@ -140,12 +150,12 @@ struct handle_sa_ctx { } local, remote; }; -static void parse_sa_message( - struct vici_message_ctx *ctx, - enum vici_type_t msgtype, - const struct blob *key, const struct blob *val) +static void parse_sa_message(struct vici_message_ctx *ctx, + enum vici_type_t msgtype, const struct blob *key, + const struct blob *val) { - struct handle_sa_ctx *sactx = container_of(ctx, struct handle_sa_ctx, msgctx); + struct handle_sa_ctx *sactx = + container_of(ctx, struct handle_sa_ctx, msgctx); struct nhrp_vc *vc; char buf[512]; @@ -162,15 +172,28 @@ static void parse_sa_message( /* End of child-sa section, update nhrp_vc */ int up = sactx->child_ok || sactx->event == 1; if (up) { - vc = nhrp_vc_get(&sactx->local.host, &sactx->remote.host, up); + vc = nhrp_vc_get(&sactx->local.host, + &sactx->remote.host, up); if (vc) { - blob2buf(&sactx->local.id, vc->local.id, sizeof(vc->local.id)); - if (blob2buf(&sactx->local.cert, (char*)vc->local.cert, sizeof(vc->local.cert))) - vc->local.certlen = sactx->local.cert.len; - blob2buf(&sactx->remote.id, vc->remote.id, sizeof(vc->remote.id)); - if (blob2buf(&sactx->remote.cert, (char*)vc->remote.cert, sizeof(vc->remote.cert))) - vc->remote.certlen = sactx->remote.cert.len; - sactx->kill_ikesa |= nhrp_vc_ipsec_updown(sactx->child_uniqueid, vc); + blob2buf(&sactx->local.id, vc->local.id, + sizeof(vc->local.id)); + if (blob2buf(&sactx->local.cert, + (char *)vc->local.cert, + sizeof(vc->local.cert))) + vc->local.certlen = + sactx->local.cert.len; + blob2buf(&sactx->remote.id, + vc->remote.id, + sizeof(vc->remote.id)); + if (blob2buf(&sactx->remote.cert, + (char *)vc->remote.cert, + sizeof(vc->remote.cert))) + vc->remote.certlen = + sactx->remote.cert.len; + sactx->kill_ikesa |= + nhrp_vc_ipsec_updown( + sactx->child_uniqueid, + vc); } } else { nhrp_vc_ipsec_updown(sactx->child_uniqueid, 0); @@ -183,41 +206,58 @@ static void parse_sa_message( switch (key->ptr[0]) { case 'l': - if (blob_equal(key, "local-host") && ctx->nsections == 1) { + if (blob_equal(key, "local-host") + && ctx->nsections == 1) { if (blob2buf(val, buf, sizeof(buf))) - if (str2sockunion(buf, &sactx->local.host) < 0) - zlog_err("VICI: bad strongSwan local-host: %s", buf); - } else if (blob_equal(key, "local-id") && ctx->nsections == 1) { + if (str2sockunion(buf, + &sactx->local.host) + < 0) + zlog_err( + "VICI: bad strongSwan local-host: %s", + buf); + } else if (blob_equal(key, "local-id") + && ctx->nsections == 1) { sactx->local.id = *val; - } else if (blob_equal(key, "local-cert-data") && ctx->nsections == 1) { + } else if (blob_equal(key, "local-cert-data") + && ctx->nsections == 1) { sactx->local.cert = *val; } break; case 'r': - if (blob_equal(key, "remote-host") && ctx->nsections == 1) { + if (blob_equal(key, "remote-host") + && ctx->nsections == 1) { if (blob2buf(val, buf, sizeof(buf))) - if (str2sockunion(buf, &sactx->remote.host) < 0) - zlog_err("VICI: bad strongSwan remote-host: %s", buf); - } else if (blob_equal(key, "remote-id") && ctx->nsections == 1) { + if (str2sockunion(buf, + &sactx->remote.host) + < 0) + zlog_err( + "VICI: bad strongSwan remote-host: %s", + buf); + } else if (blob_equal(key, "remote-id") + && ctx->nsections == 1) { sactx->remote.id = *val; - } else if (blob_equal(key, "remote-cert-data") && ctx->nsections == 1) { + } else if (blob_equal(key, "remote-cert-data") + && ctx->nsections == 1) { sactx->remote.cert = *val; } break; case 'u': - if (blob_equal(key, "uniqueid") && blob2buf(val, buf, sizeof(buf))) { + if (blob_equal(key, "uniqueid") + && blob2buf(val, buf, sizeof(buf))) { if (ctx->nsections == 3) - sactx->child_uniqueid = strtoul(buf, NULL, 0); + sactx->child_uniqueid = + strtoul(buf, NULL, 0); else if (ctx->nsections == 1) - sactx->ike_uniqueid = strtoul(buf, NULL, 0); + sactx->ike_uniqueid = + strtoul(buf, NULL, 0); } break; case 's': if (blob_equal(key, "state") && ctx->nsections == 3) { sactx->child_ok = - (sactx->event == 0 && - (blob_equal(val, "INSTALLED") || - blob_equal(val, "REKEYED"))); + (sactx->event == 0 + && (blob_equal(val, "INSTALLED") + || blob_equal(val, "REKEYED"))); } break; } @@ -225,16 +265,16 @@ static void parse_sa_message( } } -static void parse_cmd_response( - struct vici_message_ctx *ctx, - enum vici_type_t msgtype, - const struct blob *key, const struct blob *val) +static void parse_cmd_response(struct vici_message_ctx *ctx, + enum vici_type_t msgtype, const struct blob *key, + const struct blob *val) { char buf[512]; switch (msgtype) { case VICI_KEY_VALUE: - if (blob_equal(key, "errmsg") && blob2buf(val, buf, sizeof(buf))) + if (blob_equal(key, "errmsg") + && blob2buf(val, buf, sizeof(buf))) zlog_err("VICI: strongSwan: %s", buf); break; default: @@ -252,12 +292,11 @@ static void vici_recv_sa(struct vici_conn *vici, struct zbuf *msg, int event) vici_parse_message(vici, msg, parse_sa_message, &ctx.msgctx); if (ctx.kill_ikesa && ctx.ike_uniqueid) { - debugf(NHRP_DEBUG_COMMON, "VICI: Deleting IKE_SA %u", ctx.ike_uniqueid); + debugf(NHRP_DEBUG_COMMON, "VICI: Deleting IKE_SA %u", + ctx.ike_uniqueid); snprintf(buf, sizeof buf, "%u", ctx.ike_uniqueid); - vici_submit_request( - vici, "terminate", - VICI_KEY_VALUE, "ike-id", strlen(buf), buf, - VICI_END); + vici_submit_request(vici, "terminate", VICI_KEY_VALUE, "ike-id", + strlen(buf), buf, VICI_END); } } @@ -277,13 +316,14 @@ static void vici_recv_message(struct vici_conn *vici, struct zbuf *msg) name.len = zbuf_get8(msg); name.ptr = zbuf_pulln(msg, name.len); - debugf(NHRP_DEBUG_VICI, "VICI: Event '%.*s'", name.len, name.ptr); - if (blob_equal(&name, "list-sa") || - blob_equal(&name, "child-updown") || - blob_equal(&name, "child-rekey")) + debugf(NHRP_DEBUG_VICI, "VICI: Event '%.*s'", name.len, + name.ptr); + if (blob_equal(&name, "list-sa") + || blob_equal(&name, "child-updown") + || blob_equal(&name, "child-rekey")) vici_recv_sa(vici, msg, 0); - else if (blob_equal(&name, "child-state-installed") || - blob_equal(&name, "child-state-rekeyed")) + else if (blob_equal(&name, "child-state-installed") + || blob_equal(&name, "child-state-rekeyed")) vici_recv_sa(vici, msg, 1); else if (blob_equal(&name, "child-state-destroying")) vici_recv_sa(vici, msg, 2); @@ -293,7 +333,8 @@ static void vici_recv_message(struct vici_conn *vici, struct zbuf *msg) break; case VICI_EVENT_UNKNOWN: case VICI_CMD_UNKNOWN: - zlog_err("VICI: StrongSwan does not support mandatory events (unpatched?)"); + zlog_err( + "VICI: StrongSwan does not support mandatory events (unpatched?)"); break; case VICI_EVENT_CONFIRM: break; @@ -310,7 +351,7 @@ static int vici_read(struct thread *t) struct zbuf pktbuf; vici->t_read = NULL; - if (zbuf_read(ibuf, vici->fd, (size_t) -1) < 0) { + if (zbuf_read(ibuf, vici->fd, (size_t)-1) < 0) { vici_connection_error(vici); return 0; } @@ -326,7 +367,8 @@ static int vici_read(struct thread *t) } /* Handle packet */ - zbuf_init(&pktbuf, hdrlen, htonl(*hdrlen)+4, htonl(*hdrlen)+4); + zbuf_init(&pktbuf, hdrlen, htonl(*hdrlen) + 4, + htonl(*hdrlen) + 4); vici_recv_message(vici, &pktbuf); } while (1); @@ -371,7 +413,8 @@ static void vici_submit_request(struct vici_conn *vici, const char *name, ...) int type; obuf = zbuf_alloc(256); - if (!obuf) return; + if (!obuf) + return; hdrlen = zbuf_push(obuf, uint32_t); zbuf_put8(obuf, VICI_CMD_REQUEST); @@ -404,7 +447,8 @@ static void vici_register_event(struct vici_conn *vici, const char *name) namelen = strlen(name); obuf = zbuf_alloc(4 + 1 + 1 + namelen); - if (!obuf) return; + if (!obuf) + return; hdrlen = zbuf_push(obuf, uint32_t); zbuf_put8(obuf, VICI_EVENT_REGISTER); @@ -421,12 +465,14 @@ static int vici_reconnect(struct thread *t) int fd; vici->t_reconnect = NULL; - if (vici->fd >= 0) return 0; + if (vici->fd >= 0) + return 0; fd = sock_open_unix("/var/run/charon.vici"); if (fd < 0) { - debugf(NHRP_DEBUG_VICI, "%s: failure connecting VICI socket: %s", - __PRETTY_FUNCTION__, strerror(errno)); + debugf(NHRP_DEBUG_VICI, + "%s: failure connecting VICI socket: %s", + __PRETTY_FUNCTION__, strerror(errno)); thread_add_timer(master, vici_reconnect, vici, 2, &vici->t_reconnect); return 0; @@ -437,8 +483,8 @@ static int vici_reconnect(struct thread *t) thread_add_read(master, vici_read, vici, vici->fd, &vici->t_read); /* Send event subscribtions */ - //vici_register_event(vici, "child-updown"); - //vici_register_event(vici, "child-rekey"); + // vici_register_event(vici, "child-updown"); + // vici_register_event(vici, "child-rekey"); vici_register_event(vici, "child-state-installed"); vici_register_event(vici, "child-state-rekeyed"); vici_register_event(vici, "child-state-destroying"); @@ -465,7 +511,8 @@ void vici_terminate(void) { } -void vici_request_vc(const char *profile, union sockunion *src, union sockunion *dst, int prio) +void vici_request_vc(const char *profile, union sockunion *src, + union sockunion *dst, int prio) { struct vici_conn *vici = &vici_connection; char buf[2][SU_ADDRSTRLEN]; @@ -473,15 +520,13 @@ void vici_request_vc(const char *profile, union sockunion *src, union sockunion sockunion2str(src, buf[0], sizeof buf[0]); sockunion2str(dst, buf[1], sizeof buf[1]); - vici_submit_request( - vici, "initiate", - VICI_KEY_VALUE, "child", strlen(profile), profile, - VICI_KEY_VALUE, "timeout", (size_t) 2, "-1", - VICI_KEY_VALUE, "async", (size_t) 1, "1", - VICI_KEY_VALUE, "init-limits", (size_t) 1, prio ? "0" : "1", - VICI_KEY_VALUE, "my-host", strlen(buf[0]), buf[0], - VICI_KEY_VALUE, "other-host", strlen(buf[1]), buf[1], - VICI_END); + vici_submit_request(vici, "initiate", VICI_KEY_VALUE, "child", + strlen(profile), profile, VICI_KEY_VALUE, "timeout", + (size_t)2, "-1", VICI_KEY_VALUE, "async", (size_t)1, + "1", VICI_KEY_VALUE, "init-limits", (size_t)1, + prio ? "0" : "1", VICI_KEY_VALUE, "my-host", + strlen(buf[0]), buf[0], VICI_KEY_VALUE, + "other-host", strlen(buf[1]), buf[1], VICI_END); } int sock_open_unix(const char *path) @@ -493,11 +538,12 @@ int sock_open_unix(const char *path) if (fd < 0) return -1; - memset(&addr, 0, sizeof (struct sockaddr_un)); + memset(&addr, 0, sizeof(struct sockaddr_un)); addr.sun_family = AF_UNIX; strncpy(addr.sun_path, path, sizeof(addr.sun_path) - 1); - ret = connect(fd, (struct sockaddr *) &addr, sizeof(addr.sun_family) + strlen(addr.sun_path)); + ret = connect(fd, (struct sockaddr *)&addr, + sizeof(addr.sun_family) + strlen(addr.sun_path)); if (ret < 0) { close(fd); return -1; diff --git a/nhrpd/vici.h b/nhrpd/vici.h index 24b900b43..f2ad3a9fe 100644 --- a/nhrpd/vici.h +++ b/nhrpd/vici.h @@ -1,13 +1,13 @@ enum vici_type_t { - VICI_START = 0, + VICI_START = 0, VICI_SECTION_START = 1, - VICI_SECTION_END = 2, - VICI_KEY_VALUE = 3, - VICI_LIST_START = 4, - VICI_LIST_ITEM = 5, - VICI_LIST_END = 6, - VICI_END = 7 + VICI_SECTION_END = 2, + VICI_KEY_VALUE = 3, + VICI_LIST_START = 4, + VICI_LIST_ITEM = 5, + VICI_LIST_END = 6, + VICI_END = 7 }; enum vici_operation_t { diff --git a/nhrpd/zbuf.c b/nhrpd/zbuf.c index 97962b9ab..65232a309 100644 --- a/nhrpd/zbuf.c +++ b/nhrpd/zbuf.c @@ -28,7 +28,7 @@ struct zbuf *zbuf_alloc(size_t size) if (!zb) return NULL; - zbuf_init(zb, zb+1, size, 0); + zbuf_init(zb, zb + 1, size, 0); zb->allocated = 1; return zb; @@ -36,7 +36,7 @@ struct zbuf *zbuf_alloc(size_t size) void zbuf_init(struct zbuf *zb, void *buf, size_t len, size_t datalen) { - *zb = (struct zbuf) { + *zb = (struct zbuf){ .buf = buf, .end = (uint8_t *)buf + len, .head = buf, @@ -58,7 +58,7 @@ void zbuf_reset(struct zbuf *zb) void zbuf_reset_head(struct zbuf *zb, void *ptr) { - zassert((void*)zb->buf <= ptr && ptr <= (void*)zb->tail); + zassert((void *)zb->buf <= ptr && ptr <= (void *)zb->tail); zb->head = ptr; } @@ -84,9 +84,12 @@ ssize_t zbuf_read(struct zbuf *zb, int fd, size_t maxlen) maxlen = zbuf_tailroom(zb); r = read(fd, zb->tail, maxlen); - if (r > 0) zb->tail += r; - else if (r == 0) r = -2; - else if (r < 0 && ERRNO_IO_RETRY(errno)) r = 0; + if (r > 0) + zb->tail += r; + else if (r == 0) + r = -2; + else if (r < 0 && ERRNO_IO_RETRY(errno)) + r = 0; return r; } @@ -103,9 +106,10 @@ ssize_t zbuf_write(struct zbuf *zb, int fd) zb->head += r; if (zb->head == zb->tail) zbuf_reset(zb); - } - else if (r == 0) r = -2; - else if (r < 0 && ERRNO_IO_RETRY(errno)) r = 0; + } else if (r == 0) + r = -2; + else if (r < 0 && ERRNO_IO_RETRY(errno)) + r = 0; return r; } @@ -119,9 +123,12 @@ ssize_t zbuf_recv(struct zbuf *zb, int fd) zbuf_remove_headroom(zb); r = recv(fd, zb->tail, zbuf_tailroom(zb), 0); - if (r > 0) zb->tail += r; - else if (r == 0) r = -2; - else if (r < 0 && ERRNO_IO_RETRY(errno)) r = 0; + if (r > 0) + zb->tail += r; + else if (r == 0) + r = -2; + else if (r < 0 && ERRNO_IO_RETRY(errno)) + r = 0; return r; } @@ -145,7 +152,8 @@ void *zbuf_may_pull_until(struct zbuf *zb, const char *sep, struct zbuf *msg) uint8_t *ptr; ptr = memmem(zb->head, zbuf_used(zb), sep, seplen); - if (!ptr) return NULL; + if (!ptr) + return NULL; len = ptr - zb->head + seplen; zbuf_init(msg, zbuf_pulln(zb, len), len, len); @@ -154,7 +162,7 @@ void *zbuf_may_pull_until(struct zbuf *zb, const char *sep, struct zbuf *msg) void zbufq_init(struct zbuf_queue *zbq) { - *zbq = (struct zbuf_queue) { + *zbq = (struct zbuf_queue){ .queue_head = LIST_INITIALIZER(zbq->queue_head), }; } @@ -163,7 +171,8 @@ void zbufq_reset(struct zbuf_queue *zbq) { struct zbuf *buf, *bufn; - list_for_each_entry_safe(buf, bufn, &zbq->queue_head, queue_list) { + list_for_each_entry_safe(buf, bufn, &zbq->queue_head, queue_list) + { list_del(&buf->queue_list); zbuf_free(buf); } @@ -181,10 +190,10 @@ int zbufq_write(struct zbuf_queue *zbq, int fd) ssize_t r; size_t iovcnt = 0; - list_for_each_entry_safe(zb, zbn, &zbq->queue_head, queue_list) { - iov[iovcnt++] = (struct iovec) { - .iov_base = zb->head, - .iov_len = zbuf_used(zb), + list_for_each_entry_safe(zb, zbn, &zbq->queue_head, queue_list) + { + iov[iovcnt++] = (struct iovec){ + .iov_base = zb->head, .iov_len = zbuf_used(zb), }; if (iovcnt >= ZEBRA_NUM_OF(iov)) break; @@ -194,7 +203,8 @@ int zbufq_write(struct zbuf_queue *zbq, int fd) if (r < 0) return r; - list_for_each_entry_safe(zb, zbn, &zbq->queue_head, queue_list) { + list_for_each_entry_safe(zb, zbn, &zbq->queue_head, queue_list) + { if (r < (ssize_t)zbuf_used(zb)) { zb->head += r; return 1; @@ -215,6 +225,7 @@ void zbuf_copy(struct zbuf *zdst, struct zbuf *zsrc, size_t len) dst = zbuf_pushn(zdst, len); src = zbuf_pulln(zsrc, len); - if (!dst || !src) return; + if (!dst || !src) + return; memcpy(dst, src, len); } diff --git a/nhrpd/zbuf.h b/nhrpd/zbuf.h index 73d707344..d03f4ca3a 100644 --- a/nhrpd/zbuf.h +++ b/nhrpd/zbuf.h @@ -77,7 +77,8 @@ static inline void *__zbuf_pull(struct zbuf *zb, size_t size, int error) { void *head = zb->head; if (size > zbuf_used(zb)) { - if (error) zbuf_set_rerror(zb); + if (error) + zbuf_set_rerror(zb); return NULL; } zb->head += size; @@ -94,13 +95,15 @@ void *zbuf_may_pull_until(struct zbuf *zb, const char *sep, struct zbuf *msg); static inline void zbuf_get(struct zbuf *zb, void *dst, size_t len) { void *src = zbuf_pulln(zb, len); - if (src) memcpy(dst, src, len); + if (src) + memcpy(dst, src, len); } static inline uint8_t zbuf_get8(struct zbuf *zb) { uint8_t *src = zbuf_pull(zb, uint8_t); - if (src) return *src; + if (src) + return *src; return 0; } @@ -111,7 +114,8 @@ static inline uint32_t zbuf_get32(struct zbuf *zb) } __attribute__((packed)); struct unaligned32 *v = zbuf_pull(zb, struct unaligned32); - if (v) return v->value; + if (v) + return v->value; return 0; } @@ -122,7 +126,8 @@ static inline uint16_t zbuf_get_be16(struct zbuf *zb) } __attribute__((packed)); struct unaligned16 *v = zbuf_pull(zb, struct unaligned16); - if (v) return be16toh(v->value); + if (v) + return be16toh(v->value); return 0; } @@ -135,7 +140,8 @@ static inline void *__zbuf_push(struct zbuf *zb, size_t size, int error) { void *tail = zb->tail; if (size > zbuf_tailroom(zb)) { - if (error) zbuf_set_werror(zb); + if (error) + zbuf_set_werror(zb); return NULL; } zb->tail += size; @@ -150,13 +156,15 @@ static inline void *__zbuf_push(struct zbuf *zb, size_t size, int error) static inline void zbuf_put(struct zbuf *zb, const void *src, size_t len) { void *dst = zbuf_pushn(zb, len); - if (dst) memcpy(dst, src, len); + if (dst) + memcpy(dst, src, len); } static inline void zbuf_put8(struct zbuf *zb, uint8_t val) { uint8_t *dst = zbuf_push(zb, uint8_t); - if (dst) *dst = val; + if (dst) + *dst = val; } static inline void zbuf_put_be16(struct zbuf *zb, uint16_t val) @@ -166,7 +174,8 @@ static inline void zbuf_put_be16(struct zbuf *zb, uint16_t val) } __attribute__((packed)); struct unaligned16 *v = zbuf_push(zb, struct unaligned16); - if (v) v->value = htobe16(val); + if (v) + v->value = htobe16(val); } static inline void zbuf_put_be32(struct zbuf *zb, uint32_t val) @@ -176,7 +185,8 @@ static inline void zbuf_put_be32(struct zbuf *zb, uint32_t val) } __attribute__((packed)); struct unaligned32 *v = zbuf_push(zb, struct unaligned32); - if (v) v->value = htobe32(val); + if (v) + v->value = htobe32(val); } void zbuf_copy(struct zbuf *zb, struct zbuf *src, size_t len); diff --git a/nhrpd/znl.c b/nhrpd/znl.c index 5e9864c4d..01b2f433a 100644 --- a/nhrpd/znl.c +++ b/nhrpd/znl.c @@ -35,18 +35,18 @@ struct nlmsghdr *znl_nlmsg_push(struct zbuf *zb, uint16_t type, uint16_t flags) struct nlmsghdr *n; n = znl_push(zb, sizeof(*n)); - if (!n) return NULL; + if (!n) + return NULL; - *n = (struct nlmsghdr) { - .nlmsg_type = type, - .nlmsg_flags = flags, + *n = (struct nlmsghdr){ + .nlmsg_type = type, .nlmsg_flags = flags, }; return n; } void znl_nlmsg_complete(struct zbuf *zb, struct nlmsghdr *n) { - n->nlmsg_len = zb->tail - (uint8_t*)n; + n->nlmsg_len = zb->tail - (uint8_t *)n; } struct nlmsghdr *znl_nlmsg_pull(struct zbuf *zb, struct zbuf *payload) @@ -55,7 +55,8 @@ struct nlmsghdr *znl_nlmsg_pull(struct zbuf *zb, struct zbuf *payload) size_t plen; n = znl_pull(zb, sizeof(*n)); - if (!n) return NULL; + if (!n) + return NULL; plen = n->nlmsg_len - sizeof(*n); zbuf_init(payload, znl_pull(zb, plen), plen, plen); @@ -64,22 +65,23 @@ struct nlmsghdr *znl_nlmsg_pull(struct zbuf *zb, struct zbuf *payload) return n; } -struct rtattr *znl_rta_push(struct zbuf *zb, uint16_t type, const void *val, size_t len) +struct rtattr *znl_rta_push(struct zbuf *zb, uint16_t type, const void *val, + size_t len) { struct rtattr *rta; uint8_t *dst; rta = znl_push(zb, ZNL_ALIGN(sizeof(*rta)) + ZNL_ALIGN(len)); - if (!rta) return NULL; + if (!rta) + return NULL; - *rta = (struct rtattr) { - .rta_type = type, - .rta_len = ZNL_ALIGN(sizeof(*rta)) + len, + *rta = (struct rtattr){ + .rta_type = type, .rta_len = ZNL_ALIGN(sizeof(*rta)) + len, }; - dst = (uint8_t *)(rta+1); + dst = (uint8_t *)(rta + 1); memcpy(dst, val, len); - memset(dst+len, 0, ZNL_ALIGN(len) - len); + memset(dst + len, 0, ZNL_ALIGN(len) - len); return rta; } @@ -94,9 +96,10 @@ struct rtattr *znl_rta_nested_push(struct zbuf *zb, uint16_t type) struct rtattr *rta; rta = znl_push(zb, sizeof(*rta)); - if (!rta) return NULL; + if (!rta) + return NULL; - *rta = (struct rtattr) { + *rta = (struct rtattr){ .rta_type = type, }; return rta; @@ -104,12 +107,13 @@ struct rtattr *znl_rta_nested_push(struct zbuf *zb, uint16_t type) void znl_rta_nested_complete(struct zbuf *zb, struct rtattr *rta) { - size_t len = zb->tail - (uint8_t*) rta; + size_t len = zb->tail - (uint8_t *)rta; size_t align = ZNL_ALIGN(len) - len; if (align) { void *dst = zbuf_pushn(zb, align); - if (dst) memset(dst, 0, align); + if (dst) + memset(dst, 0, align); } rta->rta_len = len; } @@ -120,7 +124,8 @@ struct rtattr *znl_rta_pull(struct zbuf *zb, struct zbuf *payload) size_t plen; rta = znl_pull(zb, sizeof(*rta)); - if (!rta) return NULL; + if (!rta) + return NULL; if (rta->rta_len > sizeof(*rta)) { plen = rta->rta_len - sizeof(*rta); @@ -151,7 +156,7 @@ int znl_open(int protocol, int groups) memset(&addr, 0, sizeof(addr)); addr.nl_family = AF_NETLINK; addr.nl_groups = groups; - if (bind(fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) + if (bind(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) goto error; return fd; @@ -159,4 +164,3 @@ error: close(fd); return -1; } - diff --git a/nhrpd/znl.h b/nhrpd/znl.h index 2cd630b5d..fd31daaf4 100644 --- a/nhrpd/znl.h +++ b/nhrpd/znl.h @@ -18,7 +18,8 @@ struct nlmsghdr *znl_nlmsg_push(struct zbuf *zb, uint16_t type, uint16_t flags); void znl_nlmsg_complete(struct zbuf *zb, struct nlmsghdr *n); struct nlmsghdr *znl_nlmsg_pull(struct zbuf *zb, struct zbuf *payload); -struct rtattr *znl_rta_push(struct zbuf *zb, uint16_t type, const void *val, size_t len); +struct rtattr *znl_rta_push(struct zbuf *zb, uint16_t type, const void *val, + size_t len); struct rtattr *znl_rta_push_u32(struct zbuf *zb, uint16_t type, uint32_t val); struct rtattr *znl_rta_nested_push(struct zbuf *zb, uint16_t type); void znl_rta_nested_complete(struct zbuf *zb, struct rtattr *rta); @@ -26,4 +27,3 @@ void znl_rta_nested_complete(struct zbuf *zb, struct rtattr *rta); struct rtattr *znl_rta_pull(struct zbuf *zb, struct zbuf *payload); int znl_open(int protocol, int groups); - |