kernel_socket.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118
  1. /* Kernel communication using routing socket.
  2. * Copyright (C) 1999 Kunihiro Ishiguro
  3. *
  4. * This file is part of GNU Zebra.
  5. *
  6. * GNU Zebra is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the
  8. * Free Software Foundation; either version 2, or (at your option) any
  9. * later version.
  10. *
  11. * GNU Zebra is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with GNU Zebra; see the file COPYING. If not, write to the Free
  18. * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  19. * 02111-1307, USA.
  20. */
  21. #include <zebra.h>
  22. #include "if.h"
  23. #include "prefix.h"
  24. #include "sockunion.h"
  25. #include "connected.h"
  26. #include "memory.h"
  27. #include "ioctl.h"
  28. #include "log.h"
  29. #include "str.h"
  30. #include "table.h"
  31. #include "rib.h"
  32. #include "privs.h"
  33. #include "zebra/interface.h"
  34. #include "zebra/zserv.h"
  35. #include "zebra/debug.h"
  36. #include "zebra/kernel_socket.h"
  37. extern struct zebra_privs_t zserv_privs;
  38. extern struct zebra_t zebrad;
  39. /*
  40. * Given a sockaddr length, round it up to include pad bytes following
  41. * it. Assumes the kernel pads to sizeof(long).
  42. *
  43. * XXX: why is ROUNDUP(0) sizeof(long)? 0 is an illegal sockaddr
  44. * length anyway (< sizeof (struct sockaddr)), so this shouldn't
  45. * matter.
  46. */
  47. #define ROUNDUP(a) \
  48. ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
  49. /*
  50. * Given a pointer (sockaddr or void *), return the number of bytes
  51. * taken up by the sockaddr and any padding needed for alignment.
  52. */
  53. #if defined(HAVE_SA_LEN)
  54. #define SAROUNDUP(X) ROUNDUP(((struct sockaddr *)(X))->sa_len)
  55. #elif defined(HAVE_IPV6)
  56. /*
  57. * One would hope all fixed-size structure definitions are aligned,
  58. * but round them up nonetheless.
  59. */
  60. #define SAROUNDUP(X) \
  61. (((struct sockaddr *)(X))->sa_family == AF_INET ? \
  62. ROUNDUP(sizeof(struct sockaddr_in)):\
  63. (((struct sockaddr *)(X))->sa_family == AF_INET6 ? \
  64. ROUNDUP(sizeof(struct sockaddr_in6)) : \
  65. (((struct sockaddr *)(X))->sa_family == AF_LINK ? \
  66. ROUNDUP(sizeof(struct sockaddr_dl)) : sizeof(struct sockaddr))))
  67. #else /* HAVE_IPV6 */
  68. #define SAROUNDUP(X) \
  69. (((struct sockaddr *)(X))->sa_family == AF_INET ? \
  70. ROUNDUP(sizeof(struct sockaddr_in)):\
  71. (((struct sockaddr *)(X))->sa_family == AF_LINK ? \
  72. ROUNDUP(sizeof(struct sockaddr_dl)) : sizeof(struct sockaddr)))
  73. #endif /* HAVE_SA_LEN */
  74. /* We use an additional pointer in following, pdest, rather than (DEST)
  75. * directly, because gcc will warn if the macro is expanded and DEST is NULL,
  76. * complaining that memcpy is being passed a NULL value, despite the fact
  77. * the if (NULL) makes it impossible.
  78. */
  79. #define RTA_ADDR_GET(DEST, RTA, RTMADDRS, PNT) \
  80. if ((RTMADDRS) & (RTA)) \
  81. { \
  82. void *pdest = (DEST); \
  83. int len = SAROUNDUP ((PNT)); \
  84. if ( ((DEST) != NULL) && \
  85. af_check (((struct sockaddr *)(PNT))->sa_family)) \
  86. memcpy (pdest, (PNT), len); \
  87. (PNT) += len; \
  88. }
  89. #define RTA_ATTR_GET(DEST, RTA, RTMADDRS, PNT) \
  90. if ((RTMADDRS) & (RTA)) \
  91. { \
  92. void *pdest = (DEST); \
  93. int len = SAROUNDUP ((PNT)); \
  94. if ((DEST) != NULL) \
  95. memcpy (pdest, (PNT), len); \
  96. (PNT) += len; \
  97. }
  98. #define RTA_NAME_GET(DEST, RTA, RTMADDRS, PNT, LEN) \
  99. if ((RTMADDRS) & (RTA)) \
  100. { \
  101. u_char *pdest = (u_char *) (DEST); \
  102. int len = SAROUNDUP ((PNT)); \
  103. struct sockaddr_dl *sdl = (struct sockaddr_dl *)(PNT); \
  104. if (IS_ZEBRA_DEBUG_KERNEL) \
  105. zlog_debug ("%s: RTA_SDL_GET nlen %d, alen %d", \
  106. __func__, sdl->sdl_nlen, sdl->sdl_alen); \
  107. if ( ((DEST) != NULL) && (sdl->sdl_family == AF_LINK) \
  108. && (sdl->sdl_nlen < IFNAMSIZ) && (sdl->sdl_nlen <= len) ) \
  109. { \
  110. memcpy (pdest, sdl->sdl_data, sdl->sdl_nlen); \
  111. pdest[sdl->sdl_nlen] = '\0'; \
  112. (LEN) = sdl->sdl_nlen; \
  113. } \
  114. (PNT) += len; \
  115. } \
  116. else \
  117. { \
  118. (LEN) = 0; \
  119. }
  120. /* Routing socket message types. */
  121. struct message rtm_type_str[] =
  122. {
  123. {RTM_ADD, "RTM_ADD"},
  124. {RTM_DELETE, "RTM_DELETE"},
  125. {RTM_CHANGE, "RTM_CHANGE"},
  126. {RTM_GET, "RTM_GET"},
  127. {RTM_LOSING, "RTM_LOSING"},
  128. {RTM_REDIRECT, "RTM_REDIRECT"},
  129. {RTM_MISS, "RTM_MISS"},
  130. {RTM_LOCK, "RTM_LOCK"},
  131. #ifdef OLDADD
  132. {RTM_OLDADD, "RTM_OLDADD"},
  133. #endif /* RTM_OLDADD */
  134. #ifdef RTM_OLDDEL
  135. {RTM_OLDDEL, "RTM_OLDDEL"},
  136. #endif /* RTM_OLDDEL */
  137. {RTM_RESOLVE, "RTM_RESOLVE"},
  138. {RTM_NEWADDR, "RTM_NEWADDR"},
  139. {RTM_DELADDR, "RTM_DELADDR"},
  140. {RTM_IFINFO, "RTM_IFINFO"},
  141. #ifdef RTM_OIFINFO
  142. {RTM_OIFINFO, "RTM_OIFINFO"},
  143. #endif /* RTM_OIFINFO */
  144. #ifdef RTM_NEWMADDR
  145. {RTM_NEWMADDR, "RTM_NEWMADDR"},
  146. #endif /* RTM_NEWMADDR */
  147. #ifdef RTM_DELMADDR
  148. {RTM_DELMADDR, "RTM_DELMADDR"},
  149. #endif /* RTM_DELMADDR */
  150. #ifdef RTM_IFANNOUNCE
  151. {RTM_IFANNOUNCE, "RTM_IFANNOUNCE"},
  152. #endif /* RTM_IFANNOUNCE */
  153. {0, NULL}
  154. };
  155. struct message rtm_flag_str[] =
  156. {
  157. {RTF_UP, "UP"},
  158. {RTF_GATEWAY, "GATEWAY"},
  159. {RTF_HOST, "HOST"},
  160. {RTF_REJECT, "REJECT"},
  161. {RTF_DYNAMIC, "DYNAMIC"},
  162. {RTF_MODIFIED, "MODIFIED"},
  163. {RTF_DONE, "DONE"},
  164. #ifdef RTF_MASK
  165. {RTF_MASK, "MASK"},
  166. #endif /* RTF_MASK */
  167. {RTF_CLONING, "CLONING"},
  168. {RTF_XRESOLVE, "XRESOLVE"},
  169. {RTF_LLINFO, "LLINFO"},
  170. {RTF_STATIC, "STATIC"},
  171. {RTF_BLACKHOLE, "BLACKHOLE"},
  172. #ifdef RTF_PRIVATE
  173. {RTF_PRIVATE, "PRIVATE"},
  174. #endif /* RTF_PRIVATE */
  175. {RTF_PROTO1, "PROTO1"},
  176. {RTF_PROTO2, "PROTO2"},
  177. #ifdef RTF_PRCLONING
  178. {RTF_PRCLONING, "PRCLONING"},
  179. #endif /* RTF_PRCLONING */
  180. #ifdef RTF_WASCLONED
  181. {RTF_WASCLONED, "WASCLONED"},
  182. #endif /* RTF_WASCLONED */
  183. #ifdef RTF_PROTO3
  184. {RTF_PROTO3, "PROTO3"},
  185. #endif /* RTF_PROTO3 */
  186. #ifdef RTF_PINNED
  187. {RTF_PINNED, "PINNED"},
  188. #endif /* RTF_PINNED */
  189. #ifdef RTF_LOCAL
  190. {RTF_LOCAL, "LOCAL"},
  191. #endif /* RTF_LOCAL */
  192. #ifdef RTF_BROADCAST
  193. {RTF_BROADCAST, "BROADCAST"},
  194. #endif /* RTF_BROADCAST */
  195. #ifdef RTF_MULTICAST
  196. {RTF_MULTICAST, "MULTICAST"},
  197. #endif /* RTF_MULTICAST */
  198. #ifdef RTF_MULTIRT
  199. {RTF_MULTIRT, "MULTIRT"},
  200. #endif /* RTF_MULTIRT */
  201. #ifdef RTF_SETSRC
  202. {RTF_SETSRC, "SETSRC"},
  203. #endif /* RTF_SETSRC */
  204. {0, NULL}
  205. };
  206. /* Kernel routing update socket. */
  207. int routing_sock = -1;
  208. /* Yes I'm checking ugly routing socket behavior. */
  209. /* #define DEBUG */
  210. /* Supported address family check. */
  211. static int inline
  212. af_check (int family)
  213. {
  214. if (family == AF_INET)
  215. return 1;
  216. #ifdef HAVE_IPV6
  217. if (family == AF_INET6)
  218. return 1;
  219. #endif /* HAVE_IPV6 */
  220. return 0;
  221. }
  222. /* Dump routing table flag for debug purpose. */
  223. static void
  224. rtm_flag_dump (int flag)
  225. {
  226. struct message *mes;
  227. static char buf[BUFSIZ];
  228. buf[0] = '\0';
  229. for (mes = rtm_flag_str; mes->key != 0; mes++)
  230. {
  231. if (mes->key & flag)
  232. {
  233. strlcat (buf, mes->str, BUFSIZ);
  234. strlcat (buf, " ", BUFSIZ);
  235. }
  236. }
  237. zlog_debug ("Kernel: %s", buf);
  238. }
  239. #ifdef RTM_IFANNOUNCE
  240. /* Interface adding function */
  241. static int
  242. ifan_read (struct if_announcemsghdr *ifan)
  243. {
  244. struct interface *ifp;
  245. ifp = if_lookup_by_index (ifan->ifan_index);
  246. if (ifp)
  247. assert ( (ifp->ifindex == ifan->ifan_index)
  248. || (ifp->ifindex == IFINDEX_INTERNAL) );
  249. if ( (ifp == NULL)
  250. || ((ifp->ifindex == IFINDEX_INTERNAL)
  251. && (ifan->ifan_what == IFAN_ARRIVAL)) )
  252. {
  253. if (IS_ZEBRA_DEBUG_KERNEL)
  254. zlog_debug ("%s: creating interface for ifindex %d, name %s",
  255. __func__, ifan->ifan_index, ifan->ifan_name);
  256. /* Create Interface */
  257. ifp = if_get_by_name_len(ifan->ifan_name,
  258. strnlen(ifan->ifan_name,
  259. sizeof(ifan->ifan_name)));
  260. ifp->ifindex = ifan->ifan_index;
  261. if_add_update (ifp);
  262. }
  263. else if (ifp != NULL && ifan->ifan_what == IFAN_DEPARTURE)
  264. if_delete_update (ifp);
  265. if_get_flags (ifp);
  266. if_get_mtu (ifp);
  267. if_get_metric (ifp);
  268. if (IS_ZEBRA_DEBUG_KERNEL)
  269. zlog_debug ("%s: interface %s index %d",
  270. __func__, ifan->ifan_name, ifan->ifan_index);
  271. return 0;
  272. }
  273. #endif /* RTM_IFANNOUNCE */
  274. /*
  275. * Handle struct if_msghdr obtained from reading routing socket or
  276. * sysctl (from interface_list). There may or may not be sockaddrs
  277. * present after the header.
  278. */
  279. int
  280. ifm_read (struct if_msghdr *ifm)
  281. {
  282. struct interface *ifp = NULL;
  283. char ifname[IFNAMSIZ];
  284. short ifnlen = 0;
  285. caddr_t *cp;
  286. /* terminate ifname at head (for strnlen) and tail (for safety) */
  287. ifname[IFNAMSIZ - 1] = '\0';
  288. /* paranoia: sanity check structure */
  289. if (ifm->ifm_msglen < sizeof(struct if_msghdr))
  290. {
  291. zlog_err ("ifm_read: ifm->ifm_msglen %d too short\n",
  292. ifm->ifm_msglen);
  293. return -1;
  294. }
  295. /*
  296. * Check for a sockaddr_dl following the message. First, point to
  297. * where a socakddr might be if one follows the message.
  298. */
  299. cp = (void *)(ifm + 1);
  300. #ifdef SUNOS_5
  301. /*
  302. * XXX This behavior should be narrowed to only the kernel versions
  303. * for which the structures returned do not match the headers.
  304. *
  305. * if_msghdr_t on 64 bit kernels in Solaris 9 and earlier versions
  306. * is 12 bytes larger than the 32 bit version.
  307. */
  308. if (((struct sockaddr *) cp)->sa_family == AF_UNSPEC)
  309. cp = cp + 12;
  310. #endif
  311. RTA_ADDR_GET (NULL, RTA_DST, ifm->ifm_addrs, cp);
  312. RTA_ADDR_GET (NULL, RTA_GATEWAY, ifm->ifm_addrs, cp);
  313. RTA_ATTR_GET (NULL, RTA_NETMASK, ifm->ifm_addrs, cp);
  314. RTA_ADDR_GET (NULL, RTA_GENMASK, ifm->ifm_addrs, cp);
  315. RTA_NAME_GET (ifname, RTA_IFP, ifm->ifm_addrs, cp, ifnlen);
  316. RTA_ADDR_GET (NULL, RTA_IFA, ifm->ifm_addrs, cp);
  317. RTA_ADDR_GET (NULL, RTA_AUTHOR, ifm->ifm_addrs, cp);
  318. RTA_ADDR_GET (NULL, RTA_BRD, ifm->ifm_addrs, cp);
  319. if (IS_ZEBRA_DEBUG_KERNEL)
  320. zlog_debug ("%s: sdl ifname %s", __func__, (ifnlen ? ifname : "(nil)"));
  321. /*
  322. * Look up on ifindex first, because ifindices are the primary handle for
  323. * interfaces across the user/kernel boundary, for most systems. (Some
  324. * messages, such as up/down status changes on NetBSD, do not include a
  325. * sockaddr_dl).
  326. */
  327. if ( (ifp = if_lookup_by_index (ifm->ifm_index)) != NULL )
  328. {
  329. /* we have an ifp, verify that the name matches as some systems,
  330. * eg Solaris, have a 1:many association of ifindex:ifname
  331. * if they dont match, we dont have the correct ifp and should
  332. * set it back to NULL to let next check do lookup by name
  333. */
  334. if (ifnlen && (strncmp (ifp->name, ifname, IFNAMSIZ) != 0) )
  335. {
  336. if (IS_ZEBRA_DEBUG_KERNEL)
  337. zlog_debug ("%s: ifp name %s doesnt match sdl name %s",
  338. __func__, ifp->name, ifname);
  339. ifp = NULL;
  340. }
  341. }
  342. /*
  343. * If we dont have an ifp, try looking up by name. Particularly as some
  344. * systems (Solaris) have a 1:many mapping of ifindex:ifname - the ifname
  345. * is therefore our unique handle to that interface.
  346. *
  347. * Interfaces specified in the configuration file for which the ifindex
  348. * has not been determined will have ifindex == IFINDEX_INTERNAL, and such
  349. * interfaces are found by this search, and then their ifindex values can
  350. * be filled in.
  351. */
  352. if ( (ifp == NULL) && ifnlen)
  353. ifp = if_lookup_by_name (ifname);
  354. /*
  355. * If ifp still does not exist or has an invalid index (IFINDEX_INTERNAL),
  356. * create or fill in an interface.
  357. */
  358. if ((ifp == NULL) || (ifp->ifindex == IFINDEX_INTERNAL))
  359. {
  360. /*
  361. * To create or fill in an interface, a sockaddr_dl (via
  362. * RTA_IFP) is required.
  363. */
  364. if (!ifnlen)
  365. {
  366. zlog_warn ("Interface index %d (new) missing ifname\n",
  367. ifm->ifm_index);
  368. return -1;
  369. }
  370. #ifndef RTM_IFANNOUNCE
  371. /* Down->Down interface should be ignored here.
  372. * See further comment below.
  373. */
  374. if (!CHECK_FLAG (ifm->ifm_flags, IFF_UP))
  375. return 0;
  376. #endif /* !RTM_IFANNOUNCE */
  377. if (ifp == NULL)
  378. {
  379. /* Interface that zebra was not previously aware of, so create. */
  380. ifp = if_create (ifname, ifnlen);
  381. if (IS_ZEBRA_DEBUG_KERNEL)
  382. zlog_debug ("%s: creating ifp for ifindex %d",
  383. __func__, ifm->ifm_index);
  384. }
  385. if (IS_ZEBRA_DEBUG_KERNEL)
  386. zlog_debug ("%s: updated/created ifp, ifname %s, ifindex %d",
  387. __func__, ifp->name, ifp->ifindex);
  388. /*
  389. * Fill in newly created interface structure, or larval
  390. * structure with ifindex IFINDEX_INTERNAL.
  391. */
  392. ifp->ifindex = ifm->ifm_index;
  393. if_flags_update (ifp, ifm->ifm_flags);
  394. #if defined(__bsdi__)
  395. if_kvm_get_mtu (ifp);
  396. #else
  397. if_get_mtu (ifp);
  398. #endif /* __bsdi__ */
  399. if_get_metric (ifp);
  400. if_add_update (ifp);
  401. }
  402. else
  403. /*
  404. * Interface structure exists. Adjust stored flags from
  405. * notification. If interface has up->down or down->up
  406. * transition, call state change routines (to adjust routes,
  407. * notify routing daemons, etc.). (Other flag changes are stored
  408. * but apparently do not trigger action.)
  409. */
  410. {
  411. if (ifp->ifindex != ifm->ifm_index)
  412. {
  413. zlog_warn ("%s: index mismatch, ifname %s, ifp index %d, "
  414. "ifm index %d",
  415. __func__, ifp->name, ifp->ifindex, ifm->ifm_index);
  416. return -1;
  417. }
  418. /* update flags and handle operative->inoperative transition, if any */
  419. if_flags_update (ifp, ifm->ifm_flags);
  420. #ifndef RTM_IFANNOUNCE
  421. if (!if_is_up (ifp))
  422. {
  423. /* No RTM_IFANNOUNCE on this platform, so we can never
  424. * distinguish between ~IFF_UP and delete. We must presume
  425. * it has been deleted.
  426. * Eg, Solaris will not notify us of unplumb.
  427. *
  428. * XXX: Fixme - this should be runtime detected
  429. * So that a binary compiled on a system with IFANNOUNCE
  430. * will still behave correctly if run on a platform without
  431. */
  432. if_delete_update (ifp);
  433. }
  434. #endif /* RTM_IFANNOUNCE */
  435. }
  436. #ifdef HAVE_NET_RT_IFLIST
  437. ifp->stats = ifm->ifm_data;
  438. #endif /* HAVE_NET_RT_IFLIST */
  439. if (IS_ZEBRA_DEBUG_KERNEL)
  440. zlog_debug ("%s: interface %s index %d",
  441. __func__, ifp->name, ifp->ifindex);
  442. return 0;
  443. }
  444. /* Address read from struct ifa_msghdr. */
  445. static void
  446. ifam_read_mesg (struct ifa_msghdr *ifm,
  447. union sockunion *addr,
  448. union sockunion *mask,
  449. union sockunion *brd,
  450. char *ifname,
  451. short *ifnlen)
  452. {
  453. caddr_t pnt, end;
  454. pnt = (caddr_t)(ifm + 1);
  455. end = ((caddr_t)ifm) + ifm->ifam_msglen;
  456. /* Be sure structure is cleared */
  457. memset (mask, 0, sizeof (union sockunion));
  458. memset (addr, 0, sizeof (union sockunion));
  459. memset (brd, 0, sizeof (union sockunion));
  460. /* We fetch each socket variable into sockunion. */
  461. RTA_ADDR_GET (NULL, RTA_DST, ifm->ifam_addrs, pnt);
  462. RTA_ADDR_GET (NULL, RTA_GATEWAY, ifm->ifam_addrs, pnt);
  463. RTA_ATTR_GET (mask, RTA_NETMASK, ifm->ifam_addrs, pnt);
  464. RTA_ADDR_GET (NULL, RTA_GENMASK, ifm->ifam_addrs, pnt);
  465. RTA_NAME_GET (ifname, RTA_IFP, ifm->ifam_addrs, pnt, *ifnlen);
  466. RTA_ADDR_GET (addr, RTA_IFA, ifm->ifam_addrs, pnt);
  467. RTA_ADDR_GET (NULL, RTA_AUTHOR, ifm->ifam_addrs, pnt);
  468. RTA_ADDR_GET (brd, RTA_BRD, ifm->ifam_addrs, pnt);
  469. if (IS_ZEBRA_DEBUG_KERNEL)
  470. {
  471. switch (sockunion_family(addr))
  472. {
  473. case AF_INET:
  474. {
  475. char buf[2][INET_ADDRSTRLEN];
  476. zlog_debug ("%s: ifindex %d, ifname %s, ifam_addrs 0x%x, "
  477. "addr %s/%d broad %s",
  478. __func__, ifm->ifam_index,
  479. (ifnlen ? ifname : "(nil)"), ifm->ifam_addrs,
  480. inet_ntop(AF_INET,&addr->sin.sin_addr,
  481. buf[0],sizeof(buf[0])),
  482. ip_masklen(mask->sin.sin_addr),
  483. inet_ntop(AF_INET,&brd->sin.sin_addr,
  484. buf[1],sizeof(buf[1])));
  485. }
  486. break;
  487. #ifdef HAVE_IPV6
  488. case AF_INET6:
  489. {
  490. char buf[2][INET6_ADDRSTRLEN];
  491. zlog_debug ("%s: ifindex %d, ifname %s, ifam_addrs 0x%x, "
  492. "addr %s/%d broad %s",
  493. __func__, ifm->ifam_index,
  494. (ifnlen ? ifname : "(nil)"), ifm->ifam_addrs,
  495. inet_ntop(AF_INET6,&addr->sin6.sin6_addr,
  496. buf[0],sizeof(buf[0])),
  497. ip6_masklen(mask->sin6.sin6_addr),
  498. inet_ntop(AF_INET6,&brd->sin6.sin6_addr,
  499. buf[1],sizeof(buf[1])));
  500. }
  501. break;
  502. #endif /* HAVE_IPV6 */
  503. default:
  504. zlog_debug ("%s: ifindex %d, ifname %s, ifam_addrs 0x%x",
  505. __func__, ifm->ifam_index,
  506. (ifnlen ? ifname : "(nil)"), ifm->ifam_addrs);
  507. break;
  508. }
  509. }
  510. /* Assert read up end point matches to end point */
  511. if (pnt != end)
  512. zlog_warn ("ifam_read() does't read all socket data");
  513. }
  514. /* Interface's address information get. */
  515. int
  516. ifam_read (struct ifa_msghdr *ifam)
  517. {
  518. struct interface *ifp = NULL;
  519. union sockunion addr, mask, brd;
  520. char ifname[INTERFACE_NAMSIZ];
  521. short ifnlen = 0;
  522. char isalias = 0;
  523. ifname[0] = ifname[INTERFACE_NAMSIZ - 1] = '\0';
  524. /* Allocate and read address information. */
  525. ifam_read_mesg (ifam, &addr, &mask, &brd, ifname, &ifnlen);
  526. if ((ifp = if_lookup_by_index(ifam->ifam_index)) == NULL)
  527. {
  528. zlog_warn ("%s: no interface for ifname %s, index %d",
  529. __func__, ifname, ifam->ifam_index);
  530. return -1;
  531. }
  532. if (ifnlen && strncmp (ifp->name, ifname, INTERFACE_NAMSIZ))
  533. isalias = 1;
  534. ifp->metric = ifam->ifam_metric;
  535. /* Add connected address. */
  536. switch (sockunion_family (&addr))
  537. {
  538. case AF_INET:
  539. if (ifam->ifam_type == RTM_NEWADDR)
  540. connected_add_ipv4 (ifp, 0, &addr.sin.sin_addr,
  541. ip_masklen (mask.sin.sin_addr),
  542. &brd.sin.sin_addr,
  543. (isalias ? ifname : NULL));
  544. else
  545. connected_delete_ipv4 (ifp, 0, &addr.sin.sin_addr,
  546. ip_masklen (mask.sin.sin_addr),
  547. &brd.sin.sin_addr);
  548. break;
  549. #ifdef HAVE_IPV6
  550. case AF_INET6:
  551. /* Unset interface index from link-local address when IPv6 stack
  552. is KAME. */
  553. if (IN6_IS_ADDR_LINKLOCAL (&addr.sin6.sin6_addr))
  554. SET_IN6_LINKLOCAL_IFINDEX (addr.sin6.sin6_addr, 0);
  555. if (ifam->ifam_type == RTM_NEWADDR)
  556. connected_add_ipv6 (ifp, 0,
  557. &addr.sin6.sin6_addr,
  558. ip6_masklen (mask.sin6.sin6_addr),
  559. &brd.sin6.sin6_addr,
  560. (isalias ? ifname : NULL));
  561. else
  562. connected_delete_ipv6 (ifp,
  563. &addr.sin6.sin6_addr,
  564. ip6_masklen (mask.sin6.sin6_addr),
  565. &brd.sin6.sin6_addr);
  566. break;
  567. #endif /* HAVE_IPV6 */
  568. default:
  569. /* Unsupported family silently ignore... */
  570. break;
  571. }
  572. /* Check interface flag for implicit up of the interface. */
  573. if_refresh (ifp);
  574. #ifdef SUNOS_5
  575. /* In addition to lacking IFANNOUNCE, on SUNOS IFF_UP is strange.
  576. * See comments for SUNOS_5 in interface.c::if_flags_mangle.
  577. *
  578. * Here we take care of case where the real IFF_UP was previously
  579. * unset (as kept in struct zebra_if.primary_state) and the mangled
  580. * IFF_UP (ie IFF_UP set || listcount(connected) has now transitioned
  581. * to unset due to the lost non-primary address having DELADDR'd.
  582. *
  583. * we must delete the interface, because in between here and next
  584. * event for this interface-name the administrator could unplumb
  585. * and replumb the interface.
  586. */
  587. if (!if_is_up (ifp))
  588. if_delete_update (ifp);
  589. #endif /* SUNOS_5 */
  590. return 0;
  591. }
  592. /* Interface function for reading kernel routing table information. */
  593. static int
  594. rtm_read_mesg (struct rt_msghdr *rtm,
  595. union sockunion *dest,
  596. union sockunion *mask,
  597. union sockunion *gate,
  598. char *ifname,
  599. short *ifnlen)
  600. {
  601. caddr_t pnt, end;
  602. /* Pnt points out socket data start point. */
  603. pnt = (caddr_t)(rtm + 1);
  604. end = ((caddr_t)rtm) + rtm->rtm_msglen;
  605. /* rt_msghdr version check. */
  606. if (rtm->rtm_version != RTM_VERSION)
  607. zlog (NULL, LOG_WARNING,
  608. "Routing message version different %d should be %d."
  609. "This may cause problem\n", rtm->rtm_version, RTM_VERSION);
  610. /* Be sure structure is cleared */
  611. memset (dest, 0, sizeof (union sockunion));
  612. memset (gate, 0, sizeof (union sockunion));
  613. memset (mask, 0, sizeof (union sockunion));
  614. /* We fetch each socket variable into sockunion. */
  615. RTA_ADDR_GET (dest, RTA_DST, rtm->rtm_addrs, pnt);
  616. RTA_ADDR_GET (gate, RTA_GATEWAY, rtm->rtm_addrs, pnt);
  617. RTA_ATTR_GET (mask, RTA_NETMASK, rtm->rtm_addrs, pnt);
  618. RTA_ADDR_GET (NULL, RTA_GENMASK, rtm->rtm_addrs, pnt);
  619. RTA_NAME_GET (ifname, RTA_IFP, rtm->rtm_addrs, pnt, *ifnlen);
  620. RTA_ADDR_GET (NULL, RTA_IFA, rtm->rtm_addrs, pnt);
  621. RTA_ADDR_GET (NULL, RTA_AUTHOR, rtm->rtm_addrs, pnt);
  622. RTA_ADDR_GET (NULL, RTA_BRD, rtm->rtm_addrs, pnt);
  623. /* If there is netmask information set it's family same as
  624. destination family*/
  625. if (rtm->rtm_addrs & RTA_NETMASK)
  626. mask->sa.sa_family = dest->sa.sa_family;
  627. /* Assert read up to the end of pointer. */
  628. if (pnt != end)
  629. zlog (NULL, LOG_WARNING, "rtm_read() does't read all socket data.");
  630. return rtm->rtm_flags;
  631. }
  632. void
  633. rtm_read (struct rt_msghdr *rtm)
  634. {
  635. int flags;
  636. u_char zebra_flags;
  637. union sockunion dest, mask, gate;
  638. char ifname[INTERFACE_NAMSIZ + 1];
  639. short ifnlen = 0;
  640. zebra_flags = 0;
  641. /* Discard self send message. */
  642. if (rtm->rtm_type != RTM_GET
  643. && (rtm->rtm_pid == pid || rtm->rtm_pid == old_pid))
  644. return;
  645. /* Read destination and netmask and gateway from rtm message
  646. structure. */
  647. flags = rtm_read_mesg (rtm, &dest, &mask, &gate, ifname, &ifnlen);
  648. #ifdef RTF_CLONED /*bsdi, netbsd 1.6*/
  649. if (flags & RTF_CLONED)
  650. return;
  651. #endif
  652. #ifdef RTF_WASCLONED /*freebsd*/
  653. if (flags & RTF_WASCLONED)
  654. return;
  655. #endif
  656. if ((rtm->rtm_type == RTM_ADD) && ! (flags & RTF_UP))
  657. return;
  658. /* This is connected route. */
  659. if (! (flags & RTF_GATEWAY))
  660. return;
  661. if (flags & RTF_PROTO1)
  662. SET_FLAG (zebra_flags, ZEBRA_FLAG_SELFROUTE);
  663. /* This is persistent route. */
  664. if (flags & RTF_STATIC)
  665. SET_FLAG (zebra_flags, ZEBRA_FLAG_STATIC);
  666. /* This is a reject or blackhole route */
  667. if (flags & RTF_REJECT)
  668. SET_FLAG (zebra_flags, ZEBRA_FLAG_REJECT);
  669. if (flags & RTF_BLACKHOLE)
  670. SET_FLAG (zebra_flags, ZEBRA_FLAG_BLACKHOLE);
  671. if (dest.sa.sa_family == AF_INET)
  672. {
  673. struct prefix_ipv4 p;
  674. p.family = AF_INET;
  675. p.prefix = dest.sin.sin_addr;
  676. if (flags & RTF_HOST)
  677. p.prefixlen = IPV4_MAX_PREFIXLEN;
  678. else
  679. p.prefixlen = ip_masklen (mask.sin.sin_addr);
  680. /* Change, delete the old prefix, we have no further information
  681. * to specify the route really
  682. */
  683. if (rtm->rtm_type == RTM_CHANGE)
  684. rib_delete_ipv4 (ZEBRA_ROUTE_KERNEL, zebra_flags, &p,
  685. NULL, 0, 0);
  686. if (rtm->rtm_type == RTM_GET
  687. || rtm->rtm_type == RTM_ADD
  688. || rtm->rtm_type == RTM_CHANGE)
  689. rib_add_ipv4 (ZEBRA_ROUTE_KERNEL, zebra_flags,
  690. &p, &gate.sin.sin_addr, 0, 0, 0, 0);
  691. else
  692. rib_delete_ipv4 (ZEBRA_ROUTE_KERNEL, zebra_flags,
  693. &p, &gate.sin.sin_addr, 0, 0);
  694. }
  695. #ifdef HAVE_IPV6
  696. if (dest.sa.sa_family == AF_INET6)
  697. {
  698. struct prefix_ipv6 p;
  699. unsigned int ifindex = 0;
  700. p.family = AF_INET6;
  701. p.prefix = dest.sin6.sin6_addr;
  702. if (flags & RTF_HOST)
  703. p.prefixlen = IPV6_MAX_PREFIXLEN;
  704. else
  705. p.prefixlen = ip6_masklen (mask.sin6.sin6_addr);
  706. #ifdef KAME
  707. if (IN6_IS_ADDR_LINKLOCAL (&gate.sin6.sin6_addr))
  708. {
  709. ifindex = IN6_LINKLOCAL_IFINDEX (gate.sin6.sin6_addr);
  710. SET_IN6_LINKLOCAL_IFINDEX (gate.sin6.sin6_addr, 0);
  711. }
  712. #endif /* KAME */
  713. /* CHANGE: delete the old prefix, we have no further information
  714. * to specify the route really
  715. */
  716. if (rtm->rtm_type == RTM_CHANGE)
  717. rib_delete_ipv6 (ZEBRA_ROUTE_KERNEL, zebra_flags, &p,
  718. NULL, 0, 0);
  719. if (rtm->rtm_type == RTM_GET
  720. || rtm->rtm_type == RTM_ADD
  721. || rtm->rtm_type == RTM_CHANGE)
  722. rib_add_ipv6 (ZEBRA_ROUTE_KERNEL, zebra_flags,
  723. &p, &gate.sin6.sin6_addr, ifindex, 0, 0, 0);
  724. else
  725. rib_delete_ipv6 (ZEBRA_ROUTE_KERNEL, zebra_flags,
  726. &p, &gate.sin6.sin6_addr, ifindex, 0);
  727. }
  728. #endif /* HAVE_IPV6 */
  729. }
  730. /* Interface function for the kernel routing table updates. Support
  731. * for RTM_CHANGE will be needed.
  732. * Exported only for rt_socket.c
  733. */
  734. int
  735. rtm_write (int message,
  736. union sockunion *dest,
  737. union sockunion *mask,
  738. union sockunion *gate,
  739. unsigned int index,
  740. int zebra_flags,
  741. int metric)
  742. {
  743. int ret;
  744. caddr_t pnt;
  745. struct interface *ifp;
  746. /* Sequencial number of routing message. */
  747. static int msg_seq = 0;
  748. /* Struct of rt_msghdr and buffer for storing socket's data. */
  749. struct
  750. {
  751. struct rt_msghdr rtm;
  752. char buf[512];
  753. } msg;
  754. if (routing_sock < 0)
  755. return ZEBRA_ERR_EPERM;
  756. /* Clear and set rt_msghdr values */
  757. memset (&msg, 0, sizeof (struct rt_msghdr));
  758. msg.rtm.rtm_version = RTM_VERSION;
  759. msg.rtm.rtm_type = message;
  760. msg.rtm.rtm_seq = msg_seq++;
  761. msg.rtm.rtm_addrs = RTA_DST;
  762. msg.rtm.rtm_addrs |= RTA_GATEWAY;
  763. msg.rtm.rtm_flags = RTF_UP;
  764. msg.rtm.rtm_index = index;
  765. if (metric != 0)
  766. {
  767. msg.rtm.rtm_rmx.rmx_hopcount = metric;
  768. msg.rtm.rtm_inits |= RTV_HOPCOUNT;
  769. }
  770. ifp = if_lookup_by_index (index);
  771. if (gate && message == RTM_ADD)
  772. msg.rtm.rtm_flags |= RTF_GATEWAY;
  773. if (! gate && message == RTM_ADD && ifp &&
  774. (ifp->flags & IFF_POINTOPOINT) == 0)
  775. msg.rtm.rtm_flags |= RTF_CLONING;
  776. /* If no protocol specific gateway is specified, use link
  777. address for gateway. */
  778. if (! gate)
  779. {
  780. if (!ifp)
  781. {
  782. zlog_warn ("no gateway found for interface index %d", index);
  783. return -1;
  784. }
  785. gate = (union sockunion *) & ifp->sdl;
  786. }
  787. if (mask)
  788. msg.rtm.rtm_addrs |= RTA_NETMASK;
  789. else if (message == RTM_ADD)
  790. msg.rtm.rtm_flags |= RTF_HOST;
  791. /* Tagging route with flags */
  792. msg.rtm.rtm_flags |= (RTF_PROTO1);
  793. /* Additional flags. */
  794. if (zebra_flags & ZEBRA_FLAG_BLACKHOLE)
  795. msg.rtm.rtm_flags |= RTF_BLACKHOLE;
  796. if (zebra_flags & ZEBRA_FLAG_REJECT)
  797. msg.rtm.rtm_flags |= RTF_REJECT;
  798. #ifdef HAVE_SIN_LEN
  799. #define SOCKADDRSET(X,R) \
  800. if (msg.rtm.rtm_addrs & (R)) \
  801. { \
  802. int len = ROUNDUP ((X)->sa.sa_len); \
  803. memcpy (pnt, (caddr_t)(X), len); \
  804. pnt += len; \
  805. }
  806. #else
  807. #define SOCKADDRSET(X,R) \
  808. if (msg.rtm.rtm_addrs & (R)) \
  809. { \
  810. int len = SAROUNDUP (X); \
  811. memcpy (pnt, (caddr_t)(X), len); \
  812. pnt += len; \
  813. }
  814. #endif /* HAVE_SIN_LEN */
  815. pnt = (caddr_t) msg.buf;
  816. /* Write each socket data into rtm message buffer */
  817. SOCKADDRSET (dest, RTA_DST);
  818. SOCKADDRSET (gate, RTA_GATEWAY);
  819. SOCKADDRSET (mask, RTA_NETMASK);
  820. msg.rtm.rtm_msglen = pnt - (caddr_t) &msg;
  821. ret = write (routing_sock, &msg, msg.rtm.rtm_msglen);
  822. if (ret != msg.rtm.rtm_msglen)
  823. {
  824. if (errno == EEXIST)
  825. return ZEBRA_ERR_RTEXIST;
  826. if (errno == ENETUNREACH)
  827. return ZEBRA_ERR_RTUNREACH;
  828. zlog_warn ("write : %s (%d)", safe_strerror (errno), errno);
  829. return -1;
  830. }
  831. return 0;
  832. }
  833. #include "thread.h"
  834. #include "zebra/zserv.h"
  835. /* For debug purpose. */
  836. static void
  837. rtmsg_debug (struct rt_msghdr *rtm)
  838. {
  839. const char *type = "Unknown";
  840. struct message *mes;
  841. for (mes = rtm_type_str; mes->str; mes++)
  842. if (mes->key == rtm->rtm_type)
  843. {
  844. type = mes->str;
  845. break;
  846. }
  847. zlog_debug ("Kernel: Len: %d Type: %s", rtm->rtm_msglen, type);
  848. rtm_flag_dump (rtm->rtm_flags);
  849. zlog_debug ("Kernel: message seq %d", rtm->rtm_seq);
  850. zlog_debug ("Kernel: pid %d, rtm_addrs 0x%x", rtm->rtm_pid, rtm->rtm_addrs);
  851. }
  852. /* This is pretty gross, better suggestions welcome -- mhandler */
  853. #ifndef RTAX_MAX
  854. #ifdef RTA_NUMBITS
  855. #define RTAX_MAX RTA_NUMBITS
  856. #else
  857. #define RTAX_MAX 8
  858. #endif /* RTA_NUMBITS */
  859. #endif /* RTAX_MAX */
  860. /* Kernel routing table and interface updates via routing socket. */
  861. static int
  862. kernel_read (struct thread *thread)
  863. {
  864. int sock;
  865. int nbytes;
  866. struct rt_msghdr *rtm;
  867. /*
  868. * This must be big enough for any message the kernel might send.
  869. * Rather than determining how many sockaddrs of what size might be
  870. * in each particular message, just use RTAX_MAX of sockaddr_storage
  871. * for each. Note that the sockaddrs must be after each message
  872. * definition, or rather after whichever happens to be the largest,
  873. * since the buffer needs to be big enough for a message and the
  874. * sockaddrs together.
  875. */
  876. union
  877. {
  878. /* Routing information. */
  879. struct
  880. {
  881. struct rt_msghdr rtm;
  882. struct sockaddr_storage addr[RTAX_MAX];
  883. } r;
  884. /* Interface information. */
  885. struct
  886. {
  887. struct if_msghdr ifm;
  888. struct sockaddr_storage addr[RTAX_MAX];
  889. } im;
  890. /* Interface address information. */
  891. struct
  892. {
  893. struct ifa_msghdr ifa;
  894. struct sockaddr_storage addr[RTAX_MAX];
  895. } ia;
  896. #ifdef RTM_IFANNOUNCE
  897. /* Interface arrival/departure */
  898. struct
  899. {
  900. struct if_announcemsghdr ifan;
  901. struct sockaddr_storage addr[RTAX_MAX];
  902. } ian;
  903. #endif /* RTM_IFANNOUNCE */
  904. } buf;
  905. /* Fetch routing socket. */
  906. sock = THREAD_FD (thread);
  907. nbytes= read (sock, &buf, sizeof buf);
  908. if (nbytes <= 0)
  909. {
  910. if (nbytes < 0 && errno != EWOULDBLOCK && errno != EAGAIN)
  911. zlog_warn ("routing socket error: %s", safe_strerror (errno));
  912. return 0;
  913. }
  914. thread_add_read (zebrad.master, kernel_read, NULL, sock);
  915. if (IS_ZEBRA_DEBUG_KERNEL)
  916. rtmsg_debug (&buf.r.rtm);
  917. rtm = &buf.r.rtm;
  918. /*
  919. * Ensure that we didn't drop any data, so that processing routines
  920. * can assume they have the whole message.
  921. */
  922. if (rtm->rtm_msglen != nbytes)
  923. {
  924. zlog_warn ("kernel_read: rtm->rtm_msglen %d, nbytes %d, type %d\n",
  925. rtm->rtm_msglen, nbytes, rtm->rtm_type);
  926. return -1;
  927. }
  928. switch (rtm->rtm_type)
  929. {
  930. case RTM_ADD:
  931. case RTM_DELETE:
  932. case RTM_CHANGE:
  933. rtm_read (rtm);
  934. break;
  935. case RTM_IFINFO:
  936. ifm_read (&buf.im.ifm);
  937. break;
  938. case RTM_NEWADDR:
  939. case RTM_DELADDR:
  940. ifam_read (&buf.ia.ifa);
  941. break;
  942. #ifdef RTM_IFANNOUNCE
  943. case RTM_IFANNOUNCE:
  944. ifan_read (&buf.ian.ifan);
  945. break;
  946. #endif /* RTM_IFANNOUNCE */
  947. default:
  948. if (IS_ZEBRA_DEBUG_KERNEL)
  949. zlog_debug("Unprocessed RTM_type: %d", rtm->rtm_type);
  950. break;
  951. }
  952. return 0;
  953. }
  954. /* Make routing socket. */
  955. static void
  956. routing_socket (void)
  957. {
  958. if ( zserv_privs.change (ZPRIVS_RAISE) )
  959. zlog_err ("routing_socket: Can't raise privileges");
  960. routing_sock = socket (AF_ROUTE, SOCK_RAW, 0);
  961. if (routing_sock < 0)
  962. {
  963. if ( zserv_privs.change (ZPRIVS_LOWER) )
  964. zlog_err ("routing_socket: Can't lower privileges");
  965. zlog_warn ("Can't init kernel routing socket");
  966. return;
  967. }
  968. /* XXX: Socket should be NONBLOCK, however as we currently
  969. * discard failed writes, this will lead to inconsistencies.
  970. * For now, socket must be blocking.
  971. */
  972. /*if (fcntl (routing_sock, F_SETFL, O_NONBLOCK) < 0)
  973. zlog_warn ("Can't set O_NONBLOCK to routing socket");*/
  974. if ( zserv_privs.change (ZPRIVS_LOWER) )
  975. zlog_err ("routing_socket: Can't lower privileges");
  976. /* kernel_read needs rewrite. */
  977. thread_add_read (zebrad.master, kernel_read, NULL, routing_sock);
  978. }
  979. /* Exported interface function. This function simply calls
  980. routing_socket (). */
  981. void
  982. kernel_init (void)
  983. {
  984. routing_socket ();
  985. }