]> git.proxmox.com Git - mirror_frr.git/blob - zebra/irdp_packet.c
zebra: irdp: manage separate IRDP struct
[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 Låås at Swedish University of Agricultural Sciences
32 * for reviewing and tests.
33 */
34
35
36 #include <zebra.h>
37
38
39 #ifdef HAVE_IRDP
40
41 #include "if.h"
42 #include "vty.h"
43 #include "sockunion.h"
44 #include "prefix.h"
45 #include "command.h"
46 #include "memory.h"
47 #include "zebra_memory.h"
48 #include "stream.h"
49 #include "ioctl.h"
50 #include "connected.h"
51 #include "log.h"
52 #include "zclient.h"
53 #include "thread.h"
54 #include "zebra/interface.h"
55 #include "zebra/rtadv.h"
56 #include "zebra/rib.h"
57 #include "zebra/zserv.h"
58 #include "zebra/redistribute.h"
59 #include "zebra/irdp.h"
60 #include <netinet/ip_icmp.h>
61 #include "if.h"
62 #include "checksum.h"
63 #include "sockunion.h"
64 #include "log.h"
65 #include "sockopt.h"
66
67
68 /* GLOBAL VARS */
69
70 int irdp_sock = -1;
71
72 extern struct thread *t_irdp_raw;
73
74 static void parse_irdp_packet(char *p, int len, struct interface *ifp)
75 {
76 struct ip *ip = (struct ip *)p;
77 struct icmphdr *icmp;
78 struct in_addr src;
79 int ip_hlen, iplen, datalen;
80 struct zebra_if *zi;
81 struct irdp_interface *irdp;
82
83 zi = ifp->info;
84 if (!zi)
85 return;
86
87 irdp = zi->irdp;
88 if (!irdp)
89 return;
90
91 ip_hlen = ip->ip_hl << 2;
92
93 sockopt_iphdrincl_swab_systoh(ip);
94
95 iplen = ip->ip_len;
96 datalen = len - ip_hlen;
97 src = ip->ip_src;
98
99 if (len != iplen) {
100 zlog_err("IRDP: RX length doesnt match IP length");
101 return;
102 }
103
104 if (iplen < ICMP_MINLEN) {
105 zlog_err("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 zlog_err("IRDP: RX ICMP packet too long from %s\n",
116 inet_ntoa(src));
117 return;
118 }
119
120 icmp = (struct icmphdr *)(p + ip_hlen);
121
122 /* check icmp checksum */
123 if (in_cksum(icmp, datalen) != icmp->checksum) {
124 zlog_warn(
125 "IRDP: RX ICMP packet from %s. Bad checksum, silently ignored",
126 inet_ntoa(src));
127 return;
128 }
129
130 /* Handle just only IRDP */
131 if (!(icmp->type == ICMP_ROUTERADVERT
132 || icmp->type == ICMP_ROUTERSOLICIT))
133 return;
134
135 if (icmp->code != 0) {
136 zlog_warn(
137 "IRDP: RX packet type %d from %s. Bad ICMP type code,"
138 " silently ignored",
139 icmp->type, inet_ntoa(src));
140 return;
141 }
142
143 if (!((ntohl(ip->ip_dst.s_addr) == INADDR_BROADCAST)
144 && (irdp->flags & IF_BROADCAST))
145 || (ntohl(ip->ip_dst.s_addr) == INADDR_ALLRTRS_GROUP
146 && !(irdp->flags & IF_BROADCAST))) {
147 zlog_warn(
148 "IRDP: RX illegal from %s to %s while %s operates in %s\n",
149 inet_ntoa(src),
150 ntohl(ip->ip_dst.s_addr) == INADDR_ALLRTRS_GROUP
151 ? "multicast"
152 : inet_ntoa(ip->ip_dst),
153 ifp->name,
154 irdp->flags & IF_BROADCAST ? "broadcast" : "multicast");
155
156 zlog_warn("IRDP: Please correct settings\n");
157 return;
158 }
159
160 switch (icmp->type) {
161 case ICMP_ROUTERADVERT:
162 break;
163
164 case ICMP_ROUTERSOLICIT:
165
166 if (irdp->flags & IF_DEBUG_MESSAGES)
167 zlog_debug("IRDP: RX Solicit on %s from %s\n",
168 ifp->name, inet_ntoa(src));
169
170 process_solicit(ifp);
171 break;
172
173 default:
174 zlog_warn(
175 "IRDP: RX type %d from %s. Bad ICMP type, silently ignored",
176 icmp->type, inet_ntoa(src));
177 }
178 }
179
180 static int irdp_recvmsg(int sock, u_char *buf, int size, int *ifindex)
181 {
182 struct msghdr msg;
183 struct iovec iov;
184 char adata[CMSG_SPACE(SOPT_SIZE_CMSG_PKTINFO_IPV4())];
185 int ret;
186
187 msg.msg_name = (void *)0;
188 msg.msg_namelen = 0;
189 msg.msg_iov = &iov;
190 msg.msg_iovlen = 1;
191 msg.msg_control = (void *)adata;
192 msg.msg_controllen = sizeof adata;
193
194 iov.iov_base = buf;
195 iov.iov_len = size;
196
197 ret = recvmsg(sock, &msg, 0);
198 if (ret < 0) {
199 zlog_warn("IRDP: recvmsg: read error %s", safe_strerror(errno));
200 return ret;
201 }
202
203 if (msg.msg_flags & MSG_TRUNC) {
204 zlog_warn("IRDP: recvmsg: truncated message");
205 return ret;
206 }
207 if (msg.msg_flags & MSG_CTRUNC) {
208 zlog_warn("IRDP: recvmsg: truncated control message");
209 return ret;
210 }
211
212 *ifindex = getsockopt_ifindex(AF_INET, &msg);
213
214 return ret;
215 }
216
217 int irdp_read_raw(struct thread *r)
218 {
219 struct interface *ifp;
220 struct zebra_if *zi;
221 struct irdp_interface *irdp;
222 char buf[IRDP_RX_BUF];
223 int ret, ifindex = 0;
224
225 int irdp_sock = THREAD_FD(r);
226 t_irdp_raw = NULL;
227 thread_add_read(zebrad.master, irdp_read_raw, NULL, irdp_sock,
228 &t_irdp_raw);
229
230 ret = irdp_recvmsg(irdp_sock, (u_char *)buf, IRDP_RX_BUF, &ifindex);
231
232 if (ret < 0)
233 zlog_warn("IRDP: RX Error length = %d", ret);
234
235 ifp = if_lookup_by_index(ifindex, VRF_DEFAULT);
236 if (!ifp)
237 return ret;
238
239 zi = ifp->info;
240 if (!zi)
241 return ret;
242
243 irdp = zi->irdp;
244 if (!irdp)
245 return ret;
246
247 if (!(irdp->flags & IF_ACTIVE)) {
248
249 if (irdp->flags & IF_DEBUG_MISC)
250 zlog_debug("IRDP: RX ICMP for disabled interface %s\n",
251 ifp->name);
252 return 0;
253 }
254
255 if (irdp->flags & IF_DEBUG_PACKET) {
256 int i;
257 zlog_debug("IRDP: RX (idx %d) ", ifindex);
258 for (i = 0; i < ret; i++)
259 zlog_debug("IRDP: RX %x ", buf[i] & 0xFF);
260 }
261
262 parse_irdp_packet(buf, ret, ifp);
263
264 return ret;
265 }
266
267 void send_packet(struct interface *ifp, struct stream *s, u_int32_t dst,
268 struct prefix *p, u_int32_t ttl)
269 {
270 static struct sockaddr_in sockdst = {AF_INET};
271 struct ip *ip;
272 struct icmphdr *icmp;
273 struct msghdr *msg;
274 struct cmsghdr *cmsg;
275 struct iovec iovector;
276 char msgbuf[256];
277 char buf[256];
278 struct in_pktinfo *pktinfo;
279 u_long src;
280 u_char on;
281
282 if (!(ifp->flags & IFF_UP))
283 return;
284
285 if (p)
286 src = ntohl(p->u.prefix4.s_addr);
287 else
288 src = 0; /* Is filled in */
289
290 ip = (struct ip *)buf;
291 ip->ip_hl = sizeof(struct ip) >> 2;
292 ip->ip_v = IPVERSION;
293 ip->ip_tos = 0xC0;
294 ip->ip_off = 0L;
295 ip->ip_p = 1; /* IP_ICMP */
296 ip->ip_ttl = ttl;
297 ip->ip_src.s_addr = src;
298 ip->ip_dst.s_addr = dst;
299 icmp = (struct icmphdr *)(buf + sizeof(struct ip));
300
301 /* Merge IP header with icmp packet */
302 assert(stream_get_endp(s) < (sizeof(buf) - sizeof(struct ip)));
303 stream_get(icmp, s, stream_get_endp(s));
304
305 /* icmp->checksum is already calculated */
306 ip->ip_len = sizeof(struct ip) + stream_get_endp(s);
307
308 on = 1;
309 if (setsockopt(irdp_sock, IPPROTO_IP, IP_HDRINCL, (char *)&on,
310 sizeof(on))
311 < 0)
312 zlog_warn("sendto %s", safe_strerror(errno));
313
314
315 if (dst == INADDR_BROADCAST) {
316 on = 1;
317 if (setsockopt(irdp_sock, SOL_SOCKET, SO_BROADCAST, (char *)&on,
318 sizeof(on))
319 < 0)
320 zlog_warn("sendto %s", safe_strerror(errno));
321 }
322
323 if (dst != INADDR_BROADCAST)
324 setsockopt_ipv4_multicast_loop(irdp_sock, 0);
325
326 memset(&sockdst, 0, sizeof(sockdst));
327 sockdst.sin_family = AF_INET;
328 sockdst.sin_addr.s_addr = dst;
329
330 cmsg = (struct cmsghdr *)(msgbuf + sizeof(struct msghdr));
331 cmsg->cmsg_len = sizeof(struct cmsghdr) + sizeof(struct in_pktinfo);
332 cmsg->cmsg_level = SOL_IP;
333 cmsg->cmsg_type = IP_PKTINFO;
334 pktinfo = (struct in_pktinfo *)CMSG_DATA(cmsg);
335 pktinfo->ipi_ifindex = ifp->ifindex;
336 pktinfo->ipi_spec_dst.s_addr = src;
337 pktinfo->ipi_addr.s_addr = src;
338
339 iovector.iov_base = (void *)buf;
340 iovector.iov_len = ip->ip_len;
341 msg = (struct msghdr *)msgbuf;
342 msg->msg_name = &sockdst;
343 msg->msg_namelen = sizeof(sockdst);
344 msg->msg_iov = &iovector;
345 msg->msg_iovlen = 1;
346 msg->msg_control = cmsg;
347 msg->msg_controllen = cmsg->cmsg_len;
348
349 sockopt_iphdrincl_swab_htosys(ip);
350
351 if (sendmsg(irdp_sock, msg, 0) < 0) {
352 zlog_warn("sendto %s", safe_strerror(errno));
353 }
354 /* printf("TX on %s idx %d\n", ifp->name, ifp->ifindex); */
355 }
356
357
358 #endif /* HAVE_IRDP */