From fa31fcf2ea1157d3d14968d704878cc8fe058c91 Mon Sep 17 00:00:00 2001 From: Amol Lad Date: Wed, 17 Feb 2021 13:47:32 +1300 Subject: nhrpd: Add support for forwarding multicast packets Forwarding multicast is a pre-requisite for allowing multicast based routing protocols such as OSPF to work with DMVPN This code relies on externally adding iptables rule. For example: iptables -A OUTPUT -d 224.0.0.0/24 -o gre1 -j NFLOG --nflog-group 224 Signed-off-by: Reuben Dowle --- nhrpd/nhrpd.h | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) (limited to 'nhrpd/nhrpd.h') diff --git a/nhrpd/nhrpd.h b/nhrpd/nhrpd.h index e4afb22f8..1d0d99915 100644 --- a/nhrpd/nhrpd.h +++ b/nhrpd/nhrpd.h @@ -24,6 +24,7 @@ DECLARE_MGROUP(NHRPD); #define NHRP_VTY_PORT 2610 #define NHRP_DEFAULT_CONFIG "nhrpd.conf" +#define MCAST_NFLOG_GROUP 224 extern struct thread_master *master; @@ -264,6 +265,13 @@ struct nhrp_nhs { struct list_head reglist_head; }; +struct nhrp_multicast { + struct interface *ifp; + struct list_head list_entry; + afi_t afi; + union sockunion nbma_addr; /* IP-address */ +}; + struct nhrp_registration { struct list_head reglist_entry; struct thread *t_register; @@ -309,6 +317,7 @@ struct nhrp_interface { unsigned short mtu; unsigned int holdtime; struct list_head nhslist_head; + struct list_head mcastlist_head; } afi[AFI_MAX]; }; @@ -350,6 +359,13 @@ void nhrp_nhs_foreach(struct interface *ifp, afi_t afi, void *ctx); void nhrp_nhs_interface_del(struct interface *ifp); +int nhrp_multicast_add(struct interface *ifp, afi_t afi, union sockunion *nbma_addr); +int nhrp_multicast_del(struct interface *ifp, afi_t afi, union sockunion *nbma_addr); +void nhrp_multicast_interface_del(struct interface *ifp); +void nhrp_multicast_foreach(struct interface *ifp, afi_t afi, + void (*cb)(struct nhrp_multicast *, 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, -- cgit v1.2.3 From 0f8595a9f427165fac9d57e6a7d4df61fbdbd68c Mon Sep 17 00:00:00 2001 From: Reuben Dowle Date: Tue, 2 Mar 2021 16:34:15 +1300 Subject: nhrpd: Fix style violation issues Signed-off-by: Reuben Dowle --- nhrpd/nhrp_multicast.c | 75 +++++++++++++++++++++++++++++--------------------- nhrpd/nhrp_peer.c | 3 +- nhrpd/nhrp_vty.c | 18 ++++++------ nhrpd/nhrpd.h | 10 ++++--- 4 files changed, 60 insertions(+), 46 deletions(-) (limited to 'nhrpd/nhrpd.h') diff --git a/nhrpd/nhrp_multicast.c b/nhrpd/nhrp_multicast.c index 2f9665ab8..6fb54cd70 100755 --- a/nhrpd/nhrp_multicast.c +++ b/nhrpd/nhrp_multicast.c @@ -47,19 +47,22 @@ static void nhrp_multicast_send(struct nhrp_peer *p, struct zbuf *zb) addrlen = sockunion_get_addrlen(&p->vc->remote.nbma); ret = os_sendmsg(zb->head, zbuf_used(zb), p->ifp->ifindex, - sockunion_get_addr(&p->vc->remote.nbma), - addrlen, addrlen == 4 ? 0x0800 : 0x86DD); - - debugf(NHRP_DEBUG_COMMON, "Multicast Packet: %s -> %s, ret = %d, size = %zu, addrlen = %zu", - sockunion2str(&p->vc->local.nbma, buf[0], sizeof(buf[0])), - sockunion2str(&p->vc->remote.nbma, buf[1], sizeof(buf[1])), - ret, zbuf_used(zb), addrlen); + sockunion_get_addr(&p->vc->remote.nbma), addrlen, + addrlen == 4 ? 0x0800 : 0x86DD); + + debugf(NHRP_DEBUG_COMMON, + "Multicast Packet: %s -> %s, ret = %d, size = %zu, addrlen = %zu", + sockunion2str(&p->vc->local.nbma, buf[0], sizeof(buf[0])), + sockunion2str(&p->vc->remote.nbma, buf[1], sizeof(buf[1])), ret, + zbuf_used(zb), addrlen); } -static void nhrp_multicast_forward_nbma(union sockunion *nbma_addr, struct interface *ifp, struct zbuf *pkt) +static void nhrp_multicast_forward_nbma(union sockunion *nbma_addr, + struct interface *ifp, struct zbuf *pkt) { struct nhrp_peer *p = nhrp_peer_get(ifp, nbma_addr); - if(p && p->online) { + + if (p && p->online) { /* Send packet */ nhrp_multicast_send(p, pkt); } @@ -71,7 +74,8 @@ static void nhrp_multicast_forward_cache(struct nhrp_cache *c, void *pctx) struct mcast_ctx *ctx = (struct mcast_ctx *)pctx; if (c->cur.type == NHRP_CACHE_DYNAMIC && c->cur.peer) - nhrp_multicast_forward_nbma(&c->cur.peer->vc->remote.nbma, ctx->ifp, ctx->pkt); + nhrp_multicast_forward_nbma(&c->cur.peer->vc->remote.nbma, + ctx->ifp, ctx->pkt); } static void nhrp_multicast_forward(struct nhrp_multicast *mcast, void *pctx) @@ -84,7 +88,8 @@ static void nhrp_multicast_forward(struct nhrp_multicast *mcast, void *pctx) /* dynamic */ if (sockunion_family(&mcast->nbma_addr) == AF_UNSPEC) { - nhrp_cache_foreach(ctx->ifp, nhrp_multicast_forward_cache, pctx); + nhrp_cache_foreach(ctx->ifp, nhrp_multicast_forward_cache, + pctx); return; } @@ -102,7 +107,7 @@ static void netlink_mcast_log_handler(struct nlmsghdr *msg, struct zbuf *zb) afi_t afi; struct mcast_ctx ctx; - debugf(NHRP_DEBUG_COMMON,"Inside %s\n", __func__); + debugf(NHRP_DEBUG_COMMON, "Inside %s\n", __func__); nf = znl_pull(zb, sizeof(*nf)); if (!nf) @@ -120,7 +125,8 @@ static void netlink_mcast_log_handler(struct nlmsghdr *msg, struct zbuf *zb) /* 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. */ + * route. + */ } } @@ -131,15 +137,15 @@ static void netlink_mcast_log_handler(struct nlmsghdr *msg, struct zbuf *zb) if (!ifp) return; - debugf(NHRP_DEBUG_COMMON,"Outgoing interface = %s\n", ifp->name); + debugf(NHRP_DEBUG_COMMON, "Outgoing interface = %s\n", ifp->name); - ctx = (struct mcast_ctx) { - .ifp = ifp, - .pkt = &pktpl, + ctx = (struct mcast_ctx){ + .ifp = ifp, .pkt = &pktpl, }; for (afi = 0; afi < AFI_MAX; afi++) { - nhrp_multicast_foreach(ifp, afi, nhrp_multicast_forward, (void *)&ctx); + nhrp_multicast_foreach(ifp, afi, nhrp_multicast_forward, + (void *)&ctx); } } @@ -195,7 +201,8 @@ static void netlink_mcast_log_register(int fd, int group) zbuf_free(zb); } -static int nhrp_multicast_free(struct interface *ifp, struct nhrp_multicast *mcast) +static int nhrp_multicast_free(struct interface *ifp, + struct nhrp_multicast *mcast) { list_del(&mcast->list_entry); XFREE(MTYPE_NHRP_MULTICAST, mcast); @@ -217,13 +224,16 @@ static void netlink_mcast_set_nflog_group(struct interface *ifp, int nlgroup) return; netlink_mcast_log_register(netlink_mcast_log_fd, nlgroup); - thread_add_read(master, netlink_mcast_log_recv, 0, netlink_mcast_log_fd, + thread_add_read(master, netlink_mcast_log_recv, 0, + netlink_mcast_log_fd, &netlink_mcast_log_thread); - debugf(NHRP_DEBUG_COMMON, "Register nflog group: %d", netlink_mcast_nflog_group); + debugf(NHRP_DEBUG_COMMON, "Register nflog group: %d", + netlink_mcast_nflog_group); } } -int nhrp_multicast_add(struct interface *ifp, afi_t afi, union sockunion *nbma_addr) +int nhrp_multicast_add(struct interface *ifp, afi_t afi, + union sockunion *nbma_addr) { struct nhrp_interface *nifp = ifp->info; struct nhrp_multicast *mcast; @@ -238,9 +248,7 @@ int nhrp_multicast_add(struct interface *ifp, afi_t afi, union sockunion *nbma_a mcast = XMALLOC(MTYPE_NHRP_MULTICAST, sizeof(struct nhrp_multicast)); *mcast = (struct nhrp_multicast){ - .afi = afi, - .ifp = ifp, - .nbma_addr = *nbma_addr, + .afi = afi, .ifp = ifp, .nbma_addr = *nbma_addr, }; list_add_tail(&mcast->list_entry, &nifp->afi[afi].mcastlist_head); @@ -253,7 +261,8 @@ int nhrp_multicast_add(struct interface *ifp, afi_t afi, union sockunion *nbma_a return NHRP_OK; } -int nhrp_multicast_del(struct interface *ifp, afi_t afi, union sockunion *nbma_addr) +int nhrp_multicast_del(struct interface *ifp, afi_t afi, + union sockunion *nbma_addr) { struct nhrp_interface *nifp = ifp->info; struct nhrp_multicast *mcast, *tmp; @@ -283,25 +292,27 @@ void nhrp_multicast_interface_del(struct interface *ifp) afi_t afi; for (afi = 0; afi < AFI_MAX; afi++) { - debugf(NHRP_DEBUG_COMMON, "Cleaning up multicast entries (%d)", !list_empty(&nifp->afi[afi].mcastlist_head)); + debugf(NHRP_DEBUG_COMMON, + "Cleaning up multicast entries (%d)", + !list_empty(&nifp->afi[afi].mcastlist_head)); list_for_each_entry_safe( - mcast, tmp, &nifp->afi[afi].mcastlist_head, - list_entry) { + mcast, tmp, &nifp->afi[afi].mcastlist_head, list_entry) + { nhrp_multicast_free(ifp, mcast); } } } void nhrp_multicast_foreach(struct interface *ifp, afi_t afi, - void (*cb)(struct nhrp_multicast *, void *), - void *ctx) + void (*cb)(struct nhrp_multicast *, void *), + void *ctx) { struct nhrp_interface *nifp = ifp->info; struct nhrp_multicast *mcast; list_for_each_entry(mcast, &nifp->afi[afi].mcastlist_head, list_entry) { - cb (mcast, ctx); + cb(mcast, ctx); } } diff --git a/nhrpd/nhrp_peer.c b/nhrpd/nhrp_peer.c index 817468076..0c2259dc9 100644 --- a/nhrpd/nhrp_peer.c +++ b/nhrpd/nhrp_peer.c @@ -375,8 +375,7 @@ void nhrp_peer_send(struct nhrp_peer *p, struct zbuf *zb) os_sendmsg(zb->head, zbuf_used(zb), p->ifp->ifindex, sockunion_get_addr(&p->vc->remote.nbma), - sockunion_get_addrlen(&p->vc->remote.nbma), - ETH_P_NHRP); + sockunion_get_addrlen(&p->vc->remote.nbma), ETH_P_NHRP); zbuf_reset(zb); } diff --git a/nhrpd/nhrp_vty.c b/nhrpd/nhrp_vty.c index e649dc34e..c16a816a3 100644 --- a/nhrpd/nhrp_vty.c +++ b/nhrpd/nhrp_vty.c @@ -779,8 +779,8 @@ static void show_ip_nhrp_nhs(struct nhrp_nhs *n, struct nhrp_registration *reg, ctx->count++; if (reg && reg->peer) - sockunion2str(®->peer->vc->remote.nbma, - buf[0], sizeof(buf[0])); + sockunion2str(®->peer->vc->remote.nbma, buf[0], + sizeof(buf[0])); else snprintf(buf[0], sizeof(buf[0]), "-"); sockunion2str(reg ? ®->proto_addr : &n->proto_addr, buf[1], @@ -1094,7 +1094,8 @@ struct write_map_ctx { const char *aficmd; }; -static void interface_config_write_nhrp_map(struct nhrp_cache_config *c, void *data) +static void interface_config_write_nhrp_map(struct nhrp_cache_config *c, + void *data) { struct write_map_ctx *ctx = data; struct vty *vty = ctx->vty; @@ -1106,7 +1107,8 @@ static void interface_config_write_nhrp_map(struct nhrp_cache_config *c, void *d vty_out(vty, " %s nhrp map %s %s\n", ctx->aficmd, sockunion2str(&c->remote_addr, buf[0], sizeof(buf[0])), c->type == NHRP_CACHE_LOCAL - ? "local" : sockunion2str(&c->nbma, buf[1], sizeof(buf[1]))); + ? "local" + : sockunion2str(&c->nbma, buf[1], sizeof(buf[1]))); } static int interface_config_write(struct vty *vty) @@ -1170,8 +1172,8 @@ static int interface_config_write(struct vty *vty) .family = afi2family(afi), .aficmd = aficmd, }; - nhrp_cache_config_foreach(ifp, interface_config_write_nhrp_map, - &mapctx); + nhrp_cache_config_foreach( + ifp, interface_config_write_nhrp_map, &mapctx); list_for_each_entry(nhs, &ad->nhslist_head, nhslist_entry) @@ -1196,8 +1198,8 @@ static int interface_config_write(struct vty *vty) == AF_UNSPEC ? "dynamic" : sockunion2str( - &mcast->nbma_addr, buf, - sizeof(buf))); + &mcast->nbma_addr, + buf, sizeof(buf))); } } diff --git a/nhrpd/nhrpd.h b/nhrpd/nhrpd.h index 1d0d99915..db7828f71 100644 --- a/nhrpd/nhrpd.h +++ b/nhrpd/nhrpd.h @@ -359,12 +359,14 @@ void nhrp_nhs_foreach(struct interface *ifp, afi_t afi, void *ctx); void nhrp_nhs_interface_del(struct interface *ifp); -int nhrp_multicast_add(struct interface *ifp, afi_t afi, union sockunion *nbma_addr); -int nhrp_multicast_del(struct interface *ifp, afi_t afi, union sockunion *nbma_addr); +int nhrp_multicast_add(struct interface *ifp, afi_t afi, + union sockunion *nbma_addr); +int nhrp_multicast_del(struct interface *ifp, afi_t afi, + union sockunion *nbma_addr); void nhrp_multicast_interface_del(struct interface *ifp); void nhrp_multicast_foreach(struct interface *ifp, afi_t afi, - void (*cb)(struct nhrp_multicast *, void *), - void *ctx); + void (*cb)(struct nhrp_multicast *, 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, -- cgit v1.2.3 From 9084e209611496f98c4960928fbecd51095ebfd5 Mon Sep 17 00:00:00 2001 From: Amol Lad Date: Thu, 17 Dec 2020 13:38:06 +0530 Subject: nhrpd: Introduce new - nflog mutlicast-nflog-group (1-65535) - command Signed-off-by: Reuben Dowle --- nhrpd/netlink.h | 1 + nhrpd/nhrp_multicast.c | 23 ++++++++++------------- nhrpd/nhrp_vty.c | 30 ++++++++++++++++++++++++++++++ nhrpd/nhrpd.h | 2 +- 4 files changed, 42 insertions(+), 14 deletions(-) (limited to 'nhrpd/nhrpd.h') diff --git a/nhrpd/netlink.h b/nhrpd/netlink.h index 74cb81daa..5e971cabf 100644 --- a/nhrpd/netlink.h +++ b/nhrpd/netlink.h @@ -13,6 +13,7 @@ union sockunion; struct interface; extern int netlink_nflog_group; +extern int netlink_mcast_nflog_group; extern int netlink_req_fd; void netlink_init(void); diff --git a/nhrpd/nhrp_multicast.c b/nhrpd/nhrp_multicast.c index 42e3baaee..822a63b5f 100644 --- a/nhrpd/nhrp_multicast.c +++ b/nhrpd/nhrp_multicast.c @@ -30,7 +30,7 @@ DEFINE_MTYPE_STATIC(NHRPD, NHRP_MULTICAST, "NHRP Multicast") -static int netlink_mcast_nflog_group; +int netlink_mcast_nflog_group; static int netlink_mcast_log_fd = -1; static struct thread *netlink_mcast_log_thread; @@ -201,15 +201,7 @@ static void netlink_mcast_log_register(int fd, int group) zbuf_free(zb); } -static int nhrp_multicast_free(struct interface *ifp, - struct nhrp_multicast *mcast) -{ - list_del(&mcast->list_entry); - XFREE(MTYPE_NHRP_MULTICAST, mcast); - return 0; -} - -static void netlink_mcast_set_nflog_group(struct interface *ifp, int nlgroup) +void netlink_mcast_set_nflog_group(int nlgroup) { if (netlink_mcast_log_fd >= 0) { THREAD_OFF(netlink_mcast_log_thread); @@ -232,6 +224,14 @@ static void netlink_mcast_set_nflog_group(struct interface *ifp, int nlgroup) } } +static int nhrp_multicast_free(struct interface *ifp, + struct nhrp_multicast *mcast) +{ + list_del(&mcast->list_entry); + XFREE(MTYPE_NHRP_MULTICAST, mcast); + return 0; +} + int nhrp_multicast_add(struct interface *ifp, afi_t afi, union sockunion *nbma_addr) { @@ -252,9 +252,6 @@ int nhrp_multicast_add(struct interface *ifp, afi_t afi, }; list_add_tail(&mcast->list_entry, &nifp->afi[afi].mcastlist_head); - if (netlink_mcast_log_fd == -1) - netlink_mcast_set_nflog_group(ifp, MCAST_NFLOG_GROUP); - sockunion2str(nbma_addr, buf, sizeof(buf)); debugf(NHRP_DEBUG_COMMON, "Adding multicast entry (%s)", buf); diff --git a/nhrpd/nhrp_vty.c b/nhrpd/nhrp_vty.c index c16a816a3..48365bafb 100644 --- a/nhrpd/nhrp_vty.c +++ b/nhrpd/nhrp_vty.c @@ -187,6 +187,9 @@ static int nhrp_config_write(struct vty *vty) if (netlink_nflog_group) { vty_out(vty, "nhrp nflog-group %d\n", netlink_nflog_group); } + if (netlink_mcast_nflog_group) + vty_out(vty, "nhrp multicast-nflog-group %d\n", + netlink_mcast_nflog_group); return 0; } @@ -257,6 +260,31 @@ DEFUN(no_nhrp_nflog_group, no_nhrp_nflog_group_cmd, return CMD_SUCCESS; } +DEFUN(nhrp_multicast_nflog_group, nhrp_multicast_nflog_group_cmd, + "nhrp multicast-nflog-group (1-65535)", + NHRP_STR + "Specify NFLOG group number for Multicast Packets\n" + "NFLOG group number\n") +{ + uint32_t nfgroup; + + nfgroup = strtoul(argv[2]->arg, NULL, 10); + netlink_mcast_set_nflog_group(nfgroup); + + return CMD_SUCCESS; +} + +DEFUN(no_nhrp_multicast_nflog_group, no_nhrp_multicast_nflog_group_cmd, + "no nhrp multicast-nflog-group [(1-65535)]", + NO_STR + NHRP_STR + "Specify NFLOG group number\n" + "NFLOG group number\n") +{ + netlink_mcast_set_nflog_group(0); + return CMD_SUCCESS; +} + DEFUN(tunnel_protection, tunnel_protection_cmd, "tunnel protection vici profile PROFILE [fallback-profile FALLBACK]", "NHRP/GRE integration\n" @@ -1234,6 +1262,8 @@ void nhrp_config_init(void) install_element(CONFIG_NODE, &no_nhrp_event_socket_cmd); install_element(CONFIG_NODE, &nhrp_nflog_group_cmd); install_element(CONFIG_NODE, &no_nhrp_nflog_group_cmd); + install_element(CONFIG_NODE, &nhrp_multicast_nflog_group_cmd); + install_element(CONFIG_NODE, &no_nhrp_multicast_nflog_group_cmd); /* interface specific commands */ install_node(&nhrp_interface_node); diff --git a/nhrpd/nhrpd.h b/nhrpd/nhrpd.h index db7828f71..5562916ae 100644 --- a/nhrpd/nhrpd.h +++ b/nhrpd/nhrpd.h @@ -24,7 +24,6 @@ DECLARE_MGROUP(NHRPD); #define NHRP_VTY_PORT 2610 #define NHRP_DEFAULT_CONFIG "nhrpd.conf" -#define MCAST_NFLOG_GROUP 224 extern struct thread_master *master; @@ -367,6 +366,7 @@ void nhrp_multicast_interface_del(struct interface *ifp); void nhrp_multicast_foreach(struct interface *ifp, afi_t afi, void (*cb)(struct nhrp_multicast *, void *), void *ctx); +void netlink_mcast_set_nflog_group(int nlgroup); void nhrp_route_update_nhrp(const struct prefix *p, struct interface *ifp); void nhrp_route_announce(int add, enum nhrp_cache_type type, -- cgit v1.2.3