]>
Commit | Line | Data |
---|---|---|
12e41d03 DL |
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 | |
16 | along with this program; see the file COPYING; if not, write to the | |
17 | Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, | |
18 | MA 02110-1301 USA | |
19 | ||
12e41d03 DL |
20 | */ |
21 | ||
22 | #include <zebra.h> | |
23 | ||
744d91b3 | 24 | #include "if.h" |
346cffe3 | 25 | #include "log.h" |
744d91b3 | 26 | |
12e41d03 | 27 | #include "pimd.h" |
75a26779 | 28 | #include "pim_vty.h" |
12e41d03 DL |
29 | #include "pim_pim.h" |
30 | #include "pim_msg.h" | |
31 | #include "pim_util.h" | |
346cffe3 | 32 | #include "pim_str.h" |
75a26779 | 33 | #include "pim_rp.h" |
12e41d03 DL |
34 | |
35 | void pim_msg_build_header(uint8_t *pim_msg, int pim_msg_size, | |
36 | uint8_t pim_msg_type) | |
37 | { | |
38 | uint16_t checksum; | |
39 | ||
40 | zassert(pim_msg_size >= PIM_PIM_MIN_LEN); | |
41 | ||
42 | /* | |
43 | * Write header | |
44 | */ | |
45 | ||
46 | *(uint8_t *) PIM_MSG_HDR_OFFSET_VERSION(pim_msg) = (PIM_PROTO_VERSION << 4) | pim_msg_type; | |
47 | *(uint8_t *) PIM_MSG_HDR_OFFSET_RESERVED(pim_msg) = 0; | |
48 | ||
49 | /* | |
50 | * Compute checksum | |
51 | */ | |
52 | ||
53 | *(uint16_t *) PIM_MSG_HDR_OFFSET_CHECKSUM(pim_msg) = 0; | |
54 | checksum = in_cksum(pim_msg, pim_msg_size); | |
55 | *(uint16_t *) PIM_MSG_HDR_OFFSET_CHECKSUM(pim_msg) = checksum; | |
56 | } | |
57 | ||
58 | uint8_t *pim_msg_addr_encode_ipv4_ucast(uint8_t *buf, | |
59 | int buf_size, | |
60 | struct in_addr addr) | |
61 | { | |
62 | const int ENCODED_IPV4_UCAST_SIZE = 6; | |
63 | ||
64 | if (buf_size < ENCODED_IPV4_UCAST_SIZE) { | |
65 | return 0; | |
66 | } | |
67 | ||
68 | buf[0] = PIM_MSG_ADDRESS_FAMILY_IPV4; /* addr family */ | |
69 | buf[1] = '\0'; /* native encoding */ | |
70 | memcpy(buf+2, &addr, sizeof(struct in_addr)); | |
71 | ||
72 | return buf + ENCODED_IPV4_UCAST_SIZE; | |
73 | } | |
74 | ||
75 | uint8_t *pim_msg_addr_encode_ipv4_group(uint8_t *buf, | |
76 | int buf_size, | |
77 | struct in_addr addr) | |
78 | { | |
79 | const int ENCODED_IPV4_GROUP_SIZE = 8; | |
80 | ||
81 | if (buf_size < ENCODED_IPV4_GROUP_SIZE) { | |
82 | return 0; | |
83 | } | |
84 | ||
85 | buf[0] = PIM_MSG_ADDRESS_FAMILY_IPV4; /* addr family */ | |
86 | buf[1] = '\0'; /* native encoding */ | |
87 | buf[2] = '\0'; /* reserved */ | |
88 | buf[3] = 32; /* mask len */ | |
89 | memcpy(buf+4, &addr, sizeof(struct in_addr)); | |
90 | ||
91 | return buf + ENCODED_IPV4_GROUP_SIZE; | |
92 | } | |
93 | ||
984c84f4 DS |
94 | uint8_t * |
95 | pim_msg_addr_encode_ipv4_source(uint8_t *buf, int buf_size, | |
96 | struct in_addr addr, uint8_t bits) | |
12e41d03 DL |
97 | { |
98 | const int ENCODED_IPV4_SOURCE_SIZE = 8; | |
99 | ||
100 | if (buf_size < ENCODED_IPV4_SOURCE_SIZE) { | |
101 | return 0; | |
102 | } | |
103 | ||
104 | buf[0] = PIM_MSG_ADDRESS_FAMILY_IPV4; /* addr family */ | |
105 | buf[1] = '\0'; /* native encoding */ | |
984c84f4 | 106 | buf[2] = bits; |
12e41d03 DL |
107 | buf[3] = 32; /* mask len */ |
108 | memcpy(buf+4, &addr, sizeof(struct in_addr)); | |
109 | ||
110 | return buf + ENCODED_IPV4_SOURCE_SIZE; | |
111 | } | |
346cffe3 DS |
112 | |
113 | int | |
114 | pim_msg_join_prune_encode (uint8_t *buf, int buf_size, int is_join, | |
115 | struct in_addr source, struct in_addr group, | |
116 | struct in_addr upstream, int holdtime) | |
117 | { | |
118 | uint8_t *pim_msg = buf; | |
119 | uint8_t *pim_msg_curr = buf + PIM_MSG_HEADER_LEN; | |
120 | uint8_t *end = buf + buf_size; | |
121 | struct in_addr stosend; | |
122 | uint8_t bits; | |
123 | int remain; | |
124 | ||
125 | remain = end - pim_msg_curr; | |
126 | pim_msg_curr = pim_msg_addr_encode_ipv4_ucast (pim_msg_curr, buf_size - PIM_MSG_HEADER_LEN, upstream); | |
127 | if (!pim_msg_curr) { | |
128 | char dst_str[100]; | |
129 | pim_inet4_dump("<dst?>", upstream, dst_str, sizeof(dst_str)); | |
130 | zlog_warn("%s: failure encoding destination address %s: space left=%d", | |
131 | __PRETTY_FUNCTION__, dst_str, remain); | |
132 | return -3; | |
133 | } | |
134 | ||
135 | remain = end - pim_msg_curr; | |
136 | if (remain < 4) { | |
137 | zlog_warn("%s: group will not fit: space left=%d", | |
138 | __PRETTY_FUNCTION__, remain); | |
139 | return -4; | |
140 | } | |
141 | ||
142 | *pim_msg_curr = 0; /* reserved */ | |
143 | ++pim_msg_curr; | |
144 | *pim_msg_curr = 1; /* number of groups */ | |
145 | ++pim_msg_curr; | |
146 | ||
147 | *((uint16_t *) pim_msg_curr) = htons(holdtime); | |
148 | ++pim_msg_curr; | |
149 | ++pim_msg_curr; | |
150 | ||
151 | remain = end - pim_msg_curr; | |
152 | pim_msg_curr = pim_msg_addr_encode_ipv4_group (pim_msg_curr, remain, | |
153 | group); | |
154 | if (!pim_msg_curr) { | |
155 | char group_str[100]; | |
156 | pim_inet4_dump("<grp?>", group, group_str, sizeof(group_str)); | |
157 | zlog_warn("%s: failure encoding group address %s: space left=%d", | |
158 | __PRETTY_FUNCTION__, group_str, remain); | |
159 | return -5; | |
160 | } | |
161 | ||
162 | remain = end - pim_msg_curr; | |
163 | if (remain < 4) { | |
164 | zlog_warn("%s: sources will not fit: space left=%d", | |
165 | __PRETTY_FUNCTION__, remain); | |
166 | return -6; | |
167 | } | |
168 | ||
169 | /* number of joined sources */ | |
170 | *((uint16_t *) pim_msg_curr) = htons(is_join ? 1 : 0); | |
171 | ++pim_msg_curr; | |
172 | ++pim_msg_curr; | |
173 | ||
174 | /* number of pruned sources */ | |
175 | *((uint16_t *) pim_msg_curr) = htons(is_join ? 0 : 1); | |
176 | ++pim_msg_curr; | |
177 | ++pim_msg_curr; | |
178 | ||
179 | remain = end - pim_msg_curr; | |
180 | if (source.s_addr == INADDR_ANY) | |
181 | { | |
75a26779 | 182 | struct pim_rpf *rpf = pim_rp_g (group); |
346cffe3 | 183 | bits = PIM_ENCODE_SPARSE_BIT | PIM_ENCODE_WC_BIT | PIM_ENCODE_RPT_BIT; |
75a26779 | 184 | stosend = rpf->rpf_addr; |
346cffe3 DS |
185 | } |
186 | else | |
187 | { | |
188 | bits = PIM_ENCODE_SPARSE_BIT; | |
189 | stosend = source; | |
190 | } | |
191 | pim_msg_curr = pim_msg_addr_encode_ipv4_source (pim_msg_curr, remain, stosend, bits); | |
192 | if (!pim_msg_curr) { | |
193 | char source_str[100]; | |
194 | pim_inet4_dump("<src?>", source, source_str, sizeof(source_str)); | |
195 | zlog_warn("%s: failure encoding source address %s: space left=%d", | |
196 | __PRETTY_FUNCTION__, source_str, remain); | |
197 | return -7; | |
198 | } | |
199 | ||
200 | remain = pim_msg_curr - pim_msg; | |
201 | pim_msg_build_header (pim_msg, remain, PIM_MSG_TYPE_JOIN_PRUNE); | |
202 | ||
203 | return remain; | |
204 | } |