]>
git.proxmox.com Git - mirror_frr.git/blob - ripngd/ripng_nexthop.c
2 * Copyright (C) 2002 6WIND <vincent.jardin@6wind.com>
4 * This file is part of GNU Zebra.
6 * GNU Zebra is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2, or (at your option) any
11 * GNU Zebra is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; see the file COPYING; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 /* This file is required in order to support properly the RIPng nexthop
27 /* For struct udphdr. */
28 #include <netinet/udp.h>
38 #include "ripngd/ripngd.h"
39 #include "ripngd/ripng_debug.h"
40 #include "ripngd/ripng_nexthop.h"
42 DEFINE_MTYPE_STATIC(RIPNGD
, RIPNG_RTE_DATA
, "RIPng rte data")
46 #define min(a, b) ((a) < (b) ? (a) : (b))
48 struct ripng_rte_data
{
49 struct prefix_ipv6
*p
;
50 struct ripng_info
*rinfo
;
51 struct ripng_aggregate
*aggregate
;
54 void _ripng_rte_del(struct ripng_rte_data
*A
);
55 int _ripng_rte_cmp(struct ripng_rte_data
*A
, struct ripng_rte_data
*B
);
57 #define METRIC_OUT(a) \
58 ((a)->rinfo ? (a)->rinfo->metric_out : (a)->aggregate->metric_out)
59 #define NEXTHOP_OUT_PTR(a) \
60 ((a)->rinfo ? &((a)->rinfo->nexthop_out) \
61 : &((a)->aggregate->nexthop_out))
62 #define TAG_OUT(a) ((a)->rinfo ? (a)->rinfo->tag_out : (a)->aggregate->tag_out)
64 struct list
*ripng_rte_new(void)
69 rte
->cmp
= (int (*)(void *, void *))_ripng_rte_cmp
;
70 rte
->del
= (void (*)(void *))_ripng_rte_del
;
75 void ripng_rte_free(struct list
*ripng_rte_list
)
77 list_delete(&ripng_rte_list
);
81 void _ripng_rte_del(struct ripng_rte_data
*A
)
83 XFREE(MTYPE_RIPNG_RTE_DATA
, A
);
91 int _ripng_rte_cmp(struct ripng_rte_data
*A
, struct ripng_rte_data
*B
)
93 return addr6_cmp(NEXTHOP_OUT_PTR(A
), NEXTHOP_OUT_PTR(B
));
96 /* Add routing table entry */
97 void ripng_rte_add(struct list
*ripng_rte_list
, struct prefix_ipv6
*p
,
98 struct ripng_info
*rinfo
, struct ripng_aggregate
*aggregate
)
101 struct ripng_rte_data
*data
;
103 /* At least one should not be null */
104 assert(!rinfo
|| !aggregate
);
106 data
= XMALLOC(MTYPE_RIPNG_RTE_DATA
, sizeof(*data
));
109 data
->aggregate
= aggregate
;
111 listnode_add_sort(ripng_rte_list
, data
);
114 /* Send the RTE with the nexthop support
116 void ripng_rte_send(struct list
*ripng_rte_list
, struct interface
*ifp
,
117 struct sockaddr_in6
*to
)
119 struct ripng_interface
*ri
= ifp
->info
;
120 struct ripng
*ripng
= ri
->ripng
;
121 struct ripng_rte_data
*data
;
122 struct listnode
*node
, *nnode
;
124 struct in6_addr last_nexthop
;
125 struct in6_addr myself_nexthop
;
133 /* Most of the time, there is no nexthop */
134 memset(&last_nexthop
, 0, sizeof(last_nexthop
));
136 /* Use myself_nexthop if the nexthop is not a link-local address,
138 * we remain a right path without beeing the optimal one.
140 memset(&myself_nexthop
, 0, sizeof(myself_nexthop
));
142 /* Output stream get from ripng structre. XXX this should be
143 interface structure. */
146 /* Reset stream and RTE counter. */
154 rtemax
= (min(mtu
, RIPNG_MAX_PACKET_SIZE
) - IPV6_HDRLEN
155 - sizeof(struct udphdr
) - sizeof(struct ripng_packet
)
156 + sizeof(struct rte
))
157 / sizeof(struct rte
);
159 for (ALL_LIST_ELEMENTS(ripng_rte_list
, node
, nnode
, data
)) {
160 /* (2.1) Next hop support */
161 if (!IPV6_ADDR_SAME(&last_nexthop
, NEXTHOP_OUT_PTR(data
))) {
163 /* A nexthop entry should be at least followed by 1 RTE
165 if (num
== (rtemax
- 1)) {
166 ret
= ripng_send_packet((caddr_t
)STREAM_DATA(s
),
167 stream_get_endp(s
), to
,
170 if (ret
>= 0 && IS_RIPNG_DEBUG_SEND
)
172 (struct ripng_packet
*)
174 stream_get_endp(s
), "SEND");
179 /* Add the nexthop (2.1) */
181 /* If the received next hop address is not a link-local
183 * it should be treated as 0:0:0:0:0:0:0:0.
185 if (!IN6_IS_ADDR_LINKLOCAL(NEXTHOP_OUT_PTR(data
)))
186 last_nexthop
= myself_nexthop
;
188 last_nexthop
= *NEXTHOP_OUT_PTR(data
);
190 num
= ripng_write_rte(num
, s
, NULL
, &last_nexthop
, 0,
191 RIPNG_METRIC_NEXTHOP
);
193 /* Rewrite the nexthop for each new packet */
195 && !IPV6_ADDR_SAME(&last_nexthop
, &myself_nexthop
))
196 num
= ripng_write_rte(num
, s
, NULL
,
198 RIPNG_METRIC_NEXTHOP
);
200 num
= ripng_write_rte(num
, s
, data
->p
, NULL
, TAG_OUT(data
),
204 ret
= ripng_send_packet((caddr_t
)STREAM_DATA(s
),
205 stream_get_endp(s
), to
, ifp
);
207 if (ret
>= 0 && IS_RIPNG_DEBUG_SEND
)
209 (struct ripng_packet
*)STREAM_DATA(s
),
210 stream_get_endp(s
), "SEND");
216 /* If unwritten RTE exist, flush it. */
218 ret
= ripng_send_packet((caddr_t
)STREAM_DATA(s
),
219 stream_get_endp(s
), to
, ifp
);
221 if (ret
>= 0 && IS_RIPNG_DEBUG_SEND
)
222 ripng_packet_dump((struct ripng_packet
*)STREAM_DATA(s
),
223 stream_get_endp(s
), "SEND");