]> git.proxmox.com Git - mirror_frr.git/blob - zebra/irdp_packet.c
Merge pull request #2858 from Jafaral/sphinx
[mirror_frr.git] / zebra / irdp_packet.c
1 /*
2 *
3 * Copyright (C) 2000 Robert Olsson.
4 * Swedish University of Agricultural Sciences
5 *
6 * This file is part of GNU Zebra.
7 *
8 * GNU Zebra is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2, or (at your option) any
11 * later version.
12 *
13 * GNU Zebra is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; see the file COPYING; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23 /*
24 * This work includes work with the following copywrite:
25 *
26 * Copyright (C) 1997, 2000 Kunihiro Ishiguro
27 *
28 */
29
30 /*
31 * Thanks to Jens Laas at Swedish University of Agricultural Sciences
32 * for reviewing and tests.
33 */
34
35
36 #include <zebra.h>
37 #include <netinet/ip_icmp.h>
38
39 #include "checksum.h"
40 #include "command.h"
41 #include "connected.h"
42 #include "if.h"
43 #include "ioctl.h"
44 #include "log.h"
45 #include "log.h"
46 #include "memory.h"
47 #include "prefix.h"
48 #include "sockopt.h"
49 #include "sockunion.h"
50 #include "sockunion.h"
51 #include "stream.h"
52 #include "thread.h"
53 #include "vty.h"
54 #include "zclient.h"
55
56 #include "zebra_memory.h"
57 #include "zebra/interface.h"
58 #include "zebra/rtadv.h"
59 #include "zebra/rib.h"
60 #include "zebra/zserv.h"
61 #include "zebra/redistribute.h"
62 #include "zebra/irdp.h"
63 #include "zebra/zebra_errors.h"
64
65
66 /* GLOBAL VARS */
67
68 int irdp_sock = -1;
69
70 extern struct thread *t_irdp_raw;
71
72 static void parse_irdp_packet(char *p, int len, struct interface *ifp)
73 {
74 struct ip *ip = (struct ip *)p;
75 struct icmphdr *icmp;
76 struct in_addr src;
77 int ip_hlen, iplen, datalen;
78 struct zebra_if *zi;
79 struct irdp_interface *irdp;
80
81 zi = ifp->info;
82 if (!zi)
83 return;
84
85 irdp = zi->irdp;
86 if (!irdp)
87 return;
88
89 ip_hlen = ip->ip_hl << 2;
90
91 sockopt_iphdrincl_swab_systoh(ip);
92
93 iplen = ip->ip_len;
94 datalen = len - ip_hlen;
95 src = ip->ip_src;
96
97 if (len != iplen) {
98 flog_err(ZEBRA_ERR_IRDP_LEN_MISMATCH,
99 "IRDP: RX length doesnt match IP length");
100 return;
101 }
102
103 if (iplen < ICMP_MINLEN) {
104 flog_err(ZEBRA_ERR_IRDP_LEN_MISMATCH,
105 "IRDP: RX ICMP packet too short from %s\n",
106 inet_ntoa(src));
107 return;
108 }
109
110 /* XXX: RAW doesnt receive link-layer, surely? ??? */
111 /* Check so we don't checksum packets longer than oure RX_BUF - (ethlen
112 +
113 len of IP-header) 14+20 */
114 if (iplen > IRDP_RX_BUF - 34) {
115 flog_err(ZEBRA_ERR_IRDP_LEN_MISMATCH,
116 "IRDP: RX ICMP packet too long from %s\n",
117 inet_ntoa(src));
118 return;
119 }
120
121 icmp = (struct icmphdr *)(p + ip_hlen);
122
123 /* check icmp checksum */
124 if (in_cksum(icmp, datalen) != icmp->checksum) {
125 zlog_warn(
126 "IRDP: RX ICMP packet from %s. Bad checksum, silently ignored",
127 inet_ntoa(src));
128 return;
129 }
130
131 /* Handle just only IRDP */
132 if (!(icmp->type == ICMP_ROUTERADVERT
133 || icmp->type == ICMP_ROUTERSOLICIT))
134 return;
135
136 if (icmp->code != 0) {
137 zlog_warn(
138 "IRDP: RX packet type %d from %s. Bad ICMP type code,"
139 " silently ignored",
140 icmp->type, inet_ntoa(src));
141 return;
142 }
143
144 if (!((ntohl(ip->ip_dst.s_addr) == INADDR_BROADCAST)
145 && (irdp->flags & IF_BROADCAST))
146 || (ntohl(ip->ip_dst.s_addr) == INADDR_ALLRTRS_GROUP
147 && !(irdp->flags & IF_BROADCAST))) {
148 zlog_warn(
149 "IRDP: RX illegal from %s to %s while %s operates in %s\n",
150 inet_ntoa(src),
151 ntohl(ip->ip_dst.s_addr) == INADDR_ALLRTRS_GROUP
152 ? "multicast"
153 : inet_ntoa(ip->ip_dst),
154 ifp->name,
155 irdp->flags & IF_BROADCAST ? "broadcast" : "multicast");
156
157 zlog_warn("IRDP: Please correct settings\n");
158 return;
159 }
160
161 switch (icmp->type) {
162 case ICMP_ROUTERADVERT:
163 break;
164
165 case ICMP_ROUTERSOLICIT:
166
167 if (irdp->flags & IF_DEBUG_MESSAGES)
168 zlog_debug("IRDP: RX Solicit on %s from %s\n",
169 ifp->name, inet_ntoa(src));
170
171 process_solicit(ifp);
172 break;
173
174 default:
175 zlog_warn(
176 "IRDP: RX type %d from %s. Bad ICMP type, silently ignored",
177 icmp->type, inet_ntoa(src));
178 }
179 }
180
181 static int irdp_recvmsg(int sock, uint8_t *buf, int size, int *ifindex)
182 {
183 struct msghdr msg;
184 struct iovec iov;
185 char adata[CMSG_SPACE(SOPT_SIZE_CMSG_PKTINFO_IPV4())];
186 int ret;
187
188 memset(&msg, 0, sizeof(msg));
189 msg.msg_name = (void *)0;
190 msg.msg_namelen = 0;
191 msg.msg_iov = &iov;
192 msg.msg_iovlen = 1;
193 msg.msg_control = (void *)adata;
194 msg.msg_controllen = sizeof adata;
195
196 iov.iov_base = buf;
197 iov.iov_len = size;
198
199 ret = recvmsg(sock, &msg, 0);
200 if (ret < 0) {
201 zlog_warn("IRDP: recvmsg: read error %s", safe_strerror(errno));
202 return ret;
203 }
204
205 if (msg.msg_flags & MSG_TRUNC) {
206 zlog_warn("IRDP: recvmsg: truncated message");
207 return ret;
208 }
209 if (msg.msg_flags & MSG_CTRUNC) {
210 zlog_warn("IRDP: recvmsg: truncated control message");
211 return ret;
212 }
213
214 *ifindex = getsockopt_ifindex(AF_INET, &msg);
215
216 return ret;
217 }
218
219 int irdp_read_raw(struct thread *r)
220 {
221 struct interface *ifp;
222 struct zebra_if *zi;
223 struct irdp_interface *irdp;
224 char buf[IRDP_RX_BUF];
225 int ret, ifindex = 0;
226
227 int irdp_sock = THREAD_FD(r);
228 t_irdp_raw = NULL;
229 thread_add_read(zebrad.master, irdp_read_raw, NULL, irdp_sock,
230 &t_irdp_raw);
231
232 ret = irdp_recvmsg(irdp_sock, (uint8_t *)buf, IRDP_RX_BUF, &ifindex);
233
234 if (ret < 0)
235 zlog_warn("IRDP: RX Error length = %d", ret);
236
237 ifp = if_lookup_by_index(ifindex, VRF_DEFAULT);
238 if (!ifp)
239 return ret;
240
241 zi = ifp->info;
242 if (!zi)
243 return ret;
244
245 irdp = zi->irdp;
246 if (!irdp)
247 return ret;
248
249 if (!(irdp->flags & IF_ACTIVE)) {
250
251 if (irdp->flags & IF_DEBUG_MISC)
252 zlog_debug("IRDP: RX ICMP for disabled interface %s\n",
253 ifp->name);
254 return 0;
255 }
256
257 if (irdp->flags & IF_DEBUG_PACKET) {
258 int i;
259 zlog_debug("IRDP: RX (idx %d) ", ifindex);
260 for (i = 0; i < ret; i++)
261 zlog_debug("IRDP: RX %x ", buf[i] & 0xFF);
262 }
263
264 parse_irdp_packet(buf, ret, ifp);
265
266 return ret;
267 }
268
269 void send_packet(struct interface *ifp, struct stream *s, uint32_t dst,
270 struct prefix *p, uint32_t ttl)
271 {
272 static struct sockaddr_in sockdst = {AF_INET};
273 struct ip *ip;
274 struct icmphdr *icmp;
275 struct msghdr *msg;
276 struct cmsghdr *cmsg;
277 struct iovec iovector;
278 char msgbuf[256];
279 char buf[256];
280 struct in_pktinfo *pktinfo;
281 unsigned long src;
282 uint8_t on;
283
284 if (!(ifp->flags & IFF_UP))
285 return;
286
287 if (p)
288 src = ntohl(p->u.prefix4.s_addr);
289 else
290 src = 0; /* Is filled in */
291
292 ip = (struct ip *)buf;
293 ip->ip_hl = sizeof(struct ip) >> 2;
294 ip->ip_v = IPVERSION;
295 ip->ip_tos = 0xC0;
296 ip->ip_off = 0L;
297 ip->ip_p = 1; /* IP_ICMP */
298 ip->ip_ttl = ttl;
299 ip->ip_src.s_addr = src;
300 ip->ip_dst.s_addr = dst;
301 icmp = (struct icmphdr *)(buf + sizeof(struct ip));
302
303 /* Merge IP header with icmp packet */
304 assert(stream_get_endp(s) < (sizeof(buf) - sizeof(struct ip)));
305 stream_get(icmp, s, stream_get_endp(s));
306
307 /* icmp->checksum is already calculated */
308 ip->ip_len = sizeof(struct ip) + stream_get_endp(s);
309
310 on = 1;
311 if (setsockopt(irdp_sock, IPPROTO_IP, IP_HDRINCL, (char *)&on,
312 sizeof(on))
313 < 0)
314 zlog_warn("sendto %s", safe_strerror(errno));
315
316
317 if (dst == INADDR_BROADCAST) {
318 on = 1;
319 if (setsockopt(irdp_sock, SOL_SOCKET, SO_BROADCAST, (char *)&on,
320 sizeof(on))
321 < 0)
322 zlog_warn("sendto %s", safe_strerror(errno));
323 }
324
325 if (dst != INADDR_BROADCAST)
326 setsockopt_ipv4_multicast_loop(irdp_sock, 0);
327
328 memset(&sockdst, 0, sizeof(sockdst));
329 sockdst.sin_family = AF_INET;
330 sockdst.sin_addr.s_addr = dst;
331
332 cmsg = (struct cmsghdr *)(msgbuf + sizeof(struct msghdr));
333 cmsg->cmsg_len = sizeof(struct cmsghdr) + sizeof(struct in_pktinfo);
334 cmsg->cmsg_level = SOL_IP;
335 cmsg->cmsg_type = IP_PKTINFO;
336 pktinfo = (struct in_pktinfo *)CMSG_DATA(cmsg);
337 pktinfo->ipi_ifindex = ifp->ifindex;
338 pktinfo->ipi_spec_dst.s_addr = src;
339 pktinfo->ipi_addr.s_addr = src;
340
341 iovector.iov_base = (void *)buf;
342 iovector.iov_len = ip->ip_len;
343 msg = (struct msghdr *)msgbuf;
344 msg->msg_name = &sockdst;
345 msg->msg_namelen = sizeof(sockdst);
346 msg->msg_iov = &iovector;
347 msg->msg_iovlen = 1;
348 msg->msg_control = cmsg;
349 msg->msg_controllen = cmsg->cmsg_len;
350
351 sockopt_iphdrincl_swab_htosys(ip);
352
353 if (sendmsg(irdp_sock, msg, 0) < 0) {
354 zlog_warn("sendto %s", safe_strerror(errno));
355 }
356 /* printf("TX on %s idx %d\n", ifp->name, ifp->ifindex); */
357 }