1 |
|
|
/* $OpenBSD: packet.c,v 1.5 2015/12/07 19:14:49 mmcc Exp $ */ |
2 |
|
|
|
3 |
|
|
/* |
4 |
|
|
* Copyright (c) 2004, 2005, 2006 Esben Norby <norby@openbsd.org> |
5 |
|
|
* |
6 |
|
|
* Permission to use, copy, modify, and distribute this software for any |
7 |
|
|
* purpose with or without fee is hereby granted, provided that the above |
8 |
|
|
* copyright notice and this permission notice appear in all copies. |
9 |
|
|
* |
10 |
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
11 |
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
12 |
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
13 |
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
14 |
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
15 |
|
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
16 |
|
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
17 |
|
|
*/ |
18 |
|
|
|
19 |
|
|
#include <sys/types.h> |
20 |
|
|
#include <sys/socket.h> |
21 |
|
|
#include <sys/time.h> |
22 |
|
|
|
23 |
|
|
#include <netinet/in.h> |
24 |
|
|
#include <netinet/ip.h> |
25 |
|
|
#include <netinet/ip_mroute.h> |
26 |
|
|
#include <arpa/inet.h> |
27 |
|
|
|
28 |
|
|
#include <errno.h> |
29 |
|
|
#include <event.h> |
30 |
|
|
#include <stdlib.h> |
31 |
|
|
#include <string.h> |
32 |
|
|
|
33 |
|
|
#include "igmp.h" |
34 |
|
|
#include "dvmrpd.h" |
35 |
|
|
#include "dvmrp.h" |
36 |
|
|
#include "log.h" |
37 |
|
|
#include "dvmrpe.h" |
38 |
|
|
|
39 |
|
|
int ip_hdr_sanity_check(const struct ip *, u_int16_t); |
40 |
|
|
int dvmrp_hdr_sanity_check(const struct ip *, struct dvmrp_hdr *, |
41 |
|
|
u_int16_t, const struct iface *); |
42 |
|
|
struct iface *find_iface(struct dvmrpd_conf *, struct in_addr); |
43 |
|
|
|
44 |
|
|
extern struct dvmrpd_conf *deconf; |
45 |
|
|
|
46 |
|
|
int |
47 |
|
|
gen_dvmrp_hdr(struct ibuf *buf, struct iface *iface, u_int8_t code) |
48 |
|
|
{ |
49 |
|
|
struct dvmrp_hdr dvmrp_hdr; |
50 |
|
|
|
51 |
|
|
memset(&dvmrp_hdr, 0, sizeof(dvmrp_hdr)); |
52 |
|
|
dvmrp_hdr.type = PKT_TYPE_DVMRP; |
53 |
|
|
dvmrp_hdr.code = code; |
54 |
|
|
dvmrp_hdr.chksum = 0; /* updated later */ |
55 |
|
|
dvmrp_hdr.capabilities = DVMRP_CAP_DEFAULT; /* XXX update */ |
56 |
|
|
dvmrp_hdr.minor_version = DVMRP_MINOR_VERSION; |
57 |
|
|
dvmrp_hdr.major_version = DVMRP_MAJOR_VERSION; |
58 |
|
|
|
59 |
|
|
return (ibuf_add(buf, &dvmrp_hdr, sizeof(dvmrp_hdr))); |
60 |
|
|
} |
61 |
|
|
|
62 |
|
|
/* send and receive packets */ |
63 |
|
|
int |
64 |
|
|
send_packet(struct iface *iface, void *pkt, size_t len, struct sockaddr_in *dst) |
65 |
|
|
{ |
66 |
|
|
if (iface->passive) { |
67 |
|
|
log_warnx("send_packet: cannot send packet on passive " |
68 |
|
|
"interface %s", iface->name); |
69 |
|
|
return (-1); |
70 |
|
|
} |
71 |
|
|
|
72 |
|
|
/* set outgoing interface for multicast traffic */ |
73 |
|
|
if (IN_MULTICAST(ntohl(dst->sin_addr.s_addr))) |
74 |
|
|
if (if_set_mcast(iface) == -1) { |
75 |
|
|
log_warn("send_packet: error setting multicast " |
76 |
|
|
"interface, %s", iface->name); |
77 |
|
|
return (-1); |
78 |
|
|
} |
79 |
|
|
|
80 |
|
|
if (sendto(iface->fd, pkt, len, 0, |
81 |
|
|
(struct sockaddr *)dst, sizeof(*dst)) == -1 ) { |
82 |
|
|
log_warn("send_packet: error sending packet on interface %s", |
83 |
|
|
iface->name); |
84 |
|
|
return (-1); |
85 |
|
|
} |
86 |
|
|
|
87 |
|
|
return (0); |
88 |
|
|
} |
89 |
|
|
|
90 |
|
|
void |
91 |
|
|
recv_packet(int fd, short event, void *bula) |
92 |
|
|
{ |
93 |
|
|
struct dvmrpd_conf *xconf = bula; |
94 |
|
|
struct ip ip_hdr; |
95 |
|
|
struct dvmrp_hdr *dvmrp_hdr; |
96 |
|
|
struct iface *iface; |
97 |
|
|
struct nbr *nbr = NULL; |
98 |
|
|
struct in_addr addr; |
99 |
|
|
char *buf; |
100 |
|
|
ssize_t r; |
101 |
|
|
u_int16_t len; |
102 |
|
|
int l; |
103 |
|
|
|
104 |
|
|
if (event != EV_READ) |
105 |
|
|
return; |
106 |
|
|
|
107 |
|
|
/* setup buffer */ |
108 |
|
|
buf = pkt_ptr; |
109 |
|
|
|
110 |
|
|
if ((r = recvfrom(fd, buf, IBUF_READ_SIZE, 0, NULL, NULL)) == -1) { |
111 |
|
|
if (errno != EAGAIN && errno != EINTR) |
112 |
|
|
log_debug("recv_packet: error receiving packet"); |
113 |
|
|
return; |
114 |
|
|
} |
115 |
|
|
|
116 |
|
|
len = (u_int16_t)r; |
117 |
|
|
|
118 |
|
|
/* IP header sanity checks */ |
119 |
|
|
if (len < sizeof(ip_hdr)) { |
120 |
|
|
log_warnx("recv_packet: bad packet size"); |
121 |
|
|
return; |
122 |
|
|
} |
123 |
|
|
|
124 |
|
|
memcpy(&ip_hdr, buf, sizeof(ip_hdr)); |
125 |
|
|
if ((l = ip_hdr_sanity_check(&ip_hdr, len)) == -1) |
126 |
|
|
return; |
127 |
|
|
buf += l; |
128 |
|
|
len -= l; |
129 |
|
|
|
130 |
|
|
/* find a matching interface */ |
131 |
|
|
if ((iface = find_iface(xconf, ip_hdr.ip_src)) == NULL) { |
132 |
|
|
log_debug("recv_packet: cannot find valid interface, ip src %s", |
133 |
|
|
inet_ntoa(ip_hdr.ip_src)); |
134 |
|
|
return; |
135 |
|
|
} |
136 |
|
|
|
137 |
|
|
/* header sanity checks */ |
138 |
|
|
if (len < sizeof(*dvmrp_hdr)) { |
139 |
|
|
log_warnx("recv_packet: bad packet size"); |
140 |
|
|
return; |
141 |
|
|
} |
142 |
|
|
dvmrp_hdr = (struct dvmrp_hdr *)buf; |
143 |
|
|
|
144 |
|
|
switch (dvmrp_hdr->type) { |
145 |
|
|
/* DVMRP */ |
146 |
|
|
case PKT_TYPE_DVMRP: |
147 |
|
|
if ((l = dvmrp_hdr_sanity_check(&ip_hdr, dvmrp_hdr, len, |
148 |
|
|
iface)) == -1) |
149 |
|
|
return; |
150 |
|
|
|
151 |
|
|
/* |
152 |
|
|
* mrouted compat |
153 |
|
|
* |
154 |
|
|
* Old mrouted versions, send route reports before establishing |
155 |
|
|
* 2-WAY neighbor relationships. |
156 |
|
|
*/ |
157 |
|
|
if ((nbr_find_ip(iface, ip_hdr.ip_src.s_addr) == NULL) && |
158 |
|
|
(dvmrp_hdr->code == DVMRP_CODE_REPORT)) { |
159 |
|
|
log_debug("recv_packet: route report from neighbor" |
160 |
|
|
" ID %s, compat", inet_ntoa(ip_hdr.ip_src)); |
161 |
|
|
nbr = nbr_new(ip_hdr.ip_src.s_addr, iface, 0); |
162 |
|
|
nbr_fsm(nbr, NBR_EVT_PROBE_RCVD); |
163 |
|
|
nbr->compat = 1; |
164 |
|
|
nbr->addr = ip_hdr.ip_src; |
165 |
|
|
} |
166 |
|
|
|
167 |
|
|
if ((dvmrp_hdr->type == PKT_TYPE_DVMRP) && |
168 |
|
|
(dvmrp_hdr->code != DVMRP_CODE_PROBE)) |
169 |
|
|
/* find neighbor */ |
170 |
|
|
if ((nbr = nbr_find_ip(iface, ip_hdr.ip_src.s_addr)) |
171 |
|
|
== NULL) { |
172 |
|
|
log_debug("recv_packet: unknown neighbor ID"); |
173 |
|
|
return; |
174 |
|
|
} |
175 |
|
|
|
176 |
|
|
buf += sizeof(*dvmrp_hdr); |
177 |
|
|
len = l - sizeof(*dvmrp_hdr); |
178 |
|
|
|
179 |
|
|
inet_aton(AllDVMRPRouters, &addr); |
180 |
|
|
if ((ip_hdr.ip_dst.s_addr != addr.s_addr) && |
181 |
|
|
(ip_hdr.ip_dst.s_addr != iface->addr.s_addr)) { |
182 |
|
|
log_debug("recv_packet: interface %s, invalid" |
183 |
|
|
" destination IP address %s", iface->name, |
184 |
|
|
inet_ntoa(ip_hdr.ip_dst)); |
185 |
|
|
break; |
186 |
|
|
} |
187 |
|
|
|
188 |
|
|
switch (dvmrp_hdr->code) { |
189 |
|
|
case DVMRP_CODE_PROBE: |
190 |
|
|
recv_probe(iface, ip_hdr.ip_src, ip_hdr.ip_src.s_addr, |
191 |
|
|
dvmrp_hdr->capabilities, buf, len); |
192 |
|
|
break; |
193 |
|
|
case DVMRP_CODE_REPORT: |
194 |
|
|
recv_report(nbr, buf, len); |
195 |
|
|
break; |
196 |
|
|
case DVMRP_CODE_ASK_NBRS2: |
197 |
|
|
recv_ask_nbrs2(nbr, buf,len); |
198 |
|
|
break; |
199 |
|
|
case DVMRP_CODE_NBRS2: |
200 |
|
|
recv_nbrs2(nbr, buf,len); |
201 |
|
|
break; |
202 |
|
|
case DVMRP_CODE_PRUNE: |
203 |
|
|
recv_prune(nbr, buf, len); |
204 |
|
|
break; |
205 |
|
|
case DVMRP_CODE_GRAFT: |
206 |
|
|
recv_graft(nbr, buf,len); |
207 |
|
|
break; |
208 |
|
|
case DVMRP_CODE_GRAFT_ACK: |
209 |
|
|
recv_graft_ack(nbr, buf,len); |
210 |
|
|
break; |
211 |
|
|
default: |
212 |
|
|
log_debug("recv_packet: unknown DVMRP packet type, " |
213 |
|
|
"interface %s", iface->name); |
214 |
|
|
} |
215 |
|
|
break; |
216 |
|
|
/* IGMP */ |
217 |
|
|
case PKT_TYPE_MEMBER_QUERY: |
218 |
|
|
recv_igmp_query(iface, ip_hdr.ip_src, buf, len); |
219 |
|
|
break; |
220 |
|
|
case PKT_TYPE_MEMBER_REPORTv1: |
221 |
|
|
case PKT_TYPE_MEMBER_REPORTv2: |
222 |
|
|
recv_igmp_report(iface, ip_hdr.ip_src, buf, len, |
223 |
|
|
dvmrp_hdr->type); |
224 |
|
|
break; |
225 |
|
|
case PKT_TYPE_LEAVE_GROUPv2: |
226 |
|
|
recv_igmp_leave(iface, ip_hdr.ip_src, buf, len); |
227 |
|
|
break; |
228 |
|
|
default: |
229 |
|
|
log_debug("recv_packet: unknown IGMP packet type, interface %s", |
230 |
|
|
iface->name); |
231 |
|
|
} |
232 |
|
|
} |
233 |
|
|
|
234 |
|
|
int |
235 |
|
|
ip_hdr_sanity_check(const struct ip *ip_hdr, u_int16_t len) |
236 |
|
|
{ |
237 |
|
|
if (ntohs(ip_hdr->ip_len) != len) { |
238 |
|
|
log_debug("recv_packet: invalid IP packet length %u", |
239 |
|
|
ntohs(ip_hdr->ip_len)); |
240 |
|
|
return (-1); |
241 |
|
|
} |
242 |
|
|
|
243 |
|
|
if (ip_hdr->ip_p != IPPROTO_IGMP) |
244 |
|
|
/* this is enforced by the socket itself */ |
245 |
|
|
fatalx("recv_packet: invalid IP proto"); |
246 |
|
|
|
247 |
|
|
return (ip_hdr->ip_hl << 2); |
248 |
|
|
} |
249 |
|
|
|
250 |
|
|
int |
251 |
|
|
dvmrp_hdr_sanity_check(const struct ip *ip_hdr, struct dvmrp_hdr *dvmrp_hdr, |
252 |
|
|
u_int16_t len, const struct iface *iface) |
253 |
|
|
{ |
254 |
|
|
/* we only support DVMRPv3 */ |
255 |
|
|
if (dvmrp_hdr->major_version != DVMRP_MAJOR_VERSION) { |
256 |
|
|
log_debug("recv_packet: invalid DVMRP version"); |
257 |
|
|
return (-1); |
258 |
|
|
} |
259 |
|
|
|
260 |
|
|
/* XXX enforce minor version as well, but not yet */ |
261 |
|
|
|
262 |
|
|
/* XXX chksum */ |
263 |
|
|
|
264 |
|
|
return (len); |
265 |
|
|
} |
266 |
|
|
|
267 |
|
|
struct iface * |
268 |
|
|
find_iface(struct dvmrpd_conf *xconf, struct in_addr src) |
269 |
|
|
{ |
270 |
|
|
struct iface *iface = NULL; |
271 |
|
|
|
272 |
|
|
/* returned interface needs to be active */ |
273 |
|
|
LIST_FOREACH(iface, &xconf->iface_list, entry) { |
274 |
|
|
if (iface->fd > 0 && |
275 |
|
|
(iface->type == IF_TYPE_POINTOPOINT) && |
276 |
|
|
(iface->dst.s_addr == src.s_addr) && |
277 |
|
|
!iface->passive) |
278 |
|
|
return (iface); |
279 |
|
|
|
280 |
|
|
if (iface->fd > 0 && (iface->addr.s_addr & |
281 |
|
|
iface->mask.s_addr) == (src.s_addr & |
282 |
|
|
iface->mask.s_addr) && !iface->passive) |
283 |
|
|
return (iface); |
284 |
|
|
} |
285 |
|
|
|
286 |
|
|
return (NULL); |
287 |
|
|
} |