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