1 /*
2  * OSPF Link State Advertisement
3  * Copyright (C) 1999, 2000 Toshiaki Takada
4  *
5  * This file is part of GNU Zebra.
6  *
7  * GNU Zebra is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU General Public License as published by the
9  * Free Software Foundation; either version 2, or (at your option) any
10  * later version.
11  *
12  * GNU Zebra is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * with this program; see the file COPYING; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 #ifndef _ZEBRA_OSPF_LSA_H
23 #define _ZEBRA_OSPF_LSA_H
24 
25 #include "stream.h"
26 
27 /* OSPF LSA Default metric values */
28 #define DEFAULT_DEFAULT_METRIC 20
29 #define DEFAULT_DEFAULT_ORIGINATE_METRIC 10
30 #define DEFAULT_DEFAULT_ALWAYS_METRIC 1
31 #define DEFAULT_METRIC_TYPE EXTERNAL_METRIC_TYPE_2
32 
33 /* OSPF LSA Range definition. */
34 #define OSPF_MIN_LSA		1  /* begin range here */
35 #define OSPF_MAX_LSA           12
36 
37 /* OSPF LSA Type definition. */
38 #define OSPF_UNKNOWN_LSA	      0
39 #define OSPF_ROUTER_LSA               1
40 #define OSPF_NETWORK_LSA              2
41 #define OSPF_SUMMARY_LSA              3
42 #define OSPF_ASBR_SUMMARY_LSA         4
43 #define OSPF_AS_EXTERNAL_LSA          5
44 #define OSPF_GROUP_MEMBER_LSA	      6  /* Not supported. */
45 #define OSPF_AS_NSSA_LSA	              7
46 #define OSPF_EXTERNAL_ATTRIBUTES_LSA  8  /* Not supported. */
47 #define OSPF_OPAQUE_LINK_LSA	      9
48 #define OSPF_OPAQUE_AREA_LSA	     10
49 #define OSPF_OPAQUE_AS_LSA	     11
50 
51 #define OSPF_LSA_HEADER_SIZE	     20U
52 #define OSPF_ROUTER_LSA_LINK_SIZE    12U
53 #define OSPF_ROUTER_LSA_TOS_SIZE      4U
54 #define OSPF_MAX_LSA_SIZE	   1500U
55 
56 /* AS-external-LSA refresh method. */
57 #define LSA_REFRESH_IF_CHANGED	0
58 #define LSA_REFRESH_FORCE	1
59 
60 /* OSPF LSA header. */
61 struct lsa_header {
62 	uint16_t ls_age;
63 	uint8_t options;
64 	uint8_t type;
65 	struct in_addr id;
66 	struct in_addr adv_router;
67 	uint32_t ls_seqnum;
68 	uint16_t checksum;
69 	uint16_t length;
70 };
71 
72 struct vertex;
73 
74 /* OSPF LSA. */
75 struct ospf_lsa {
76 	/* LSA origination flag. */
77 	uint8_t flags;
78 #define OSPF_LSA_SELF		  0x01
79 #define OSPF_LSA_SELF_CHECKED	  0x02
80 #define OSPF_LSA_RECEIVED	  0x04
81 #define OSPF_LSA_APPROVED	  0x08
82 #define OSPF_LSA_DISCARD	  0x10
83 #define OSPF_LSA_LOCAL_XLT	  0x20
84 #define OSPF_LSA_PREMATURE_AGE	  0x40
85 #define OSPF_LSA_IN_MAXAGE	  0x80
86 
87 	/* LSA data. */
88 	struct lsa_header *data;
89 
90 	/* Received time stamp. */
91 	struct timeval tv_recv;
92 
93 	/* Last time it was originated */
94 	struct timeval tv_orig;
95 
96 	/* All of reference count, also lock to remove. */
97 	int lock;
98 
99 	/* Flags for the SPF calculation. */
100 	struct vertex *stat;
101 
102 	/* References to this LSA in neighbor retransmission lists*/
103 	int retransmit_counter;
104 
105 	/* Area the LSA belongs to, may be NULL if AS-external-LSA. */
106 	struct ospf_area *area;
107 
108 	/* Parent LSDB. */
109 	struct ospf_lsdb *lsdb;
110 
111 	/* Related Route. */
112 	void *route;
113 
114 	/* Refreshement List or Queue */
115 	int refresh_list;
116 
117 	/* For Type-9 Opaque-LSAs */
118 	struct ospf_interface *oi;
119 
120 	/* VRF Id */
121 	vrf_id_t vrf_id;
122 };
123 
124 /* OSPF LSA Link Type. */
125 #define LSA_LINK_TYPE_POINTOPOINT      1
126 #define LSA_LINK_TYPE_TRANSIT          2
127 #define LSA_LINK_TYPE_STUB             3
128 #define LSA_LINK_TYPE_VIRTUALLINK      4
129 
130 /* OSPF Router LSA Flag. */
131 #define ROUTER_LSA_BORDER	       0x01 /* The router is an ABR */
132 #define ROUTER_LSA_EXTERNAL	       0x02 /* The router is an ASBR */
133 #define ROUTER_LSA_VIRTUAL	       0x04 /* The router has a VL in this area */
134 #define ROUTER_LSA_NT		       0x10 /* The routers always translates Type-7 */
135 #define ROUTER_LSA_SHORTCUT	       0x20 /* Shortcut-ABR specific flag */
136 
137 #define IS_ROUTER_LSA_VIRTUAL(x)       ((x)->flags & ROUTER_LSA_VIRTUAL)
138 #define IS_ROUTER_LSA_EXTERNAL(x)      ((x)->flags & ROUTER_LSA_EXTERNAL)
139 #define IS_ROUTER_LSA_BORDER(x)	       ((x)->flags & ROUTER_LSA_BORDER)
140 #define IS_ROUTER_LSA_SHORTCUT(x)      ((x)->flags & ROUTER_LSA_SHORTCUT)
141 #define IS_ROUTER_LSA_NT(x)            ((x)->flags & ROUTER_LSA_NT)
142 
143 /* OSPF Router-LSA Link information. */
144 struct router_lsa_link {
145 	struct in_addr link_id;
146 	struct in_addr link_data;
147 	struct {
148 		uint8_t type;
149 		uint8_t tos_count;
150 		uint16_t metric;
151 	} m[1];
152 };
153 
154 /* OSPF Router-LSAs structure. */
155 #define OSPF_ROUTER_LSA_MIN_SIZE                   4U /* w/0 link descriptors */
156 /* There is an edge case, when number of links in a Router-LSA may be 0 without
157    breaking the specification. A router, which has no other links to backbone
158    area besides one virtual link, will not put any VL descriptor blocks into
159    the Router-LSA generated for area 0 until a full adjacency over the VL is
160    reached (RFC2328 12.4.1.3). In this case the Router-LSA initially received
161    by the other end of the VL will have 0 link descriptor blocks, but soon will
162    be replaced with the next revision having 1 descriptor block. */
163 struct router_lsa {
164 	struct lsa_header header;
165 	uint8_t flags;
166 	uint8_t zero;
167 	uint16_t links;
168 	struct {
169 		struct in_addr link_id;
170 		struct in_addr link_data;
171 		uint8_t type;
172 		uint8_t tos;
173 		uint16_t metric;
174 	} link[1];
175 };
176 
177 /* OSPF Network-LSAs structure. */
178 #define OSPF_NETWORK_LSA_MIN_SIZE                  8U /* w/1 router-ID */
179 struct network_lsa {
180 	struct lsa_header header;
181 	struct in_addr mask;
182 	struct in_addr routers[1];
183 };
184 
185 /* OSPF Summary-LSAs structure. */
186 #define OSPF_SUMMARY_LSA_MIN_SIZE                  8U /* w/1 TOS metric block */
187 struct summary_lsa {
188 	struct lsa_header header;
189 	struct in_addr mask;
190 	uint8_t tos;
191 	uint8_t metric[3];
192 };
193 
194 /* OSPF AS-external-LSAs structure. */
195 #define OSPF_AS_EXTERNAL_LSA_MIN_SIZE             16U /* w/1 TOS forwarding block */
196 struct as_external_lsa {
197 	struct lsa_header header;
198 	struct in_addr mask;
199 	struct {
200 		uint8_t tos;
201 		uint8_t metric[3];
202 		struct in_addr fwd_addr;
203 		uint32_t route_tag;
204 	} e[1];
205 };
206 
207 #include "ospfd/ospf_opaque.h"
208 
209 /* Macros. */
210 #define GET_METRIC(x) get_metric(x)
211 #define IS_EXTERNAL_METRIC(x)   ((x) & 0x80)
212 
213 #define GET_AGE(x)     (ntohs ((x)->data->ls_age) + time (NULL) - (x)->tv_recv)
214 #define LS_AGE(x) (OSPF_LSA_MAXAGE < get_age(x) ? OSPF_LSA_MAXAGE : get_age(x))
215 #define IS_LSA_SELF(L)          (CHECK_FLAG ((L)->flags, OSPF_LSA_SELF))
216 #define IS_LSA_MAXAGE(L)        (LS_AGE ((L)) == OSPF_LSA_MAXAGE)
217 
218 #define OSPF_LSA_UPDATE_DELAY		2
219 
220 #define OSPF_LSA_UPDATE_TIMER_ON(T, F)                                         \
221 	if (!(T))                                                              \
222 	(T) = thread_add_timer(master, (F), 0, 2)
223 
224 /* Prototypes. */
225 /* XXX: Eek, time functions, similar are in lib/thread.c */
226 extern struct timeval int2tv(int);
227 extern struct timeval msec2tv(int);
228 
229 extern int get_age(struct ospf_lsa *);
230 extern uint16_t ospf_lsa_checksum(struct lsa_header *);
231 extern int ospf_lsa_checksum_valid(struct lsa_header *);
232 extern int ospf_lsa_refresh_delay(struct ospf_lsa *);
233 
234 extern const char *dump_lsa_key(struct ospf_lsa *);
235 extern uint32_t lsa_seqnum_increment(struct ospf_lsa *);
236 extern void lsa_header_set(struct stream *, uint8_t, uint8_t, struct in_addr,
237 			   struct in_addr);
238 extern struct ospf_neighbor *ospf_nbr_lookup_ptop(struct ospf_interface *);
239 extern int ospf_check_nbr_status(struct ospf *);
240 
241 /* Prototype for LSA primitive. */
242 extern struct ospf_lsa *ospf_lsa_new(void);
243 extern struct ospf_lsa *ospf_lsa_new_and_data(size_t size);
244 extern struct ospf_lsa *ospf_lsa_dup(struct ospf_lsa *);
245 extern void ospf_lsa_free(struct ospf_lsa *);
246 extern struct ospf_lsa *ospf_lsa_lock(struct ospf_lsa *);
247 extern void ospf_lsa_unlock(struct ospf_lsa **);
248 extern void ospf_lsa_discard(struct ospf_lsa *);
249 extern int ospf_lsa_flush_schedule(struct ospf *, struct ospf_lsa *);
250 extern struct lsa_header *ospf_lsa_data_new(size_t);
251 extern struct lsa_header *ospf_lsa_data_dup(struct lsa_header *);
252 extern void ospf_lsa_data_free(struct lsa_header *);
253 
254 /* Prototype for various LSAs */
255 extern int ospf_router_lsa_update(struct ospf *);
256 extern int ospf_router_lsa_update_area(struct ospf_area *);
257 
258 extern void ospf_network_lsa_update(struct ospf_interface *);
259 
260 extern struct ospf_lsa *
261 ospf_summary_lsa_originate(struct prefix_ipv4 *, uint32_t, struct ospf_area *);
262 extern struct ospf_lsa *ospf_summary_asbr_lsa_originate(struct prefix_ipv4 *,
263 							uint32_t,
264 							struct ospf_area *);
265 
266 extern struct ospf_lsa *ospf_lsa_install(struct ospf *, struct ospf_interface *,
267 					 struct ospf_lsa *);
268 
269 extern void ospf_nssa_lsa_flush(struct ospf *ospf, struct prefix_ipv4 *p);
270 extern void ospf_external_lsa_flush(struct ospf *, uint8_t,
271 				    struct prefix_ipv4 *,
272 				    ifindex_t /* , struct in_addr nexthop */);
273 
274 extern struct in_addr ospf_get_ip_from_ifp(struct ospf_interface *);
275 
276 extern struct ospf_lsa *ospf_external_lsa_originate(struct ospf *,
277 						    struct external_info *);
278 extern void ospf_external_lsa_rid_change(struct ospf *ospf);
279 extern struct ospf_lsa *ospf_lsa_lookup(struct ospf *ospf, struct ospf_area *,
280 					uint32_t, struct in_addr,
281 					struct in_addr);
282 extern struct ospf_lsa *ospf_lsa_lookup_by_id(struct ospf_area *, uint32_t,
283 					      struct in_addr);
284 extern struct ospf_lsa *ospf_lsa_lookup_by_header(struct ospf_area *,
285 						  struct lsa_header *);
286 extern int ospf_lsa_more_recent(struct ospf_lsa *, struct ospf_lsa *);
287 extern int ospf_lsa_different(struct ospf_lsa *, struct ospf_lsa *);
288 extern void ospf_flush_self_originated_lsas_now(struct ospf *);
289 
290 extern int ospf_lsa_is_self_originated(struct ospf *, struct ospf_lsa *);
291 
292 extern struct ospf_lsa *ospf_lsa_lookup_by_prefix(struct ospf_lsdb *, uint8_t,
293 						  struct prefix_ipv4 *,
294 						  struct in_addr);
295 
296 extern void ospf_lsa_maxage(struct ospf *, struct ospf_lsa *);
297 extern uint32_t get_metric(uint8_t *);
298 
299 extern int ospf_lsa_maxage_walker(struct thread *);
300 extern struct ospf_lsa *ospf_lsa_refresh(struct ospf *, struct ospf_lsa *);
301 
302 extern void ospf_external_lsa_refresh_default(struct ospf *);
303 
304 extern void ospf_external_lsa_refresh_type(struct ospf *, uint8_t,
305 					   unsigned short, int);
306 extern struct ospf_lsa *ospf_external_lsa_refresh(struct ospf *,
307 						  struct ospf_lsa *,
308 						  struct external_info *, int);
309 extern struct in_addr ospf_lsa_unique_id(struct ospf *, struct ospf_lsdb *,
310 					 uint8_t, struct prefix_ipv4 *);
311 extern void ospf_schedule_lsa_flood_area(struct ospf_area *, struct ospf_lsa *);
312 extern void ospf_schedule_lsa_flush_area(struct ospf_area *, struct ospf_lsa *);
313 
314 extern void ospf_refresher_register_lsa(struct ospf *, struct ospf_lsa *);
315 extern void ospf_refresher_unregister_lsa(struct ospf *, struct ospf_lsa *);
316 extern int ospf_lsa_refresh_walker(struct thread *);
317 
318 extern void ospf_lsa_maxage_delete(struct ospf *, struct ospf_lsa *);
319 
320 extern void ospf_discard_from_db(struct ospf *, struct ospf_lsdb *,
321 				 struct ospf_lsa *);
322 extern int is_prefix_default(struct prefix_ipv4 *);
323 
324 extern int metric_type(struct ospf *, uint8_t, unsigned short);
325 extern int metric_value(struct ospf *, uint8_t, unsigned short);
326 
327 extern struct in_addr ospf_get_nssa_ip(struct ospf_area *);
328 extern int ospf_translated_nssa_compare(struct ospf_lsa *, struct ospf_lsa *);
329 extern struct ospf_lsa *ospf_translated_nssa_refresh(struct ospf *,
330 						     struct ospf_lsa *,
331 						     struct ospf_lsa *);
332 extern struct ospf_lsa *ospf_translated_nssa_originate(struct ospf *,
333 						       struct ospf_lsa *);
334 extern void ospf_flush_lsa_from_area(struct ospf *ospf, struct in_addr area_id,
335 				     int type);
336 #endif /* _ZEBRA_OSPF_LSA_H */
337