Debian Codegraph @

protolib/src/unix/zebraRouteMgr.cpp

raw
1 #include "unix/zebraRouteMgr.h"
2
3 void ZebraRouteMgr::OnClientSocketEvent(ProtoSocket& theSocket,
4 ProtoSocket::Event theEvent)
5 {
6 //printf("Socket Event\n");
7 }
8
9 ZebraRouteMgr::ZebraRouteMgr()
10 :zPipe(ProtoPipe::STREAM)
11 {
12 }
13
14 ZebraRouteMgr::~ZebraRouteMgr()
15 {
16 Close();
17 }
18
19 bool ZebraRouteMgr::SetForwarding(bool state)
20 {
21 return true;
22 } // end ZebraRouteMgr::SetForwarding()
23
24
25 bool ZebraRouteMgr::Open(const void* /*userData*/)
26 {
27 //printf("zebra mgr open\n");
28 if ((descriptor = socket(PF_NETLINK, SOCK_RAW, NETLINK_ROUTE)) < 0)
29 {
30 printf("ZebraRouteMgr::Open() socket(NETLINK_ROUTE) error: %s\n",
31 strerror(errno));
32 return false;
33 }
34 else
35 {
36 pid = (UINT32)getpid();
37 }
38
39 //printf("descriptor open\n");
40
41 if (!zPipe.Connect("/var/run/zserv.api")) {
42 if(!zPipe.Connect("/var/run/quagga/zserv.api")) {
43 printf("ZebraRouteMgr::Open fail to connect %s\n",zPipe.GetErrorString());
44 printf("ZebraRouteMgr::Open tries to connect to /var/run/zserv.api and /var/run/quagga/zserv.api. Is Zebra running? Is its runs directory one of the above (set durring zebra configure)?\n");
45 return false;
46 }
47 }
48 return true;
49
50 } // end ZebraRouteMgr::Open()
51 bool ZebraRouteMgr::IsOpen() const
52 {
53 //return zSock.IsOpen();
54 return zPipe.IsOpen();
55 }
56
57 void ZebraRouteMgr::Close()
58 {
59 if (IsOpen())
60 {
61 //zSock.Close();
62 zPipe.Close();
63 }
64 } // end ZebraRouteMgr::Close()
65
66 /*
67 * "xdr_encode"-like interface that allows daemon (client) to send
68 * a message to zebra server for a route that needs to be
69 * added/deleted to the kernel. Info about the route is specified
70 * by the caller in a struct zapi_ipv4. zapi_ipv4_read() then writes
71 * the info down the zclient socket using the stream_* functions.
72 *
73 * The corresponding read ("xdr_decode") function on the server
74 * side is zread_ipv4_add()/zread_ipv4_delete().
75 *
76 * 0 1 2 3 4 5 6 7 8 9 A B C D E F 0 1 2 3 4 5 6 7 8 9 A B C D E F
77 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
78 * | Length (2) | Command | Route Type |
79 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
80 * | ZEBRA Flags | Message Flags | Prefix length |
81 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
82 * | Destination IPv4 Prefix for route |
83 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
84 * | Nexthop count |
85 * +-+-+-+-+-+-+-+-+
86 *
87 *
88 * A number of IPv4 nexthop(s) or nexthop interface index(es) are then
89 * described, as per the Nexthop count. Each nexthop described as:
90 *
91 * +-+-+-+-+-+-+-+-+
92 * | Nexthop Type | Set to one of ZEBRA_NEXTHOP_*
93 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
94 * | IPv4 Nexthop address or Interface Index number |
95 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
96 *
97 * Alternatively, if the flags field has ZEBRA_FLAG_BLACKHOLE or
98 * ZEBRA_FLAG_REJECT is set then Nexthop count is set to 1, then _no_
99 * nexthop information is provided, and the message describes a prefix
100 * to blackhole or reject route.
101 *
102 * If ZAPI_MESSAGE_DISTANCE is set, the distance value is written as a 1
103 * byte value.
104 *
105 * If ZAPI_MESSAGE_METRIC is set, the metric value is written as an 8
106 * byte value.
107 *
108 * XXX: No attention paid to alignment.
109 */
110 bool ZebraRouteMgr::SetRoute(const ProtoAddress& dst,
111 unsigned int prefixLen,
112 const ProtoAddress& gw,
113 unsigned int ifIndex,
114 int metric)
115 {
116 //printf("enter setRoute\n");
117 unsigned char *s = obuf;
118 unsigned short *p ;
119 unsigned int len;
120
121 //header
122 p = (unsigned short *)s;
123 *p = htons(ZEBRA_HEADER_SIZE);
124 s +=2 ; // length
125 *s++ = ZEBRA_HEADER_MARKER; // marker
126 *s++ = ZEBRA_VERSION; //version
127 p = (unsigned short *)s;
128 *p = htons(ZEBRA_IPV4_ROUTE_ADD); // cmd
129 s += 2;
130 *s++ = 0 ; //type
131 *s++ = 0 ; //flags
132 *s++ = ZAPI_MESSAGE_NEXTHOP | ZAPI_MESSAGE_DISTANCE; //message
133
134 // destionation
135 *s++ = prefixLen; //prefixlen
136 unsigned long ipLong;
137 ipLong = dst.IPv4GetAddress(); //tbd for ipv6 and others
138 ipLong = htonl(ipLong);
139 int prefixInBytes = (prefixLen +7 )/8;
140 memcpy(s,&ipLong,prefixInBytes); // copy the prefix addr
141 s += prefixInBytes;
142
143 // nexthop num
144 *s++ = 2; // two nexthop entries
145 // nexthop
146 *s++ = ZEBRA_NEXTHOP_IPV4; //type: ipv4
147 ipLong = gw.IPv4GetAddress(); //tbd for ipv6 and others
148 ipLong = htonl(ipLong);
149 memcpy(s,&ipLong,4); // copy the prefix addr
150 s += 4;
151
152 // ifindex
153 *s++ = ZEBRA_NEXTHOP_IFINDEX; // type: ifindex
154 ipLong = htonl(ifIndex);
155 memcpy(s,&ipLong,4); // copy the ifindex
156 s += 4;
157
158 // distance
159 *s++ = metric; // metric
160
161 len = s - obuf;
162 ipLong = htons(len); // length
163 p = (unsigned short *)obuf;
164 *p = htons(len);
165 //PrintBuffer((char *)obuf,len,0);
166 //printf("zPipe Send len=%d\n",len);
167
168 zPipe.Send((char *)obuf,len);
169 //printf("Send len=%d %s\n",len,zPipe.GetErrorString());
170 return true;
171 } // end ZebraRouteMgr::SetRoute()
172
173
174 bool ZebraRouteMgr::DeleteRoute(const ProtoAddress& dst,
175 unsigned int prefixLen,
176 const ProtoAddress& gw,
177 unsigned int ifIndex)
178 {
179 //printf("enter delroute\n");
180 unsigned char *s = obuf;
181 unsigned short *p ;
182 unsigned int len;
183
184 //header
185 p = (unsigned short *)s;
186 *p = htons(ZEBRA_HEADER_SIZE);
187 s +=2 ; // length
188 *s++ = ZEBRA_HEADER_MARKER; // marker
189 *s++ = ZEBRA_VERSION; //version
190 p = (unsigned short *)s;
191 *p = htons(ZEBRA_IPV4_ROUTE_DELETE); // cmd
192 s += 2;
193 *s++ = 0 ; //type
194 *s++ = 0 ; //flags
195 *s++ = ZAPI_MESSAGE_NEXTHOP ; //message
196
197 // destionation
198 *s++ = prefixLen; //prefixlen
199 unsigned long ipLong;
200 ipLong = dst.IPv4GetAddress(); //tbd for ipv6 and others
201 ipLong = htonl(ipLong);
202 int prefixInBytes = (prefixLen +7 )/8;
203 memcpy(s,&ipLong,prefixInBytes); // copy the prefix addr
204 s += prefixInBytes;
205
206 // nexthop num
207 *s++ = 2; // two nexthop entries
208 // nexthop
209 *s++ = ZEBRA_NEXTHOP_IPV4; //type: ipv4
210 ipLong = gw.IPv4GetAddress(); //tbd for ipv6 and others
211 ipLong = htonl(ipLong);
212 memcpy(s,&ipLong,4); // copy the prefix addr
213 s += 4;
214
215 // ifindex
216 *s++ = ZEBRA_NEXTHOP_IFINDEX; // type: ifindex
217 ipLong = htonl(ifIndex);
218 memcpy(s,&ipLong,4); // copy the ifindex
219 s += 4;
220
221 len = s - obuf;
222 ipLong = htons(len); // length
223 p = (unsigned short *)obuf;
224 *p = htons(len);
225 //PrintBuffer((char *)obuf,len,0);
226 //printf("zPipe Send len=%d\n",len);
227
228 zPipe.Send((char *)obuf,len);
229 //printf("Send len=%d %s\n",len,zPipe.GetErrorString());
230 return true;
231 } // end ZebraRouteMgr::DeleteRoute()
232
233
234 bool ZebraRouteMgr::GetRoute(const ProtoAddress& dst,
235 unsigned int prefixLen,
236 ProtoAddress& gw,
237 unsigned int& ifIndex,
238 int& metric)
239 {
240 return true;
241 } // end ZebraRouteMgr::GetRoute()
242
243
244 bool ZebraRouteMgr::GetAllRoutes(ProtoAddress::Type addrType,
245 ProtoRouteTable& routeTable)
246 {
247 // The "do" loop is needed to make sure we get all of the routes
248 // Routing tables can be big (particularly IPv6) and since the
249 // Netlink does _not_ do multi-part messages properly (It only
250 // gives you part of the routing table if your receive buffer
251 // isn't big enough), we repeat the request, increasing the buffer
252 // size until it works
253
254 return true;
255 } // end ZebraRouteMgr::GetAllRoutes()
256
257 void
258 ZebraRouteMgr::PrintBuffer(char* buffer,int len,int dlevel)
259 {
260 /*
261 * @brief Will print out the buffer in hex format
262 *
263 * @param buffer the buffer to be printed
264 * @param len the length of the buffer contents to be printed in bytes
265 *
266 */
267 for(int i=0;i<(int)len;i++)
268 {
269 printf("%02X ",(unsigned char)buffer[i]);
270 if(i % 4 == 3)
271 printf("\n");
272 }
273 }
274 bool ZebraRouteMgr::GetInterfaceAddressList(unsigned int ifIndex,
275 ProtoAddress::Type addrType,
276 ProtoAddressList& addrList)
277 {
278 ProtoAddressList localAddrList; // keep link/site local addresses separate and add to "addrList" at end
279 // Construct request for interface address
280 struct
281 {
282 struct nlmsghdr msg;
283 struct ifaddrmsg ifa;
284 } req;
285 memset(&req, 0, sizeof(req));
286
287 // DMSG(0,"Justin comment in getinterfaceaddress\n");
288 // netlink message header
289 req.msg.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrmsg));
290 req.msg.nlmsg_type = RTM_GETADDR;
291 req.msg.nlmsg_flags = NLM_F_REQUEST | NLM_F_MATCH ;
292 UINT32 seq = sequence++;
293 req.msg.nlmsg_seq = seq;
294 req.msg.nlmsg_pid = pid;
295
296 // route dump request
297 unsigned int addrLength = 0;
298 switch (addrType)
299 {
300 case ProtoAddress::IPv4:
301 // DMSG(0,"Justin comment in getinterfaceaddress is ipv4\n");
302 req.ifa.ifa_family = AF_INET;
303 req.ifa.ifa_prefixlen = 32;
304 addrLength = 4;
305 break;
306 #ifdef HAVE_IPV6
307 case ProtoAddress::IPv6:
308 // DMSG(0,"Justin comment in getinterfaceaddress is ipv6\n");
309 req.ifa.ifa_family = AF_INET6;
310 req.ifa.ifa_prefixlen = 128;
311 addrLength = 16;
312 break;
313 #endif // HAVE_IPV6
314 default:
315 PLOG(PL_ERROR, "LinuxRouteMgr::GetInterfaceAddressList() invalid destination address!\n");
316 return false;
317 break;
318 }
319 req.ifa.ifa_flags = 0;//IFA_F_SECONDARY;//0;//IFA_F_PERMANENT;
320 req.ifa.ifa_scope = RT_SCOPE_UNIVERSE;
321 req.ifa.ifa_index = ifIndex;
322
323 // write message to our netlink socket
324 // DMSG(0,"Justin before send comment in getinterfaceaddress is ipv4\n");
325 int result = send(descriptor, &req, req.msg.nlmsg_len, 0);
326 //DMSG(0,"Justin comment after send in getinterfaceaddress is ipv4\n");
327 if (result < 0)
328 {
329 PLOG(PL_ERROR, "LinuxRouteMgr::GetInterfaceAddressList() send() error: %s\n",
330 strerror(errno));
331 return false;
332 }
333
334 // read response(s)
335 bool done = false;
336 while(!done)
337 {
338 // DMSG(0,"Justin in while comment in getinterfaceaddress is ipv4\n");
339 char buffer[1024];
340 int msgLen = recv(descriptor, buffer, 1024, 0);
341 if (msgLen < 0)
342 {
343 PLOG(PL_ERROR, "LinuxRouteMgr::GetInterfaceAddressList() recv() error: %s\n", strerror(errno));
344 return false;
345 }
346 struct nlmsghdr* msg = (struct nlmsghdr*)buffer;
347 for (; 0 != NLMSG_OK(msg, (unsigned int)msgLen); msg = NLMSG_NEXT(msg, msgLen))
348 {
349 //Justin Below is a semi-hack to get SMF to function correctly in core namespaces.
350 //If when the msg->nlmsg_pid gets fixed this #if can be removed
351 #ifndef CORE_NAMESPACES
352 if ((msg->nlmsg_pid == (UINT32)pid) && (msg->nlmsg_seq == seq))
353 #else
354 if (msg->nlmsg_seq == seq)
355 #endif // if/else !CORE_NAMESPACES
356 {
357 switch (msg->nlmsg_type)
358 {
359 case NLMSG_NOOP:
360 //TRACE("recvd NLMSG_NOOP ...\n");
361 break;
362 case NLMSG_ERROR:
363 {
364 struct nlmsgerr* errorMsg = (struct nlmsgerr*)NLMSG_DATA(msg);
365 PLOG(PL_ERROR, "LinuxRouteMgr::GetInterfaceAddressList() recvd NLMSG_ERROR error seq:%d code:%d...\n",
366 msg->nlmsg_seq, errorMsg->error);
367 return false;
368 break;
369 }
370 case NLMSG_DONE:
371 //TRACE("recvd NLMSG_DONE ...\n");
372 done = true;
373 break;
374 case RTM_NEWADDR:
375 {
376 //TRACE("recvd RTM_NEWADDR ... \n");
377 struct ifaddrmsg* ifa = (struct ifaddrmsg*)NLMSG_DATA(msg);
378 struct rtattr* rta = IFA_RTA(ifa);
379 int rtaLen = msg->nlmsg_len - NLMSG_LENGTH(sizeof(struct ifaddrmsg));
380 for (; RTA_OK(rta, rtaLen);
381 rta = RTA_NEXT(rta, rtaLen))
382 {
383 switch (rta->rta_type)
384 {
385 case IFA_ADDRESS:
386 case IFA_LOCAL:
387 {
388 //ProtoAddress addr;
389 //addr.SetRawHostAddress(addrType, (char*)RTA_DATA(rta), addrLength);
390 //TRACE("%s: %s (index:%d scope:%d)\n", (IFA_ADDRESS == rta->rta_type) ?
391 // "IFA_ADDRESS" : "IFA_LOCAL", addr.GetHostString(),
392 // ifa->ifa_index, ifa->ifa_scope);
393 if (ifa->ifa_index == ifIndex)
394 {
395 switch (ifa->ifa_scope)
396 {
397 case RT_SCOPE_UNIVERSE:
398 {
399 ProtoAddress theAddress;
400 theAddress.SetRawHostAddress(addrType, (char*)RTA_DATA(rta), addrLength);
401 if (theAddress.IsValid())
402 {
403 if (!addrList.Insert(theAddress))
404 {
405 PLOG(PL_ERROR, "LinuxRouteMgr::GetInterfaceAddressList() error: couldn't add to addrList\n");
406 done = true;
407 }
408 }
409 break;
410 }
411 case RT_SCOPE_SITE:
412 case RT_SCOPE_LINK:
413 {
414 // Keep site-local addresses in separate list and add at end.
415 ProtoAddress theAddress;
416 theAddress.SetRawHostAddress(addrType, (char*)RTA_DATA(rta), addrLength);
417 if (theAddress.IsValid() && !localAddrList.Insert(theAddress))
418 PLOG(PL_ERROR, "LinuxRouteMgr::GetInterfaceAddressList() error: couldn't add to localAddrList\n");
419 break;
420 }
421 default:
422 // ignore other address types for now
423 break;
424 }
425 }
426 break;
427 }
428 case IFA_BROADCAST:
429 {
430 //ProtoAddress addr;
431 //addr.SetRawHostAddress(addrType, (char*)RTA_DATA(rta), addrLength);
432 //TRACE("IFA_BROADCAST: %s\n", addr.GetHostString());
433 break;
434 }
435 default:
436 //TRACE("LinuxRouteMgr::GetInterfaceAddressList() unhandled rtattr type:%d len:%d\n",
437 // rta->rta_type, RTA_PAYLOAD(rta));
438 break;
439
440 } // end switch(rta_type)
441 } // end for(RTA_NEXT())
442 break;
443 }
444 default:
445 PLOG(PL_ERROR, "LinuxRouteMgr::GetInterfaceAddressList() matching reply type:%d len:%d bytes\n",
446 msg->nlmsg_type, msg->nlmsg_len);
447 break;
448 } // end switch(nlmsg_type)
449 }
450 else
451 {
452 if (NLMSG_ERROR == msg->nlmsg_type)
453 {
454 struct nlmsgerr* errorMsg = (struct nlmsgerr*)NLMSG_DATA(msg);
455 PLOG(PL_ERROR, "LinuxRouteMgr::GetInterfaceAddressList() recvd NLMSG_ERROR seq:%d code:%d\n",
456 msg->nlmsg_seq, errorMsg->error);
457 }
458 } // end if/else (matching pid && seq)
459 } // end for(NLMSG_NEXT())
460 } // end while (!done)
461
462 ProtoAddressList::Iterator iterator(localAddrList);
463 ProtoAddress localAddr;
464 while (iterator.GetNextAddress(localAddr))
465 {
466 if (!addrList.Insert(localAddr))
467 {
468 PLOG(PL_ERROR, "LinuxRouteMgr::GetInterfaceAddressList() error: couldn't add localAddr to addrList\n");
469 break;
470 }
471 }
472 if (addrList.IsEmpty())
473 PLOG(PL_WARN, "LinuxRouteMgr::GetInterfaceAddressList() warning: no addresses found\n");
474 return true;
475 } // end LinuxRouteMgr::GetInterfaceAddressList()