kernel_socket.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251
  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_STRUCT_SOCKADDR_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_STRUCT_SOCKADDR_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. const 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. #ifdef HAVE_BSD_LINK_DETECT
  275. /* BSD link detect translation */
  276. static void
  277. bsd_linkdetect_translate (struct if_msghdr *ifm)
  278. {
  279. if ((ifm->ifm_data.ifi_link_state >= LINK_STATE_UP) ||
  280. (ifm->ifm_data.ifi_link_state == LINK_STATE_UNKNOWN))
  281. SET_FLAG(ifm->ifm_flags, IFF_RUNNING);
  282. else
  283. UNSET_FLAG(ifm->ifm_flags, IFF_RUNNING);
  284. }
  285. #endif /* HAVE_BSD_LINK_DETECT */
  286. /*
  287. * Handle struct if_msghdr obtained from reading routing socket or
  288. * sysctl (from interface_list). There may or may not be sockaddrs
  289. * present after the header.
  290. */
  291. int
  292. ifm_read (struct if_msghdr *ifm)
  293. {
  294. struct interface *ifp = NULL;
  295. char ifname[IFNAMSIZ];
  296. short ifnlen = 0;
  297. caddr_t *cp;
  298. /* terminate ifname at head (for strnlen) and tail (for safety) */
  299. ifname[IFNAMSIZ - 1] = '\0';
  300. /* paranoia: sanity check structure */
  301. if (ifm->ifm_msglen < sizeof(struct if_msghdr))
  302. {
  303. zlog_err ("ifm_read: ifm->ifm_msglen %d too short\n",
  304. ifm->ifm_msglen);
  305. return -1;
  306. }
  307. /*
  308. * Check for a sockaddr_dl following the message. First, point to
  309. * where a socakddr might be if one follows the message.
  310. */
  311. cp = (void *)(ifm + 1);
  312. #ifdef SUNOS_5
  313. /*
  314. * XXX This behavior should be narrowed to only the kernel versions
  315. * for which the structures returned do not match the headers.
  316. *
  317. * if_msghdr_t on 64 bit kernels in Solaris 9 and earlier versions
  318. * is 12 bytes larger than the 32 bit version.
  319. */
  320. if (((struct sockaddr *) cp)->sa_family == AF_UNSPEC)
  321. cp = cp + 12;
  322. #endif
  323. RTA_ADDR_GET (NULL, RTA_DST, ifm->ifm_addrs, cp);
  324. RTA_ADDR_GET (NULL, RTA_GATEWAY, ifm->ifm_addrs, cp);
  325. RTA_ATTR_GET (NULL, RTA_NETMASK, ifm->ifm_addrs, cp);
  326. RTA_ADDR_GET (NULL, RTA_GENMASK, ifm->ifm_addrs, cp);
  327. RTA_NAME_GET (ifname, RTA_IFP, ifm->ifm_addrs, cp, ifnlen);
  328. RTA_ADDR_GET (NULL, RTA_IFA, ifm->ifm_addrs, cp);
  329. RTA_ADDR_GET (NULL, RTA_AUTHOR, ifm->ifm_addrs, cp);
  330. RTA_ADDR_GET (NULL, RTA_BRD, ifm->ifm_addrs, cp);
  331. if (IS_ZEBRA_DEBUG_KERNEL)
  332. zlog_debug ("%s: sdl ifname %s", __func__, (ifnlen ? ifname : "(nil)"));
  333. /*
  334. * Look up on ifindex first, because ifindices are the primary handle for
  335. * interfaces across the user/kernel boundary, for most systems. (Some
  336. * messages, such as up/down status changes on NetBSD, do not include a
  337. * sockaddr_dl).
  338. */
  339. if ( (ifp = if_lookup_by_index (ifm->ifm_index)) != NULL )
  340. {
  341. /* we have an ifp, verify that the name matches as some systems,
  342. * eg Solaris, have a 1:many association of ifindex:ifname
  343. * if they dont match, we dont have the correct ifp and should
  344. * set it back to NULL to let next check do lookup by name
  345. */
  346. if (ifnlen && (strncmp (ifp->name, ifname, IFNAMSIZ) != 0) )
  347. {
  348. if (IS_ZEBRA_DEBUG_KERNEL)
  349. zlog_debug ("%s: ifp name %s doesnt match sdl name %s",
  350. __func__, ifp->name, ifname);
  351. ifp = NULL;
  352. }
  353. }
  354. /*
  355. * If we dont have an ifp, try looking up by name. Particularly as some
  356. * systems (Solaris) have a 1:many mapping of ifindex:ifname - the ifname
  357. * is therefore our unique handle to that interface.
  358. *
  359. * Interfaces specified in the configuration file for which the ifindex
  360. * has not been determined will have ifindex == IFINDEX_INTERNAL, and such
  361. * interfaces are found by this search, and then their ifindex values can
  362. * be filled in.
  363. */
  364. if ( (ifp == NULL) && ifnlen)
  365. ifp = if_lookup_by_name (ifname);
  366. /*
  367. * If ifp still does not exist or has an invalid index (IFINDEX_INTERNAL),
  368. * create or fill in an interface.
  369. */
  370. if ((ifp == NULL) || (ifp->ifindex == IFINDEX_INTERNAL))
  371. {
  372. /*
  373. * To create or fill in an interface, a sockaddr_dl (via
  374. * RTA_IFP) is required.
  375. */
  376. if (!ifnlen)
  377. {
  378. zlog_warn ("Interface index %d (new) missing ifname\n",
  379. ifm->ifm_index);
  380. return -1;
  381. }
  382. #ifndef RTM_IFANNOUNCE
  383. /* Down->Down interface should be ignored here.
  384. * See further comment below.
  385. */
  386. if (!CHECK_FLAG (ifm->ifm_flags, IFF_UP))
  387. return 0;
  388. #endif /* !RTM_IFANNOUNCE */
  389. if (ifp == NULL)
  390. {
  391. /* Interface that zebra was not previously aware of, so create. */
  392. ifp = if_create (ifname, ifnlen);
  393. if (IS_ZEBRA_DEBUG_KERNEL)
  394. zlog_debug ("%s: creating ifp for ifindex %d",
  395. __func__, ifm->ifm_index);
  396. }
  397. if (IS_ZEBRA_DEBUG_KERNEL)
  398. zlog_debug ("%s: updated/created ifp, ifname %s, ifindex %d",
  399. __func__, ifp->name, ifp->ifindex);
  400. /*
  401. * Fill in newly created interface structure, or larval
  402. * structure with ifindex IFINDEX_INTERNAL.
  403. */
  404. ifp->ifindex = ifm->ifm_index;
  405. #ifdef HAVE_BSD_LINK_DETECT /* translate BSD kernel msg for link-state */
  406. bsd_linkdetect_translate(ifm);
  407. #endif /* HAVE_BSD_LINK_DETECT */
  408. if_flags_update (ifp, ifm->ifm_flags);
  409. #if defined(__bsdi__)
  410. if_kvm_get_mtu (ifp);
  411. #else
  412. if_get_mtu (ifp);
  413. #endif /* __bsdi__ */
  414. if_get_metric (ifp);
  415. if_add_update (ifp);
  416. }
  417. else
  418. /*
  419. * Interface structure exists. Adjust stored flags from
  420. * notification. If interface has up->down or down->up
  421. * transition, call state change routines (to adjust routes,
  422. * notify routing daemons, etc.). (Other flag changes are stored
  423. * but apparently do not trigger action.)
  424. */
  425. {
  426. if (ifp->ifindex != ifm->ifm_index)
  427. {
  428. zlog_warn ("%s: index mismatch, ifname %s, ifp index %d, "
  429. "ifm index %d",
  430. __func__, ifp->name, ifp->ifindex, ifm->ifm_index);
  431. return -1;
  432. }
  433. #ifdef HAVE_BSD_LINK_DETECT /* translate BSD kernel msg for link-state */
  434. bsd_linkdetect_translate(ifm);
  435. #endif /* HAVE_BSD_LINK_DETECT */
  436. /* update flags and handle operative->inoperative transition, if any */
  437. if_flags_update (ifp, ifm->ifm_flags);
  438. #ifndef RTM_IFANNOUNCE
  439. if (!if_is_up (ifp))
  440. {
  441. /* No RTM_IFANNOUNCE on this platform, so we can never
  442. * distinguish between ~IFF_UP and delete. We must presume
  443. * it has been deleted.
  444. * Eg, Solaris will not notify us of unplumb.
  445. *
  446. * XXX: Fixme - this should be runtime detected
  447. * So that a binary compiled on a system with IFANNOUNCE
  448. * will still behave correctly if run on a platform without
  449. */
  450. if_delete_update (ifp);
  451. }
  452. #endif /* RTM_IFANNOUNCE */
  453. if (if_is_up (ifp))
  454. {
  455. #if defined(__bsdi__)
  456. if_kvm_get_mtu (ifp);
  457. #else
  458. if_get_mtu (ifp);
  459. #endif /* __bsdi__ */
  460. if_get_metric (ifp);
  461. }
  462. }
  463. #ifdef HAVE_NET_RT_IFLIST
  464. ifp->stats = ifm->ifm_data;
  465. #endif /* HAVE_NET_RT_IFLIST */
  466. if (IS_ZEBRA_DEBUG_KERNEL)
  467. zlog_debug ("%s: interface %s index %d",
  468. __func__, ifp->name, ifp->ifindex);
  469. return 0;
  470. }
  471. /* Address read from struct ifa_msghdr. */
  472. static void
  473. ifam_read_mesg (struct ifa_msghdr *ifm,
  474. union sockunion *addr,
  475. union sockunion *mask,
  476. union sockunion *brd,
  477. char *ifname,
  478. short *ifnlen)
  479. {
  480. caddr_t pnt, end;
  481. union sockunion dst;
  482. union sockunion gateway;
  483. pnt = (caddr_t)(ifm + 1);
  484. end = ((caddr_t)ifm) + ifm->ifam_msglen;
  485. /* Be sure structure is cleared */
  486. memset (mask, 0, sizeof (union sockunion));
  487. memset (addr, 0, sizeof (union sockunion));
  488. memset (brd, 0, sizeof (union sockunion));
  489. memset (&dst, 0, sizeof (union sockunion));
  490. memset (&gateway, 0, sizeof (union sockunion));
  491. /* We fetch each socket variable into sockunion. */
  492. RTA_ADDR_GET (&dst, RTA_DST, ifm->ifam_addrs, pnt);
  493. RTA_ADDR_GET (&gateway, RTA_GATEWAY, ifm->ifam_addrs, pnt);
  494. RTA_ATTR_GET (mask, RTA_NETMASK, ifm->ifam_addrs, pnt);
  495. RTA_ADDR_GET (NULL, RTA_GENMASK, ifm->ifam_addrs, pnt);
  496. RTA_NAME_GET (ifname, RTA_IFP, ifm->ifam_addrs, pnt, *ifnlen);
  497. RTA_ADDR_GET (addr, RTA_IFA, ifm->ifam_addrs, pnt);
  498. RTA_ADDR_GET (NULL, RTA_AUTHOR, ifm->ifam_addrs, pnt);
  499. RTA_ADDR_GET (brd, RTA_BRD, ifm->ifam_addrs, pnt);
  500. if (IS_ZEBRA_DEBUG_KERNEL)
  501. {
  502. switch (sockunion_family(addr))
  503. {
  504. case AF_INET:
  505. {
  506. char buf[4][INET_ADDRSTRLEN];
  507. zlog_debug ("%s: ifindex %d, ifname %s, ifam_addrs 0x%x, "
  508. "ifam_flags 0x%x, addr %s/%d broad %s dst %s "
  509. "gateway %s",
  510. __func__, ifm->ifam_index,
  511. (ifnlen ? ifname : "(nil)"), ifm->ifam_addrs,
  512. ifm->ifam_flags,
  513. inet_ntop(AF_INET,&addr->sin.sin_addr,
  514. buf[0],sizeof(buf[0])),
  515. ip_masklen(mask->sin.sin_addr),
  516. inet_ntop(AF_INET,&brd->sin.sin_addr,
  517. buf[1],sizeof(buf[1])),
  518. inet_ntop(AF_INET,&dst.sin.sin_addr,
  519. buf[2],sizeof(buf[2])),
  520. inet_ntop(AF_INET,&gateway.sin.sin_addr,
  521. buf[3],sizeof(buf[3])));
  522. }
  523. break;
  524. #ifdef HAVE_IPV6
  525. case AF_INET6:
  526. {
  527. char buf[4][INET6_ADDRSTRLEN];
  528. zlog_debug ("%s: ifindex %d, ifname %s, ifam_addrs 0x%x, "
  529. "ifam_flags 0x%x, addr %s/%d broad %s dst %s "
  530. "gateway %s",
  531. __func__, ifm->ifam_index,
  532. (ifnlen ? ifname : "(nil)"), ifm->ifam_addrs,
  533. ifm->ifam_flags,
  534. inet_ntop(AF_INET6,&addr->sin6.sin6_addr,
  535. buf[0],sizeof(buf[0])),
  536. ip6_masklen(mask->sin6.sin6_addr),
  537. inet_ntop(AF_INET6,&brd->sin6.sin6_addr,
  538. buf[1],sizeof(buf[1])),
  539. inet_ntop(AF_INET6,&dst.sin6.sin6_addr,
  540. buf[2],sizeof(buf[2])),
  541. inet_ntop(AF_INET6,&gateway.sin6.sin6_addr,
  542. buf[3],sizeof(buf[3])));
  543. }
  544. break;
  545. #endif /* HAVE_IPV6 */
  546. default:
  547. zlog_debug ("%s: ifindex %d, ifname %s, ifam_addrs 0x%x",
  548. __func__, ifm->ifam_index,
  549. (ifnlen ? ifname : "(nil)"), ifm->ifam_addrs);
  550. break;
  551. }
  552. }
  553. /* Assert read up end point matches to end point */
  554. if (pnt != end)
  555. zlog_warn ("ifam_read() does't read all socket data");
  556. }
  557. /* Interface's address information get. */
  558. int
  559. ifam_read (struct ifa_msghdr *ifam)
  560. {
  561. struct interface *ifp = NULL;
  562. union sockunion addr, mask, brd;
  563. char ifname[INTERFACE_NAMSIZ];
  564. short ifnlen = 0;
  565. char isalias = 0;
  566. int flags = 0;
  567. ifname[0] = ifname[INTERFACE_NAMSIZ - 1] = '\0';
  568. /* Allocate and read address information. */
  569. ifam_read_mesg (ifam, &addr, &mask, &brd, ifname, &ifnlen);
  570. if ((ifp = if_lookup_by_index(ifam->ifam_index)) == NULL)
  571. {
  572. zlog_warn ("%s: no interface for ifname %s, index %d",
  573. __func__, ifname, ifam->ifam_index);
  574. return -1;
  575. }
  576. if (ifnlen && strncmp (ifp->name, ifname, INTERFACE_NAMSIZ))
  577. isalias = 1;
  578. /* N.B. The info in ifa_msghdr does not tell us whether the RTA_BRD
  579. field contains a broadcast address or a peer address, so we are forced to
  580. rely upon the interface type. */
  581. if (if_is_pointopoint(ifp))
  582. SET_FLAG(flags, ZEBRA_IFA_PEER);
  583. #if 0
  584. /* it might seem cute to grab the interface metric here, however
  585. * we're processing an address update message, and so some systems
  586. * (e.g. FBSD) dont bother to fill in ifam_metric. Disabled, but left
  587. * in deliberately, as comment.
  588. */
  589. ifp->metric = ifam->ifam_metric;
  590. #endif
  591. /* Add connected address. */
  592. switch (sockunion_family (&addr))
  593. {
  594. case AF_INET:
  595. if (ifam->ifam_type == RTM_NEWADDR)
  596. connected_add_ipv4 (ifp, flags, &addr.sin.sin_addr,
  597. ip_masklen (mask.sin.sin_addr),
  598. &brd.sin.sin_addr,
  599. (isalias ? ifname : NULL));
  600. else
  601. connected_delete_ipv4 (ifp, flags, &addr.sin.sin_addr,
  602. ip_masklen (mask.sin.sin_addr),
  603. &brd.sin.sin_addr);
  604. break;
  605. #ifdef HAVE_IPV6
  606. case AF_INET6:
  607. /* Unset interface index from link-local address when IPv6 stack
  608. is KAME. */
  609. if (IN6_IS_ADDR_LINKLOCAL (&addr.sin6.sin6_addr))
  610. SET_IN6_LINKLOCAL_IFINDEX (addr.sin6.sin6_addr, 0);
  611. if (ifam->ifam_type == RTM_NEWADDR)
  612. connected_add_ipv6 (ifp, flags, &addr.sin6.sin6_addr,
  613. ip6_masklen (mask.sin6.sin6_addr),
  614. &brd.sin6.sin6_addr,
  615. (isalias ? ifname : NULL));
  616. else
  617. connected_delete_ipv6 (ifp,
  618. &addr.sin6.sin6_addr,
  619. ip6_masklen (mask.sin6.sin6_addr),
  620. &brd.sin6.sin6_addr);
  621. break;
  622. #endif /* HAVE_IPV6 */
  623. default:
  624. /* Unsupported family silently ignore... */
  625. break;
  626. }
  627. /* Check interface flag for implicit up of the interface. */
  628. if_refresh (ifp);
  629. #ifdef SUNOS_5
  630. /* In addition to lacking IFANNOUNCE, on SUNOS IFF_UP is strange.
  631. * See comments for SUNOS_5 in interface.c::if_flags_mangle.
  632. *
  633. * Here we take care of case where the real IFF_UP was previously
  634. * unset (as kept in struct zebra_if.primary_state) and the mangled
  635. * IFF_UP (ie IFF_UP set || listcount(connected) has now transitioned
  636. * to unset due to the lost non-primary address having DELADDR'd.
  637. *
  638. * we must delete the interface, because in between here and next
  639. * event for this interface-name the administrator could unplumb
  640. * and replumb the interface.
  641. */
  642. if (!if_is_up (ifp))
  643. if_delete_update (ifp);
  644. #endif /* SUNOS_5 */
  645. return 0;
  646. }
  647. /* Interface function for reading kernel routing table information. */
  648. static int
  649. rtm_read_mesg (struct rt_msghdr *rtm,
  650. union sockunion *dest,
  651. union sockunion *mask,
  652. union sockunion *gate,
  653. char *ifname,
  654. short *ifnlen)
  655. {
  656. caddr_t pnt, end;
  657. /* Pnt points out socket data start point. */
  658. pnt = (caddr_t)(rtm + 1);
  659. end = ((caddr_t)rtm) + rtm->rtm_msglen;
  660. /* rt_msghdr version check. */
  661. if (rtm->rtm_version != RTM_VERSION)
  662. zlog (NULL, LOG_WARNING,
  663. "Routing message version different %d should be %d."
  664. "This may cause problem\n", rtm->rtm_version, RTM_VERSION);
  665. /* Be sure structure is cleared */
  666. memset (dest, 0, sizeof (union sockunion));
  667. memset (gate, 0, sizeof (union sockunion));
  668. memset (mask, 0, sizeof (union sockunion));
  669. /* We fetch each socket variable into sockunion. */
  670. RTA_ADDR_GET (dest, RTA_DST, rtm->rtm_addrs, pnt);
  671. RTA_ADDR_GET (gate, RTA_GATEWAY, rtm->rtm_addrs, pnt);
  672. RTA_ATTR_GET (mask, RTA_NETMASK, rtm->rtm_addrs, pnt);
  673. RTA_ADDR_GET (NULL, RTA_GENMASK, rtm->rtm_addrs, pnt);
  674. RTA_NAME_GET (ifname, RTA_IFP, rtm->rtm_addrs, pnt, *ifnlen);
  675. RTA_ADDR_GET (NULL, RTA_IFA, rtm->rtm_addrs, pnt);
  676. RTA_ADDR_GET (NULL, RTA_AUTHOR, rtm->rtm_addrs, pnt);
  677. RTA_ADDR_GET (NULL, RTA_BRD, rtm->rtm_addrs, pnt);
  678. /* If there is netmask information set it's family same as
  679. destination family*/
  680. if (rtm->rtm_addrs & RTA_NETMASK)
  681. mask->sa.sa_family = dest->sa.sa_family;
  682. /* Assert read up to the end of pointer. */
  683. if (pnt != end)
  684. zlog (NULL, LOG_WARNING, "rtm_read() does't read all socket data.");
  685. return rtm->rtm_flags;
  686. }
  687. void
  688. rtm_read (struct rt_msghdr *rtm)
  689. {
  690. int flags;
  691. u_char zebra_flags;
  692. union sockunion dest, mask, gate;
  693. char ifname[INTERFACE_NAMSIZ + 1];
  694. short ifnlen = 0;
  695. zebra_flags = 0;
  696. /* Read destination and netmask and gateway from rtm message
  697. structure. */
  698. flags = rtm_read_mesg (rtm, &dest, &mask, &gate, ifname, &ifnlen);
  699. if (!(flags & RTF_DONE))
  700. return;
  701. if (IS_ZEBRA_DEBUG_KERNEL)
  702. zlog_debug ("%s: got rtm of type %d (%s)", __func__, rtm->rtm_type,
  703. lookup (rtm_type_str, rtm->rtm_type));
  704. #ifdef RTF_CLONED /*bsdi, netbsd 1.6*/
  705. if (flags & RTF_CLONED)
  706. return;
  707. #endif
  708. #ifdef RTF_WASCLONED /*freebsd*/
  709. if (flags & RTF_WASCLONED)
  710. return;
  711. #endif
  712. if ((rtm->rtm_type == RTM_ADD) && ! (flags & RTF_UP))
  713. return;
  714. /* This is connected route. */
  715. if (! (flags & RTF_GATEWAY))
  716. return;
  717. if (flags & RTF_PROTO1)
  718. SET_FLAG (zebra_flags, ZEBRA_FLAG_SELFROUTE);
  719. /* This is persistent route. */
  720. if (flags & RTF_STATIC)
  721. SET_FLAG (zebra_flags, ZEBRA_FLAG_STATIC);
  722. /* This is a reject or blackhole route */
  723. if (flags & RTF_REJECT)
  724. SET_FLAG (zebra_flags, ZEBRA_FLAG_REJECT);
  725. if (flags & RTF_BLACKHOLE)
  726. SET_FLAG (zebra_flags, ZEBRA_FLAG_BLACKHOLE);
  727. if (dest.sa.sa_family == AF_INET)
  728. {
  729. struct prefix_ipv4 p;
  730. p.family = AF_INET;
  731. p.prefix = dest.sin.sin_addr;
  732. if (flags & RTF_HOST)
  733. p.prefixlen = IPV4_MAX_PREFIXLEN;
  734. else
  735. p.prefixlen = ip_masklen (mask.sin.sin_addr);
  736. /* Catch self originated messages and match them against our current RIB.
  737. * At the same time, ignore unconfirmed messages, they should be tracked
  738. * by rtm_write() and kernel_rtm_ipv4().
  739. */
  740. if (rtm->rtm_type != RTM_GET && rtm->rtm_pid == pid)
  741. {
  742. char buf[INET_ADDRSTRLEN], gate_buf[INET_ADDRSTRLEN];
  743. int ret;
  744. if (! IS_ZEBRA_DEBUG_RIB)
  745. return;
  746. ret = rib_lookup_ipv4_route (&p, &gate);
  747. inet_ntop (AF_INET, &p.prefix, buf, INET_ADDRSTRLEN);
  748. switch (rtm->rtm_type)
  749. {
  750. case RTM_ADD:
  751. case RTM_GET:
  752. case RTM_CHANGE:
  753. /* The kernel notifies us about a new route in FIB created by us.
  754. Do we have a correspondent entry in our RIB? */
  755. switch (ret)
  756. {
  757. case ZEBRA_RIB_NOTFOUND:
  758. zlog_debug ("%s: %s %s/%d: desync: RR isn't yet in RIB, while already in FIB",
  759. __func__, lookup (rtm_type_str, rtm->rtm_type), buf, p.prefixlen);
  760. break;
  761. case ZEBRA_RIB_FOUND_CONNECTED:
  762. case ZEBRA_RIB_FOUND_NOGATE:
  763. inet_ntop (AF_INET, &gate.sin.sin_addr, gate_buf, INET_ADDRSTRLEN);
  764. zlog_debug ("%s: %s %s/%d: desync: RR is in RIB, but gate differs (ours is %s)",
  765. __func__, lookup (rtm_type_str, rtm->rtm_type), buf, p.prefixlen, gate_buf);
  766. break;
  767. case ZEBRA_RIB_FOUND_EXACT: /* RIB RR == FIB RR */
  768. zlog_debug ("%s: %s %s/%d: done Ok",
  769. __func__, lookup (rtm_type_str, rtm->rtm_type), buf, p.prefixlen);
  770. rib_lookup_and_dump (&p);
  771. return;
  772. break;
  773. }
  774. break;
  775. case RTM_DELETE:
  776. /* The kernel notifies us about a route deleted by us. Do we still
  777. have it in the RIB? Do we have anything instead? */
  778. switch (ret)
  779. {
  780. case ZEBRA_RIB_FOUND_EXACT:
  781. zlog_debug ("%s: %s %s/%d: desync: RR is still in RIB, while already not in FIB",
  782. __func__, lookup (rtm_type_str, rtm->rtm_type), buf, p.prefixlen);
  783. rib_lookup_and_dump (&p);
  784. break;
  785. case ZEBRA_RIB_FOUND_CONNECTED:
  786. case ZEBRA_RIB_FOUND_NOGATE:
  787. zlog_debug ("%s: %s %s/%d: desync: RR is still in RIB, plus gate differs",
  788. __func__, lookup (rtm_type_str, rtm->rtm_type), buf, p.prefixlen);
  789. rib_lookup_and_dump (&p);
  790. break;
  791. case ZEBRA_RIB_NOTFOUND: /* RIB RR == FIB RR */
  792. zlog_debug ("%s: %s %s/%d: done Ok",
  793. __func__, lookup (rtm_type_str, rtm->rtm_type), buf, p.prefixlen);
  794. rib_lookup_and_dump (&p);
  795. return;
  796. break;
  797. }
  798. break;
  799. default:
  800. zlog_debug ("%s: %s/%d: warning: loopback RTM of type %s received",
  801. __func__, buf, p.prefixlen, lookup (rtm_type_str, rtm->rtm_type));
  802. }
  803. return;
  804. }
  805. /* Change, delete the old prefix, we have no further information
  806. * to specify the route really
  807. */
  808. if (rtm->rtm_type == RTM_CHANGE)
  809. rib_delete_ipv4 (ZEBRA_ROUTE_KERNEL, zebra_flags, &p,
  810. NULL, 0, 0);
  811. if (rtm->rtm_type == RTM_GET
  812. || rtm->rtm_type == RTM_ADD
  813. || rtm->rtm_type == RTM_CHANGE)
  814. rib_add_ipv4 (ZEBRA_ROUTE_KERNEL, zebra_flags,
  815. &p, &gate.sin.sin_addr, NULL, 0, 0, 0, 0);
  816. else
  817. rib_delete_ipv4 (ZEBRA_ROUTE_KERNEL, zebra_flags,
  818. &p, &gate.sin.sin_addr, 0, 0);
  819. }
  820. #ifdef HAVE_IPV6
  821. if (dest.sa.sa_family == AF_INET6)
  822. {
  823. /* One day we might have a debug section here like one in the
  824. * IPv4 case above. Just ignore own messages at the moment.
  825. */
  826. if (rtm->rtm_type != RTM_GET && rtm->rtm_pid == pid)
  827. return;
  828. struct prefix_ipv6 p;
  829. unsigned int ifindex = 0;
  830. p.family = AF_INET6;
  831. p.prefix = dest.sin6.sin6_addr;
  832. if (flags & RTF_HOST)
  833. p.prefixlen = IPV6_MAX_PREFIXLEN;
  834. else
  835. p.prefixlen = ip6_masklen (mask.sin6.sin6_addr);
  836. #ifdef KAME
  837. if (IN6_IS_ADDR_LINKLOCAL (&gate.sin6.sin6_addr))
  838. {
  839. ifindex = IN6_LINKLOCAL_IFINDEX (gate.sin6.sin6_addr);
  840. SET_IN6_LINKLOCAL_IFINDEX (gate.sin6.sin6_addr, 0);
  841. }
  842. #endif /* KAME */
  843. /* CHANGE: delete the old prefix, we have no further information
  844. * to specify the route really
  845. */
  846. if (rtm->rtm_type == RTM_CHANGE)
  847. rib_delete_ipv6 (ZEBRA_ROUTE_KERNEL, zebra_flags, &p,
  848. NULL, 0, 0);
  849. if (rtm->rtm_type == RTM_GET
  850. || rtm->rtm_type == RTM_ADD
  851. || rtm->rtm_type == RTM_CHANGE)
  852. rib_add_ipv6 (ZEBRA_ROUTE_KERNEL, zebra_flags,
  853. &p, &gate.sin6.sin6_addr, ifindex, 0, 0, 0);
  854. else
  855. rib_delete_ipv6 (ZEBRA_ROUTE_KERNEL, zebra_flags,
  856. &p, &gate.sin6.sin6_addr, ifindex, 0);
  857. }
  858. #endif /* HAVE_IPV6 */
  859. }
  860. /* Interface function for the kernel routing table updates. Support
  861. * for RTM_CHANGE will be needed.
  862. * Exported only for rt_socket.c
  863. */
  864. int
  865. rtm_write (int message,
  866. union sockunion *dest,
  867. union sockunion *mask,
  868. union sockunion *gate,
  869. unsigned int index,
  870. int zebra_flags,
  871. int metric)
  872. {
  873. int ret;
  874. caddr_t pnt;
  875. struct interface *ifp;
  876. /* Sequencial number of routing message. */
  877. static int msg_seq = 0;
  878. /* Struct of rt_msghdr and buffer for storing socket's data. */
  879. struct
  880. {
  881. struct rt_msghdr rtm;
  882. char buf[512];
  883. } msg;
  884. if (routing_sock < 0)
  885. return ZEBRA_ERR_EPERM;
  886. /* Clear and set rt_msghdr values */
  887. memset (&msg, 0, sizeof (struct rt_msghdr));
  888. msg.rtm.rtm_version = RTM_VERSION;
  889. msg.rtm.rtm_type = message;
  890. msg.rtm.rtm_seq = msg_seq++;
  891. msg.rtm.rtm_addrs = RTA_DST;
  892. msg.rtm.rtm_addrs |= RTA_GATEWAY;
  893. msg.rtm.rtm_flags = RTF_UP;
  894. msg.rtm.rtm_index = index;
  895. if (metric != 0)
  896. {
  897. msg.rtm.rtm_rmx.rmx_hopcount = metric;
  898. msg.rtm.rtm_inits |= RTV_HOPCOUNT;
  899. }
  900. ifp = if_lookup_by_index (index);
  901. if (gate && message == RTM_ADD)
  902. msg.rtm.rtm_flags |= RTF_GATEWAY;
  903. if (! gate && message == RTM_ADD && ifp &&
  904. (ifp->flags & IFF_POINTOPOINT) == 0)
  905. msg.rtm.rtm_flags |= RTF_CLONING;
  906. /* If no protocol specific gateway is specified, use link
  907. address for gateway. */
  908. if (! gate)
  909. {
  910. if (!ifp)
  911. {
  912. char dest_buf[INET_ADDRSTRLEN] = "NULL", mask_buf[INET_ADDRSTRLEN] = "255.255.255.255";
  913. if (dest)
  914. inet_ntop (AF_INET, &dest->sin.sin_addr, dest_buf, INET_ADDRSTRLEN);
  915. if (mask)
  916. inet_ntop (AF_INET, &mask->sin.sin_addr, mask_buf, INET_ADDRSTRLEN);
  917. zlog_warn ("%s: %s/%s: gate == NULL and no gateway found for ifindex %d",
  918. __func__, dest_buf, mask_buf, index);
  919. return -1;
  920. }
  921. gate = (union sockunion *) & ifp->sdl;
  922. }
  923. if (mask)
  924. msg.rtm.rtm_addrs |= RTA_NETMASK;
  925. else if (message == RTM_ADD)
  926. msg.rtm.rtm_flags |= RTF_HOST;
  927. /* Tagging route with flags */
  928. msg.rtm.rtm_flags |= (RTF_PROTO1);
  929. /* Additional flags. */
  930. if (zebra_flags & ZEBRA_FLAG_BLACKHOLE)
  931. msg.rtm.rtm_flags |= RTF_BLACKHOLE;
  932. if (zebra_flags & ZEBRA_FLAG_REJECT)
  933. msg.rtm.rtm_flags |= RTF_REJECT;
  934. #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
  935. #define SOCKADDRSET(X,R) \
  936. if (msg.rtm.rtm_addrs & (R)) \
  937. { \
  938. int len = ROUNDUP ((X)->sa.sa_len); \
  939. memcpy (pnt, (caddr_t)(X), len); \
  940. pnt += len; \
  941. }
  942. #else
  943. #define SOCKADDRSET(X,R) \
  944. if (msg.rtm.rtm_addrs & (R)) \
  945. { \
  946. int len = SAROUNDUP (X); \
  947. memcpy (pnt, (caddr_t)(X), len); \
  948. pnt += len; \
  949. }
  950. #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
  951. pnt = (caddr_t) msg.buf;
  952. /* Write each socket data into rtm message buffer */
  953. SOCKADDRSET (dest, RTA_DST);
  954. SOCKADDRSET (gate, RTA_GATEWAY);
  955. SOCKADDRSET (mask, RTA_NETMASK);
  956. msg.rtm.rtm_msglen = pnt - (caddr_t) &msg;
  957. ret = write (routing_sock, &msg, msg.rtm.rtm_msglen);
  958. if (ret != msg.rtm.rtm_msglen)
  959. {
  960. if (errno == EEXIST)
  961. return ZEBRA_ERR_RTEXIST;
  962. if (errno == ENETUNREACH)
  963. return ZEBRA_ERR_RTUNREACH;
  964. if (errno == ESRCH)
  965. return ZEBRA_ERR_RTNOEXIST;
  966. zlog_warn ("%s: write : %s (%d)", __func__, safe_strerror (errno), errno);
  967. return ZEBRA_ERR_KERNEL;
  968. }
  969. return ZEBRA_ERR_NOERROR;
  970. }
  971. #include "thread.h"
  972. #include "zebra/zserv.h"
  973. /* For debug purpose. */
  974. static void
  975. rtmsg_debug (struct rt_msghdr *rtm)
  976. {
  977. zlog_debug ("Kernel: Len: %d Type: %s", rtm->rtm_msglen, lookup (rtm_type_str, rtm->rtm_type));
  978. rtm_flag_dump (rtm->rtm_flags);
  979. zlog_debug ("Kernel: message seq %d", rtm->rtm_seq);
  980. zlog_debug ("Kernel: pid %d, rtm_addrs 0x%x", rtm->rtm_pid, rtm->rtm_addrs);
  981. }
  982. /* This is pretty gross, better suggestions welcome -- mhandler */
  983. #ifndef RTAX_MAX
  984. #ifdef RTA_NUMBITS
  985. #define RTAX_MAX RTA_NUMBITS
  986. #else
  987. #define RTAX_MAX 8
  988. #endif /* RTA_NUMBITS */
  989. #endif /* RTAX_MAX */
  990. /* Kernel routing table and interface updates via routing socket. */
  991. static int
  992. kernel_read (struct thread *thread)
  993. {
  994. int sock;
  995. int nbytes;
  996. struct rt_msghdr *rtm;
  997. /*
  998. * This must be big enough for any message the kernel might send.
  999. * Rather than determining how many sockaddrs of what size might be
  1000. * in each particular message, just use RTAX_MAX of sockaddr_storage
  1001. * for each. Note that the sockaddrs must be after each message
  1002. * definition, or rather after whichever happens to be the largest,
  1003. * since the buffer needs to be big enough for a message and the
  1004. * sockaddrs together.
  1005. */
  1006. union
  1007. {
  1008. /* Routing information. */
  1009. struct
  1010. {
  1011. struct rt_msghdr rtm;
  1012. struct sockaddr_storage addr[RTAX_MAX];
  1013. } r;
  1014. /* Interface information. */
  1015. struct
  1016. {
  1017. struct if_msghdr ifm;
  1018. struct sockaddr_storage addr[RTAX_MAX];
  1019. } im;
  1020. /* Interface address information. */
  1021. struct
  1022. {
  1023. struct ifa_msghdr ifa;
  1024. struct sockaddr_storage addr[RTAX_MAX];
  1025. } ia;
  1026. #ifdef RTM_IFANNOUNCE
  1027. /* Interface arrival/departure */
  1028. struct
  1029. {
  1030. struct if_announcemsghdr ifan;
  1031. struct sockaddr_storage addr[RTAX_MAX];
  1032. } ian;
  1033. #endif /* RTM_IFANNOUNCE */
  1034. } buf;
  1035. /* Fetch routing socket. */
  1036. sock = THREAD_FD (thread);
  1037. nbytes= read (sock, &buf, sizeof buf);
  1038. if (nbytes <= 0)
  1039. {
  1040. if (nbytes < 0 && errno != EWOULDBLOCK && errno != EAGAIN)
  1041. zlog_warn ("routing socket error: %s", safe_strerror (errno));
  1042. return 0;
  1043. }
  1044. thread_add_read (zebrad.master, kernel_read, NULL, sock);
  1045. if (IS_ZEBRA_DEBUG_KERNEL)
  1046. rtmsg_debug (&buf.r.rtm);
  1047. rtm = &buf.r.rtm;
  1048. /*
  1049. * Ensure that we didn't drop any data, so that processing routines
  1050. * can assume they have the whole message.
  1051. */
  1052. if (rtm->rtm_msglen != nbytes)
  1053. {
  1054. zlog_warn ("kernel_read: rtm->rtm_msglen %d, nbytes %d, type %d\n",
  1055. rtm->rtm_msglen, nbytes, rtm->rtm_type);
  1056. return -1;
  1057. }
  1058. switch (rtm->rtm_type)
  1059. {
  1060. case RTM_ADD:
  1061. case RTM_DELETE:
  1062. case RTM_CHANGE:
  1063. rtm_read (rtm);
  1064. break;
  1065. case RTM_IFINFO:
  1066. ifm_read (&buf.im.ifm);
  1067. break;
  1068. case RTM_NEWADDR:
  1069. case RTM_DELADDR:
  1070. ifam_read (&buf.ia.ifa);
  1071. break;
  1072. #ifdef RTM_IFANNOUNCE
  1073. case RTM_IFANNOUNCE:
  1074. ifan_read (&buf.ian.ifan);
  1075. break;
  1076. #endif /* RTM_IFANNOUNCE */
  1077. default:
  1078. if (IS_ZEBRA_DEBUG_KERNEL)
  1079. zlog_debug("Unprocessed RTM_type: %d", rtm->rtm_type);
  1080. break;
  1081. }
  1082. return 0;
  1083. }
  1084. /* Make routing socket. */
  1085. static void
  1086. routing_socket (void)
  1087. {
  1088. if ( zserv_privs.change (ZPRIVS_RAISE) )
  1089. zlog_err ("routing_socket: Can't raise privileges");
  1090. routing_sock = socket (AF_ROUTE, SOCK_RAW, 0);
  1091. if (routing_sock < 0)
  1092. {
  1093. if ( zserv_privs.change (ZPRIVS_LOWER) )
  1094. zlog_err ("routing_socket: Can't lower privileges");
  1095. zlog_warn ("Can't init kernel routing socket");
  1096. return;
  1097. }
  1098. /* XXX: Socket should be NONBLOCK, however as we currently
  1099. * discard failed writes, this will lead to inconsistencies.
  1100. * For now, socket must be blocking.
  1101. */
  1102. /*if (fcntl (routing_sock, F_SETFL, O_NONBLOCK) < 0)
  1103. zlog_warn ("Can't set O_NONBLOCK to routing socket");*/
  1104. if ( zserv_privs.change (ZPRIVS_LOWER) )
  1105. zlog_err ("routing_socket: Can't lower privileges");
  1106. /* kernel_read needs rewrite. */
  1107. thread_add_read (zebrad.master, kernel_read, NULL, routing_sock);
  1108. }
  1109. /* Exported interface function. This function simply calls
  1110. routing_socket (). */
  1111. void
  1112. kernel_init (void)
  1113. {
  1114. routing_socket ();
  1115. }