1 /*
2  * PIM for Quagga
3  * Copyright (C) 2008  Everton da Silva Marques
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; see the file COPYING; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18  */
19 
20 #include <zebra.h>
21 
22 #include "log.h"
23 #include "memory.h"
24 #include "if.h"
25 #include "prefix.h"
26 #include "vty.h"
27 #include "plist.h"
28 #include "hash.h"
29 #include "jhash.h"
30 #include "vrf.h"
31 #include "lib_errors.h"
32 
33 #include "pimd.h"
34 #include "pim_cmd.h"
35 #include "pim_str.h"
36 #include "pim_oil.h"
37 #include "pim_pim.h"
38 #include "pim_ssmpingd.h"
39 #include "pim_static.h"
40 #include "pim_rp.h"
41 #include "pim_ssm.h"
42 #include "pim_vxlan.h"
43 #include "pim_zlookup.h"
44 #include "pim_zebra.h"
45 #include "pim_mlag.h"
46 
47 #if MAXVIFS > 256
48 CPP_NOTICE("Work needs to be done to make this work properly via the pim mroute socket\n");
49 #endif /* MAXVIFS > 256 */
50 
51 const char *const PIM_ALL_SYSTEMS = MCAST_ALL_SYSTEMS;
52 const char *const PIM_ALL_ROUTERS = MCAST_ALL_ROUTERS;
53 const char *const PIM_ALL_PIM_ROUTERS = MCAST_ALL_PIM_ROUTERS;
54 const char *const PIM_ALL_IGMP_ROUTERS = MCAST_ALL_IGMP_ROUTERS;
55 
56 DEFINE_MTYPE_STATIC(PIMD, ROUTER, "PIM Router information");
57 
58 struct pim_router *router = NULL;
59 struct in_addr qpim_all_pim_routers_addr;
60 
pim_prefix_list_update(struct prefix_list * plist)61 void pim_prefix_list_update(struct prefix_list *plist)
62 {
63 	struct pim_instance *pim;
64 	struct vrf *vrf;
65 
66 	RB_FOREACH (vrf, vrf_name_head, &vrfs_by_name) {
67 		pim = vrf->info;
68 		if (!pim)
69 			continue;
70 
71 		pim_rp_prefix_list_update(pim, plist);
72 		pim_ssm_prefix_list_update(pim, plist);
73 		pim_upstream_spt_prefix_list_update(pim, plist);
74 	}
75 }
76 
pim_free(void)77 static void pim_free(void)
78 {
79 	pim_route_map_terminate();
80 
81 	zclient_lookup_free();
82 }
83 
pim_router_init(void)84 void pim_router_init(void)
85 {
86 	router = XCALLOC(MTYPE_ROUTER, sizeof(*router));
87 
88 	router->debugs = 0;
89 	router->master = frr_init();
90 	router->t_periodic = PIM_DEFAULT_T_PERIODIC;
91 
92 	/*
93 	  RFC 4601: 4.6.3.  Assert Metrics
94 
95 	  assert_metric
96 	  infinite_assert_metric() {
97 	  return {1,infinity,infinity,0}
98 	  }
99 	*/
100 	router->infinite_assert_metric.rpt_bit_flag = 1;
101 	router->infinite_assert_metric.metric_preference =
102 		PIM_ASSERT_METRIC_PREFERENCE_MAX;
103 	router->infinite_assert_metric.route_metric =
104 		PIM_ASSERT_ROUTE_METRIC_MAX;
105 	router->infinite_assert_metric.ip_address.s_addr = INADDR_ANY;
106 	router->rpf_cache_refresh_delay_msec = 50;
107 	router->register_suppress_time = PIM_REGISTER_SUPPRESSION_TIME_DEFAULT;
108 	router->packet_process = PIM_DEFAULT_PACKET_PROCESS;
109 	router->register_probe_time = PIM_REGISTER_PROBE_TIME_DEFAULT;
110 	router->vrf_id = VRF_DEFAULT;
111 	router->pim_mlag_intf_cnt = 0;
112 	router->connected_to_mlag = false;
113 }
114 
pim_router_terminate(void)115 void pim_router_terminate(void)
116 {
117 	pim_mlag_terminate();
118 	XFREE(MTYPE_ROUTER, router);
119 }
120 
pim_init(void)121 void pim_init(void)
122 {
123 	if (!inet_aton(PIM_ALL_PIM_ROUTERS, &qpim_all_pim_routers_addr)) {
124 		flog_err(
125 			EC_LIB_SOCKET,
126 			"%s %s: could not solve %s to group address: errno=%d: %s",
127 			__FILE__, __func__, PIM_ALL_PIM_ROUTERS, errno,
128 			safe_strerror(errno));
129 		zassert(0);
130 		return;
131 	}
132 
133 	pim_cmd_init();
134 }
135 
pim_terminate(void)136 void pim_terminate(void)
137 {
138 	struct zclient *zclient;
139 
140 	/* reverse prefix_list_init */
141 	prefix_list_add_hook(NULL);
142 	prefix_list_delete_hook(NULL);
143 	prefix_list_reset();
144 
145 	pim_vxlan_terminate();
146 	pim_vrf_terminate();
147 
148 	zclient = pim_zebra_zclient_get();
149 	if (zclient) {
150 		zclient_stop(zclient);
151 		zclient_free(zclient);
152 	}
153 
154 	pim_free();
155 	pim_router_terminate();
156 
157 	frr_fini();
158 }
159