kernel_socket.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299
  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. * On OS X, both 32, 64bit syatems align on 4 byte boundary
  47. */
  48. #ifdef __APPLE__
  49. #define ROUNDUP(a) \
  50. ((a) > 0 ? (1 + (((a) - 1) | (sizeof(int) - 1))) : sizeof(int))
  51. #else
  52. #define ROUNDUP(a) \
  53. ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
  54. #endif
  55. /*
  56. * Given a pointer (sockaddr or void *), return the number of bytes
  57. * taken up by the sockaddr and any padding needed for alignment.
  58. */
  59. #if defined(HAVE_STRUCT_SOCKADDR_SA_LEN)
  60. #define SAROUNDUP(X) ROUNDUP(((struct sockaddr *)(X))->sa_len)
  61. #elif defined(HAVE_IPV6)
  62. /*
  63. * One would hope all fixed-size structure definitions are aligned,
  64. * but round them up nonetheless.
  65. */
  66. #define SAROUNDUP(X) \
  67. (((struct sockaddr *)(X))->sa_family == AF_INET ? \
  68. ROUNDUP(sizeof(struct sockaddr_in)):\
  69. (((struct sockaddr *)(X))->sa_family == AF_INET6 ? \
  70. ROUNDUP(sizeof(struct sockaddr_in6)) : \
  71. (((struct sockaddr *)(X))->sa_family == AF_LINK ? \
  72. ROUNDUP(sizeof(struct sockaddr_dl)) : sizeof(struct sockaddr))))
  73. #else /* HAVE_IPV6 */
  74. #define SAROUNDUP(X) \
  75. (((struct sockaddr *)(X))->sa_family == AF_INET ? \
  76. ROUNDUP(sizeof(struct sockaddr_in)):\
  77. (((struct sockaddr *)(X))->sa_family == AF_LINK ? \
  78. ROUNDUP(sizeof(struct sockaddr_dl)) : sizeof(struct sockaddr)))
  79. #endif /* HAVE_STRUCT_SOCKADDR_SA_LEN */
  80. /*
  81. * We use a call to an inline function to copy (PNT) to (DEST)
  82. * 1. Calculating the length of the copy requires an #ifdef to determine
  83. * if sa_len is a field and can't be used directly inside a #define
  84. * 2. So the compiler doesn't complain when DEST is NULL, which is only true
  85. * when we are skipping the copy and incrementing to the next SA
  86. */
  87. static void inline
  88. rta_copy (union sockunion *dest, caddr_t src) {
  89. int len;
  90. #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
  91. len = (((struct sockaddr *)src)->sa_len > sizeof (*dest)) ?
  92. sizeof (*dest) : ((struct sockaddr *)src)->sa_len ;
  93. #else
  94. len = (SAROUNDUP (src) > sizeof (*dest)) ?
  95. sizeof (*dest) : SAROUNDUP (src) ;
  96. #endif
  97. memcpy (dest, src, len);
  98. }
  99. #define RTA_ADDR_GET(DEST, RTA, RTMADDRS, PNT) \
  100. if ((RTMADDRS) & (RTA)) \
  101. { \
  102. int len = SAROUNDUP ((PNT)); \
  103. if ( ((DEST) != NULL) && \
  104. af_check (((struct sockaddr *)(PNT))->sa_family)) \
  105. rta_copy((DEST), (PNT)); \
  106. (PNT) += len; \
  107. }
  108. #define RTA_ATTR_GET(DEST, RTA, RTMADDRS, PNT) \
  109. if ((RTMADDRS) & (RTA)) \
  110. { \
  111. int len = SAROUNDUP ((PNT)); \
  112. if ((DEST) != NULL) \
  113. rta_copy((DEST), (PNT)); \
  114. (PNT) += len; \
  115. }
  116. #define RTA_NAME_GET(DEST, RTA, RTMADDRS, PNT, LEN) \
  117. if ((RTMADDRS) & (RTA)) \
  118. { \
  119. u_char *pdest = (u_char *) (DEST); \
  120. int len = SAROUNDUP ((PNT)); \
  121. struct sockaddr_dl *sdl = (struct sockaddr_dl *)(PNT); \
  122. if (IS_ZEBRA_DEBUG_KERNEL) \
  123. zlog_debug ("%s: RTA_SDL_GET nlen %d, alen %d", \
  124. __func__, sdl->sdl_nlen, sdl->sdl_alen); \
  125. if ( ((DEST) != NULL) && (sdl->sdl_family == AF_LINK) \
  126. && (sdl->sdl_nlen < IFNAMSIZ) && (sdl->sdl_nlen <= len) ) \
  127. { \
  128. memcpy (pdest, sdl->sdl_data, sdl->sdl_nlen); \
  129. pdest[sdl->sdl_nlen] = '\0'; \
  130. (LEN) = sdl->sdl_nlen; \
  131. } \
  132. (PNT) += len; \
  133. } \
  134. else \
  135. { \
  136. (LEN) = 0; \
  137. }
  138. /* Routing socket message types. */
  139. const struct message rtm_type_str[] =
  140. {
  141. {RTM_ADD, "RTM_ADD"},
  142. {RTM_DELETE, "RTM_DELETE"},
  143. {RTM_CHANGE, "RTM_CHANGE"},
  144. {RTM_GET, "RTM_GET"},
  145. {RTM_LOSING, "RTM_LOSING"},
  146. {RTM_REDIRECT, "RTM_REDIRECT"},
  147. {RTM_MISS, "RTM_MISS"},
  148. {RTM_LOCK, "RTM_LOCK"},
  149. #ifdef OLDADD
  150. {RTM_OLDADD, "RTM_OLDADD"},
  151. #endif /* RTM_OLDADD */
  152. #ifdef RTM_OLDDEL
  153. {RTM_OLDDEL, "RTM_OLDDEL"},
  154. #endif /* RTM_OLDDEL */
  155. {RTM_RESOLVE, "RTM_RESOLVE"},
  156. {RTM_NEWADDR, "RTM_NEWADDR"},
  157. {RTM_DELADDR, "RTM_DELADDR"},
  158. {RTM_IFINFO, "RTM_IFINFO"},
  159. #ifdef RTM_OIFINFO
  160. {RTM_OIFINFO, "RTM_OIFINFO"},
  161. #endif /* RTM_OIFINFO */
  162. #ifdef RTM_NEWMADDR
  163. {RTM_NEWMADDR, "RTM_NEWMADDR"},
  164. #endif /* RTM_NEWMADDR */
  165. #ifdef RTM_DELMADDR
  166. {RTM_DELMADDR, "RTM_DELMADDR"},
  167. #endif /* RTM_DELMADDR */
  168. #ifdef RTM_IFANNOUNCE
  169. {RTM_IFANNOUNCE, "RTM_IFANNOUNCE"},
  170. #endif /* RTM_IFANNOUNCE */
  171. {0, NULL}
  172. };
  173. static const struct message rtm_flag_str[] =
  174. {
  175. {RTF_UP, "UP"},
  176. {RTF_GATEWAY, "GATEWAY"},
  177. {RTF_HOST, "HOST"},
  178. {RTF_REJECT, "REJECT"},
  179. {RTF_DYNAMIC, "DYNAMIC"},
  180. {RTF_MODIFIED, "MODIFIED"},
  181. {RTF_DONE, "DONE"},
  182. #ifdef RTF_MASK
  183. {RTF_MASK, "MASK"},
  184. #endif /* RTF_MASK */
  185. #ifdef RTF_CLONING
  186. {RTF_CLONING, "CLONING"},
  187. #endif /* RTF_CLONING */
  188. {RTF_XRESOLVE, "XRESOLVE"},
  189. {RTF_LLINFO, "LLINFO"},
  190. {RTF_STATIC, "STATIC"},
  191. {RTF_BLACKHOLE, "BLACKHOLE"},
  192. #ifdef RTF_PRIVATE
  193. {RTF_PRIVATE, "PRIVATE"},
  194. #endif /* RTF_PRIVATE */
  195. {RTF_PROTO1, "PROTO1"},
  196. {RTF_PROTO2, "PROTO2"},
  197. #ifdef RTF_PRCLONING
  198. {RTF_PRCLONING, "PRCLONING"},
  199. #endif /* RTF_PRCLONING */
  200. #ifdef RTF_WASCLONED
  201. {RTF_WASCLONED, "WASCLONED"},
  202. #endif /* RTF_WASCLONED */
  203. #ifdef RTF_PROTO3
  204. {RTF_PROTO3, "PROTO3"},
  205. #endif /* RTF_PROTO3 */
  206. #ifdef RTF_PINNED
  207. {RTF_PINNED, "PINNED"},
  208. #endif /* RTF_PINNED */
  209. #ifdef RTF_LOCAL
  210. {RTF_LOCAL, "LOCAL"},
  211. #endif /* RTF_LOCAL */
  212. #ifdef RTF_BROADCAST
  213. {RTF_BROADCAST, "BROADCAST"},
  214. #endif /* RTF_BROADCAST */
  215. #ifdef RTF_MULTICAST
  216. {RTF_MULTICAST, "MULTICAST"},
  217. #endif /* RTF_MULTICAST */
  218. #ifdef RTF_MULTIRT
  219. {RTF_MULTIRT, "MULTIRT"},
  220. #endif /* RTF_MULTIRT */
  221. #ifdef RTF_SETSRC
  222. {RTF_SETSRC, "SETSRC"},
  223. #endif /* RTF_SETSRC */
  224. {0, NULL}
  225. };
  226. /* Kernel routing update socket. */
  227. int routing_sock = -1;
  228. /* Yes I'm checking ugly routing socket behavior. */
  229. /* #define DEBUG */
  230. /* Supported address family check. */
  231. static int inline
  232. af_check (int family)
  233. {
  234. if (family == AF_INET)
  235. return 1;
  236. #ifdef HAVE_IPV6
  237. if (family == AF_INET6)
  238. return 1;
  239. #endif /* HAVE_IPV6 */
  240. return 0;
  241. }
  242. /* Dump routing table flag for debug purpose. */
  243. static void
  244. rtm_flag_dump (int flag)
  245. {
  246. const struct message *mes;
  247. static char buf[BUFSIZ];
  248. buf[0] = '\0';
  249. for (mes = rtm_flag_str; mes->key != 0; mes++)
  250. {
  251. if (mes->key & flag)
  252. {
  253. strlcat (buf, mes->str, BUFSIZ);
  254. strlcat (buf, " ", BUFSIZ);
  255. }
  256. }
  257. zlog_debug ("Kernel: %s", buf);
  258. }
  259. #ifdef RTM_IFANNOUNCE
  260. /* Interface adding function */
  261. static int
  262. ifan_read (struct if_announcemsghdr *ifan)
  263. {
  264. struct interface *ifp;
  265. ifp = if_lookup_by_index (ifan->ifan_index);
  266. if (ifp)
  267. assert ( (ifp->ifindex == ifan->ifan_index)
  268. || (ifp->ifindex == IFINDEX_INTERNAL) );
  269. if ( (ifp == NULL)
  270. || ((ifp->ifindex == IFINDEX_INTERNAL)
  271. && (ifan->ifan_what == IFAN_ARRIVAL)) )
  272. {
  273. if (IS_ZEBRA_DEBUG_KERNEL)
  274. zlog_debug ("%s: creating interface for ifindex %d, name %s",
  275. __func__, ifan->ifan_index, ifan->ifan_name);
  276. /* Create Interface */
  277. ifp = if_get_by_name_len(ifan->ifan_name,
  278. strnlen(ifan->ifan_name,
  279. sizeof(ifan->ifan_name)));
  280. ifp->ifindex = ifan->ifan_index;
  281. if_get_metric (ifp);
  282. if_add_update (ifp);
  283. }
  284. else if (ifp != NULL && ifan->ifan_what == IFAN_DEPARTURE)
  285. if_delete_update (ifp);
  286. if_get_flags (ifp);
  287. if_get_mtu (ifp);
  288. if_get_metric (ifp);
  289. if (IS_ZEBRA_DEBUG_KERNEL)
  290. zlog_debug ("%s: interface %s index %d",
  291. __func__, ifan->ifan_name, ifan->ifan_index);
  292. return 0;
  293. }
  294. #endif /* RTM_IFANNOUNCE */
  295. #ifdef HAVE_BSD_IFI_LINK_STATE
  296. /* BSD link detect translation */
  297. static void
  298. bsd_linkdetect_translate (struct if_msghdr *ifm)
  299. {
  300. if ((ifm->ifm_data.ifi_link_state >= LINK_STATE_UP) ||
  301. (ifm->ifm_data.ifi_link_state == LINK_STATE_UNKNOWN))
  302. SET_FLAG(ifm->ifm_flags, IFF_RUNNING);
  303. else
  304. UNSET_FLAG(ifm->ifm_flags, IFF_RUNNING);
  305. }
  306. #endif /* HAVE_BSD_IFI_LINK_STATE */
  307. /*
  308. * Handle struct if_msghdr obtained from reading routing socket or
  309. * sysctl (from interface_list). There may or may not be sockaddrs
  310. * present after the header.
  311. */
  312. int
  313. ifm_read (struct if_msghdr *ifm)
  314. {
  315. struct interface *ifp = NULL;
  316. struct sockaddr_dl *sdl;
  317. char ifname[IFNAMSIZ];
  318. short ifnlen = 0;
  319. caddr_t cp;
  320. /* terminate ifname at head (for strnlen) and tail (for safety) */
  321. ifname[IFNAMSIZ - 1] = '\0';
  322. /* paranoia: sanity check structure */
  323. if (ifm->ifm_msglen < sizeof(struct if_msghdr))
  324. {
  325. zlog_err ("ifm_read: ifm->ifm_msglen %d too short\n",
  326. ifm->ifm_msglen);
  327. return -1;
  328. }
  329. /*
  330. * Check for a sockaddr_dl following the message. First, point to
  331. * where a socakddr might be if one follows the message.
  332. */
  333. cp = (void *)(ifm + 1);
  334. #ifdef SUNOS_5
  335. /*
  336. * XXX This behavior should be narrowed to only the kernel versions
  337. * for which the structures returned do not match the headers.
  338. *
  339. * if_msghdr_t on 64 bit kernels in Solaris 9 and earlier versions
  340. * is 12 bytes larger than the 32 bit version.
  341. */
  342. if (((struct sockaddr *) cp)->sa_family == AF_UNSPEC)
  343. cp = cp + 12;
  344. #endif
  345. RTA_ADDR_GET (NULL, RTA_DST, ifm->ifm_addrs, cp);
  346. RTA_ADDR_GET (NULL, RTA_GATEWAY, ifm->ifm_addrs, cp);
  347. RTA_ATTR_GET (NULL, RTA_NETMASK, ifm->ifm_addrs, cp);
  348. RTA_ADDR_GET (NULL, RTA_GENMASK, ifm->ifm_addrs, cp);
  349. sdl = (struct sockaddr_dl *)cp;
  350. RTA_NAME_GET (ifname, RTA_IFP, ifm->ifm_addrs, cp, ifnlen);
  351. RTA_ADDR_GET (NULL, RTA_IFA, ifm->ifm_addrs, cp);
  352. RTA_ADDR_GET (NULL, RTA_AUTHOR, ifm->ifm_addrs, cp);
  353. RTA_ADDR_GET (NULL, RTA_BRD, ifm->ifm_addrs, cp);
  354. if (IS_ZEBRA_DEBUG_KERNEL)
  355. zlog_debug ("%s: sdl ifname %s", __func__, (ifnlen ? ifname : "(nil)"));
  356. /*
  357. * Look up on ifindex first, because ifindices are the primary handle for
  358. * interfaces across the user/kernel boundary, for most systems. (Some
  359. * messages, such as up/down status changes on NetBSD, do not include a
  360. * sockaddr_dl).
  361. */
  362. if ( (ifp = if_lookup_by_index (ifm->ifm_index)) != NULL )
  363. {
  364. /* we have an ifp, verify that the name matches as some systems,
  365. * eg Solaris, have a 1:many association of ifindex:ifname
  366. * if they dont match, we dont have the correct ifp and should
  367. * set it back to NULL to let next check do lookup by name
  368. */
  369. if (ifnlen && (strncmp (ifp->name, ifname, IFNAMSIZ) != 0) )
  370. {
  371. if (IS_ZEBRA_DEBUG_KERNEL)
  372. zlog_debug ("%s: ifp name %s doesnt match sdl name %s",
  373. __func__, ifp->name, ifname);
  374. ifp = NULL;
  375. }
  376. }
  377. /*
  378. * If we dont have an ifp, try looking up by name. Particularly as some
  379. * systems (Solaris) have a 1:many mapping of ifindex:ifname - the ifname
  380. * is therefore our unique handle to that interface.
  381. *
  382. * Interfaces specified in the configuration file for which the ifindex
  383. * has not been determined will have ifindex == IFINDEX_INTERNAL, and such
  384. * interfaces are found by this search, and then their ifindex values can
  385. * be filled in.
  386. */
  387. if ( (ifp == NULL) && ifnlen)
  388. ifp = if_lookup_by_name (ifname);
  389. /*
  390. * If ifp still does not exist or has an invalid index (IFINDEX_INTERNAL),
  391. * create or fill in an interface.
  392. */
  393. if ((ifp == NULL) || (ifp->ifindex == IFINDEX_INTERNAL))
  394. {
  395. /*
  396. * To create or fill in an interface, a sockaddr_dl (via
  397. * RTA_IFP) is required.
  398. */
  399. if (!ifnlen)
  400. {
  401. zlog_warn ("Interface index %d (new) missing ifname\n",
  402. ifm->ifm_index);
  403. return -1;
  404. }
  405. #ifndef RTM_IFANNOUNCE
  406. /* Down->Down interface should be ignored here.
  407. * See further comment below.
  408. */
  409. if (!CHECK_FLAG (ifm->ifm_flags, IFF_UP))
  410. return 0;
  411. #endif /* !RTM_IFANNOUNCE */
  412. if (ifp == NULL)
  413. {
  414. /* Interface that zebra was not previously aware of, so create. */
  415. ifp = if_create (ifname, ifnlen);
  416. if (IS_ZEBRA_DEBUG_KERNEL)
  417. zlog_debug ("%s: creating ifp for ifindex %d",
  418. __func__, ifm->ifm_index);
  419. }
  420. if (IS_ZEBRA_DEBUG_KERNEL)
  421. zlog_debug ("%s: updated/created ifp, ifname %s, ifindex %d",
  422. __func__, ifp->name, ifp->ifindex);
  423. /*
  424. * Fill in newly created interface structure, or larval
  425. * structure with ifindex IFINDEX_INTERNAL.
  426. */
  427. ifp->ifindex = ifm->ifm_index;
  428. #ifdef HAVE_BSD_IFI_LINK_STATE /* translate BSD kernel msg for link-state */
  429. bsd_linkdetect_translate(ifm);
  430. #endif /* HAVE_BSD_IFI_LINK_STATE */
  431. if_flags_update (ifp, ifm->ifm_flags);
  432. #if defined(__bsdi__)
  433. if_kvm_get_mtu (ifp);
  434. #else
  435. if_get_mtu (ifp);
  436. #endif /* __bsdi__ */
  437. if_get_metric (ifp);
  438. /*
  439. * XXX sockaddr_dl contents can be larger than the structure
  440. * definition. There are 2 big families here:
  441. * - BSD has sdl_len + sdl_data[16] + overruns sdl_data
  442. * we MUST use sdl_len here or we'll truncate data.
  443. * - Solaris has no sdl_len, but sdl_data[244]
  444. * presumably, it's not going to run past that, so sizeof()
  445. * is fine here.
  446. * a nonzero ifnlen from RTA_NAME_GET() means sdl is valid
  447. */
  448. if (ifnlen)
  449. {
  450. #ifdef HAVE_STRUCT_SOCKADDR_DL_SDL_LEN
  451. memcpy (&ifp->sdl, sdl, sdl->sdl_len);
  452. #else
  453. memcpy (&ifp->sdl, sdl, sizeof (struct sockaddr_dl));
  454. #endif /* HAVE_STRUCT_SOCKADDR_DL_SDL_LEN */
  455. }
  456. if_add_update (ifp);
  457. }
  458. else
  459. /*
  460. * Interface structure exists. Adjust stored flags from
  461. * notification. If interface has up->down or down->up
  462. * transition, call state change routines (to adjust routes,
  463. * notify routing daemons, etc.). (Other flag changes are stored
  464. * but apparently do not trigger action.)
  465. */
  466. {
  467. if (ifp->ifindex != ifm->ifm_index)
  468. {
  469. zlog_warn ("%s: index mismatch, ifname %s, ifp index %d, "
  470. "ifm index %d",
  471. __func__, ifp->name, ifp->ifindex, ifm->ifm_index);
  472. return -1;
  473. }
  474. #ifdef HAVE_BSD_IFI_LINK_STATE /* translate BSD kernel msg for link-state */
  475. bsd_linkdetect_translate(ifm);
  476. #endif /* HAVE_BSD_IFI_LINK_STATE */
  477. /* update flags and handle operative->inoperative transition, if any */
  478. if_flags_update (ifp, ifm->ifm_flags);
  479. #ifndef RTM_IFANNOUNCE
  480. if (!if_is_up (ifp))
  481. {
  482. /* No RTM_IFANNOUNCE on this platform, so we can never
  483. * distinguish between ~IFF_UP and delete. We must presume
  484. * it has been deleted.
  485. * Eg, Solaris will not notify us of unplumb.
  486. *
  487. * XXX: Fixme - this should be runtime detected
  488. * So that a binary compiled on a system with IFANNOUNCE
  489. * will still behave correctly if run on a platform without
  490. */
  491. if_delete_update (ifp);
  492. }
  493. #endif /* RTM_IFANNOUNCE */
  494. if (if_is_up (ifp))
  495. {
  496. #if defined(__bsdi__)
  497. if_kvm_get_mtu (ifp);
  498. #else
  499. if_get_mtu (ifp);
  500. #endif /* __bsdi__ */
  501. if_get_metric (ifp);
  502. }
  503. }
  504. #ifdef HAVE_NET_RT_IFLIST
  505. ifp->stats = ifm->ifm_data;
  506. #endif /* HAVE_NET_RT_IFLIST */
  507. if (IS_ZEBRA_DEBUG_KERNEL)
  508. zlog_debug ("%s: interface %s index %d",
  509. __func__, ifp->name, ifp->ifindex);
  510. return 0;
  511. }
  512. /* Address read from struct ifa_msghdr. */
  513. static void
  514. ifam_read_mesg (struct ifa_msghdr *ifm,
  515. union sockunion *addr,
  516. union sockunion *mask,
  517. union sockunion *brd,
  518. char *ifname,
  519. short *ifnlen)
  520. {
  521. caddr_t pnt, end;
  522. union sockunion dst;
  523. union sockunion gateway;
  524. pnt = (caddr_t)(ifm + 1);
  525. end = ((caddr_t)ifm) + ifm->ifam_msglen;
  526. /* Be sure structure is cleared */
  527. memset (mask, 0, sizeof (union sockunion));
  528. memset (addr, 0, sizeof (union sockunion));
  529. memset (brd, 0, sizeof (union sockunion));
  530. memset (&dst, 0, sizeof (union sockunion));
  531. memset (&gateway, 0, sizeof (union sockunion));
  532. /* We fetch each socket variable into sockunion. */
  533. RTA_ADDR_GET (&dst, RTA_DST, ifm->ifam_addrs, pnt);
  534. RTA_ADDR_GET (&gateway, RTA_GATEWAY, ifm->ifam_addrs, pnt);
  535. RTA_ATTR_GET (mask, RTA_NETMASK, ifm->ifam_addrs, pnt);
  536. RTA_ADDR_GET (NULL, RTA_GENMASK, ifm->ifam_addrs, pnt);
  537. RTA_NAME_GET (ifname, RTA_IFP, ifm->ifam_addrs, pnt, *ifnlen);
  538. RTA_ADDR_GET (addr, RTA_IFA, ifm->ifam_addrs, pnt);
  539. RTA_ADDR_GET (NULL, RTA_AUTHOR, ifm->ifam_addrs, pnt);
  540. RTA_ADDR_GET (brd, RTA_BRD, ifm->ifam_addrs, pnt);
  541. if (IS_ZEBRA_DEBUG_KERNEL)
  542. {
  543. switch (sockunion_family(addr))
  544. {
  545. case AF_INET:
  546. {
  547. char buf[4][INET_ADDRSTRLEN];
  548. zlog_debug ("%s: ifindex %d, ifname %s, ifam_addrs 0x%x, "
  549. "ifam_flags 0x%x, addr %s/%d broad %s dst %s "
  550. "gateway %s",
  551. __func__, ifm->ifam_index,
  552. (ifnlen ? ifname : "(nil)"), ifm->ifam_addrs,
  553. ifm->ifam_flags,
  554. inet_ntop(AF_INET,&addr->sin.sin_addr,
  555. buf[0],sizeof(buf[0])),
  556. ip_masklen(mask->sin.sin_addr),
  557. inet_ntop(AF_INET,&brd->sin.sin_addr,
  558. buf[1],sizeof(buf[1])),
  559. inet_ntop(AF_INET,&dst.sin.sin_addr,
  560. buf[2],sizeof(buf[2])),
  561. inet_ntop(AF_INET,&gateway.sin.sin_addr,
  562. buf[3],sizeof(buf[3])));
  563. }
  564. break;
  565. #ifdef HAVE_IPV6
  566. case AF_INET6:
  567. {
  568. char buf[4][INET6_ADDRSTRLEN];
  569. zlog_debug ("%s: ifindex %d, ifname %s, ifam_addrs 0x%x, "
  570. "ifam_flags 0x%x, addr %s/%d broad %s dst %s "
  571. "gateway %s",
  572. __func__, ifm->ifam_index,
  573. (ifnlen ? ifname : "(nil)"), ifm->ifam_addrs,
  574. ifm->ifam_flags,
  575. inet_ntop(AF_INET6,&addr->sin6.sin6_addr,
  576. buf[0],sizeof(buf[0])),
  577. ip6_masklen(mask->sin6.sin6_addr),
  578. inet_ntop(AF_INET6,&brd->sin6.sin6_addr,
  579. buf[1],sizeof(buf[1])),
  580. inet_ntop(AF_INET6,&dst.sin6.sin6_addr,
  581. buf[2],sizeof(buf[2])),
  582. inet_ntop(AF_INET6,&gateway.sin6.sin6_addr,
  583. buf[3],sizeof(buf[3])));
  584. }
  585. break;
  586. #endif /* HAVE_IPV6 */
  587. default:
  588. zlog_debug ("%s: ifindex %d, ifname %s, ifam_addrs 0x%x",
  589. __func__, ifm->ifam_index,
  590. (ifnlen ? ifname : "(nil)"), ifm->ifam_addrs);
  591. break;
  592. }
  593. }
  594. /* Assert read up end point matches to end point */
  595. if (pnt != end)
  596. zlog_warn ("ifam_read() doesn't read all socket data");
  597. }
  598. /* Interface's address information get. */
  599. int
  600. ifam_read (struct ifa_msghdr *ifam)
  601. {
  602. struct interface *ifp = NULL;
  603. union sockunion addr, mask, brd;
  604. char ifname[INTERFACE_NAMSIZ];
  605. short ifnlen = 0;
  606. char isalias = 0;
  607. int flags = 0;
  608. ifname[0] = ifname[INTERFACE_NAMSIZ - 1] = '\0';
  609. /* Allocate and read address information. */
  610. ifam_read_mesg (ifam, &addr, &mask, &brd, ifname, &ifnlen);
  611. if ((ifp = if_lookup_by_index(ifam->ifam_index)) == NULL)
  612. {
  613. zlog_warn ("%s: no interface for ifname %s, index %d",
  614. __func__, ifname, ifam->ifam_index);
  615. return -1;
  616. }
  617. if (ifnlen && strncmp (ifp->name, ifname, INTERFACE_NAMSIZ))
  618. isalias = 1;
  619. /* N.B. The info in ifa_msghdr does not tell us whether the RTA_BRD
  620. field contains a broadcast address or a peer address, so we are forced to
  621. rely upon the interface type. */
  622. if (if_is_pointopoint(ifp))
  623. SET_FLAG(flags, ZEBRA_IFA_PEER);
  624. #if 0
  625. /* it might seem cute to grab the interface metric here, however
  626. * we're processing an address update message, and so some systems
  627. * (e.g. FBSD) dont bother to fill in ifam_metric. Disabled, but left
  628. * in deliberately, as comment.
  629. */
  630. ifp->metric = ifam->ifam_metric;
  631. #endif
  632. /* Add connected address. */
  633. switch (sockunion_family (&addr))
  634. {
  635. case AF_INET:
  636. if (ifam->ifam_type == RTM_NEWADDR)
  637. connected_add_ipv4 (ifp, flags, &addr.sin.sin_addr,
  638. ip_masklen (mask.sin.sin_addr),
  639. &brd.sin.sin_addr,
  640. (isalias ? ifname : NULL));
  641. else
  642. connected_delete_ipv4 (ifp, flags, &addr.sin.sin_addr,
  643. ip_masklen (mask.sin.sin_addr),
  644. &brd.sin.sin_addr);
  645. break;
  646. #ifdef HAVE_IPV6
  647. case AF_INET6:
  648. /* Unset interface index from link-local address when IPv6 stack
  649. is KAME. */
  650. if (IN6_IS_ADDR_LINKLOCAL (&addr.sin6.sin6_addr))
  651. SET_IN6_LINKLOCAL_IFINDEX (addr.sin6.sin6_addr, 0);
  652. if (ifam->ifam_type == RTM_NEWADDR)
  653. connected_add_ipv6 (ifp, flags, &addr.sin6.sin6_addr,
  654. ip6_masklen (mask.sin6.sin6_addr),
  655. &brd.sin6.sin6_addr,
  656. (isalias ? ifname : NULL));
  657. else
  658. connected_delete_ipv6 (ifp,
  659. &addr.sin6.sin6_addr,
  660. ip6_masklen (mask.sin6.sin6_addr),
  661. &brd.sin6.sin6_addr);
  662. break;
  663. #endif /* HAVE_IPV6 */
  664. default:
  665. /* Unsupported family silently ignore... */
  666. break;
  667. }
  668. /* Check interface flag for implicit up of the interface. */
  669. if_refresh (ifp);
  670. #ifdef SUNOS_5
  671. /* In addition to lacking IFANNOUNCE, on SUNOS IFF_UP is strange.
  672. * See comments for SUNOS_5 in interface.c::if_flags_mangle.
  673. *
  674. * Here we take care of case where the real IFF_UP was previously
  675. * unset (as kept in struct zebra_if.primary_state) and the mangled
  676. * IFF_UP (ie IFF_UP set || listcount(connected) has now transitioned
  677. * to unset due to the lost non-primary address having DELADDR'd.
  678. *
  679. * we must delete the interface, because in between here and next
  680. * event for this interface-name the administrator could unplumb
  681. * and replumb the interface.
  682. */
  683. if (!if_is_up (ifp))
  684. if_delete_update (ifp);
  685. #endif /* SUNOS_5 */
  686. return 0;
  687. }
  688. /* Interface function for reading kernel routing table information. */
  689. static int
  690. rtm_read_mesg (struct rt_msghdr *rtm,
  691. union sockunion *dest,
  692. union sockunion *mask,
  693. union sockunion *gate,
  694. char *ifname,
  695. short *ifnlen)
  696. {
  697. caddr_t pnt, end;
  698. /* Pnt points out socket data start point. */
  699. pnt = (caddr_t)(rtm + 1);
  700. end = ((caddr_t)rtm) + rtm->rtm_msglen;
  701. /* rt_msghdr version check. */
  702. if (rtm->rtm_version != RTM_VERSION)
  703. zlog (NULL, LOG_WARNING,
  704. "Routing message version different %d should be %d."
  705. "This may cause problem\n", rtm->rtm_version, RTM_VERSION);
  706. /* Be sure structure is cleared */
  707. memset (dest, 0, sizeof (union sockunion));
  708. memset (gate, 0, sizeof (union sockunion));
  709. memset (mask, 0, sizeof (union sockunion));
  710. /* We fetch each socket variable into sockunion. */
  711. RTA_ADDR_GET (dest, RTA_DST, rtm->rtm_addrs, pnt);
  712. RTA_ADDR_GET (gate, RTA_GATEWAY, rtm->rtm_addrs, pnt);
  713. RTA_ATTR_GET (mask, RTA_NETMASK, rtm->rtm_addrs, pnt);
  714. RTA_ADDR_GET (NULL, RTA_GENMASK, rtm->rtm_addrs, pnt);
  715. RTA_NAME_GET (ifname, RTA_IFP, rtm->rtm_addrs, pnt, *ifnlen);
  716. RTA_ADDR_GET (NULL, RTA_IFA, rtm->rtm_addrs, pnt);
  717. RTA_ADDR_GET (NULL, RTA_AUTHOR, rtm->rtm_addrs, pnt);
  718. RTA_ADDR_GET (NULL, RTA_BRD, rtm->rtm_addrs, pnt);
  719. /* If there is netmask information set it's family same as
  720. destination family*/
  721. if (rtm->rtm_addrs & RTA_NETMASK)
  722. mask->sa.sa_family = dest->sa.sa_family;
  723. /* Assert read up to the end of pointer. */
  724. if (pnt != end)
  725. zlog (NULL, LOG_WARNING, "rtm_read() doesn't read all socket data.");
  726. return rtm->rtm_flags;
  727. }
  728. void
  729. rtm_read (struct rt_msghdr *rtm)
  730. {
  731. int flags;
  732. u_char zebra_flags;
  733. union sockunion dest, mask, gate;
  734. char ifname[INTERFACE_NAMSIZ + 1];
  735. short ifnlen = 0;
  736. zebra_flags = 0;
  737. /* Read destination and netmask and gateway from rtm message
  738. structure. */
  739. flags = rtm_read_mesg (rtm, &dest, &mask, &gate, ifname, &ifnlen);
  740. if (!(flags & RTF_DONE))
  741. return;
  742. if (IS_ZEBRA_DEBUG_KERNEL)
  743. zlog_debug ("%s: got rtm of type %d (%s)", __func__, rtm->rtm_type,
  744. lookup (rtm_type_str, rtm->rtm_type));
  745. #ifdef RTF_CLONED /*bsdi, netbsd 1.6*/
  746. if (flags & RTF_CLONED)
  747. return;
  748. #endif
  749. #ifdef RTF_WASCLONED /*freebsd*/
  750. if (flags & RTF_WASCLONED)
  751. return;
  752. #endif
  753. if ((rtm->rtm_type == RTM_ADD) && ! (flags & RTF_UP))
  754. return;
  755. /* This is connected route. */
  756. if (! (flags & RTF_GATEWAY))
  757. return;
  758. if (flags & RTF_PROTO1)
  759. SET_FLAG (zebra_flags, ZEBRA_FLAG_SELFROUTE);
  760. /* This is persistent route. */
  761. if (flags & RTF_STATIC)
  762. SET_FLAG (zebra_flags, ZEBRA_FLAG_STATIC);
  763. /* This is a reject or blackhole route */
  764. if (flags & RTF_REJECT)
  765. SET_FLAG (zebra_flags, ZEBRA_FLAG_REJECT);
  766. if (flags & RTF_BLACKHOLE)
  767. SET_FLAG (zebra_flags, ZEBRA_FLAG_BLACKHOLE);
  768. if (dest.sa.sa_family == AF_INET)
  769. {
  770. struct prefix_ipv4 p;
  771. p.family = AF_INET;
  772. p.prefix = dest.sin.sin_addr;
  773. if (flags & RTF_HOST)
  774. p.prefixlen = IPV4_MAX_PREFIXLEN;
  775. else
  776. p.prefixlen = ip_masklen (mask.sin.sin_addr);
  777. /* Catch self originated messages and match them against our current RIB.
  778. * At the same time, ignore unconfirmed messages, they should be tracked
  779. * by rtm_write() and kernel_rtm_ipv4().
  780. */
  781. if (rtm->rtm_type != RTM_GET && rtm->rtm_pid == pid)
  782. {
  783. char buf[INET_ADDRSTRLEN], gate_buf[INET_ADDRSTRLEN];
  784. int ret;
  785. if (! IS_ZEBRA_DEBUG_RIB)
  786. return;
  787. ret = rib_lookup_ipv4_route (&p, &gate);
  788. inet_ntop (AF_INET, &p.prefix, buf, INET_ADDRSTRLEN);
  789. switch (rtm->rtm_type)
  790. {
  791. case RTM_ADD:
  792. case RTM_GET:
  793. case RTM_CHANGE:
  794. /* The kernel notifies us about a new route in FIB created by us.
  795. Do we have a correspondent entry in our RIB? */
  796. switch (ret)
  797. {
  798. case ZEBRA_RIB_NOTFOUND:
  799. zlog_debug ("%s: %s %s/%d: desync: RR isn't yet in RIB, while already in FIB",
  800. __func__, lookup (rtm_type_str, rtm->rtm_type), buf, p.prefixlen);
  801. break;
  802. case ZEBRA_RIB_FOUND_CONNECTED:
  803. case ZEBRA_RIB_FOUND_NOGATE:
  804. inet_ntop (AF_INET, &gate.sin.sin_addr, gate_buf, INET_ADDRSTRLEN);
  805. zlog_debug ("%s: %s %s/%d: desync: RR is in RIB, but gate differs (ours is %s)",
  806. __func__, lookup (rtm_type_str, rtm->rtm_type), buf, p.prefixlen, gate_buf);
  807. break;
  808. case ZEBRA_RIB_FOUND_EXACT: /* RIB RR == FIB RR */
  809. zlog_debug ("%s: %s %s/%d: done Ok",
  810. __func__, lookup (rtm_type_str, rtm->rtm_type), buf, p.prefixlen);
  811. rib_lookup_and_dump (&p);
  812. return;
  813. break;
  814. }
  815. break;
  816. case RTM_DELETE:
  817. /* The kernel notifies us about a route deleted by us. Do we still
  818. have it in the RIB? Do we have anything instead? */
  819. switch (ret)
  820. {
  821. case ZEBRA_RIB_FOUND_EXACT:
  822. zlog_debug ("%s: %s %s/%d: desync: RR is still in RIB, while already not in FIB",
  823. __func__, lookup (rtm_type_str, rtm->rtm_type), buf, p.prefixlen);
  824. rib_lookup_and_dump (&p);
  825. break;
  826. case ZEBRA_RIB_FOUND_CONNECTED:
  827. case ZEBRA_RIB_FOUND_NOGATE:
  828. zlog_debug ("%s: %s %s/%d: desync: RR is still in RIB, plus gate differs",
  829. __func__, lookup (rtm_type_str, rtm->rtm_type), buf, p.prefixlen);
  830. rib_lookup_and_dump (&p);
  831. break;
  832. case ZEBRA_RIB_NOTFOUND: /* RIB RR == FIB RR */
  833. zlog_debug ("%s: %s %s/%d: done Ok",
  834. __func__, lookup (rtm_type_str, rtm->rtm_type), buf, p.prefixlen);
  835. rib_lookup_and_dump (&p);
  836. return;
  837. break;
  838. }
  839. break;
  840. default:
  841. zlog_debug ("%s: %s/%d: warning: loopback RTM of type %s received",
  842. __func__, buf, p.prefixlen, lookup (rtm_type_str, rtm->rtm_type));
  843. }
  844. return;
  845. }
  846. /* Change, delete the old prefix, we have no further information
  847. * to specify the route really
  848. */
  849. if (rtm->rtm_type == RTM_CHANGE)
  850. rib_delete_ipv4 (ZEBRA_ROUTE_KERNEL, zebra_flags, &p,
  851. NULL, 0, 0, SAFI_UNICAST);
  852. if (rtm->rtm_type == RTM_GET
  853. || rtm->rtm_type == RTM_ADD
  854. || rtm->rtm_type == RTM_CHANGE)
  855. rib_add_ipv4 (ZEBRA_ROUTE_KERNEL, zebra_flags,
  856. &p, &gate.sin.sin_addr, NULL, 0, 0, 0, 0, SAFI_UNICAST);
  857. else
  858. rib_delete_ipv4 (ZEBRA_ROUTE_KERNEL, zebra_flags,
  859. &p, &gate.sin.sin_addr, 0, 0, SAFI_UNICAST);
  860. }
  861. #ifdef HAVE_IPV6
  862. if (dest.sa.sa_family == AF_INET6)
  863. {
  864. /* One day we might have a debug section here like one in the
  865. * IPv4 case above. Just ignore own messages at the moment.
  866. */
  867. if (rtm->rtm_type != RTM_GET && rtm->rtm_pid == pid)
  868. return;
  869. struct prefix_ipv6 p;
  870. unsigned int ifindex = 0;
  871. p.family = AF_INET6;
  872. p.prefix = dest.sin6.sin6_addr;
  873. if (flags & RTF_HOST)
  874. p.prefixlen = IPV6_MAX_PREFIXLEN;
  875. else
  876. p.prefixlen = ip6_masklen (mask.sin6.sin6_addr);
  877. #ifdef KAME
  878. if (IN6_IS_ADDR_LINKLOCAL (&gate.sin6.sin6_addr))
  879. {
  880. ifindex = IN6_LINKLOCAL_IFINDEX (gate.sin6.sin6_addr);
  881. SET_IN6_LINKLOCAL_IFINDEX (gate.sin6.sin6_addr, 0);
  882. }
  883. #endif /* KAME */
  884. /* CHANGE: delete the old prefix, we have no further information
  885. * to specify the route really
  886. */
  887. if (rtm->rtm_type == RTM_CHANGE)
  888. rib_delete_ipv6 (ZEBRA_ROUTE_KERNEL, zebra_flags, &p,
  889. NULL, 0, 0, SAFI_UNICAST);
  890. if (rtm->rtm_type == RTM_GET
  891. || rtm->rtm_type == RTM_ADD
  892. || rtm->rtm_type == RTM_CHANGE)
  893. rib_add_ipv6 (ZEBRA_ROUTE_KERNEL, zebra_flags,
  894. &p, &gate.sin6.sin6_addr, ifindex, 0, 0, 0, SAFI_UNICAST);
  895. else
  896. rib_delete_ipv6 (ZEBRA_ROUTE_KERNEL, zebra_flags,
  897. &p, &gate.sin6.sin6_addr, ifindex, 0, SAFI_UNICAST);
  898. }
  899. #endif /* HAVE_IPV6 */
  900. }
  901. /* Interface function for the kernel routing table updates. Support
  902. * for RTM_CHANGE will be needed.
  903. * Exported only for rt_socket.c
  904. */
  905. int
  906. rtm_write (int message,
  907. union sockunion *dest,
  908. union sockunion *mask,
  909. union sockunion *gate,
  910. unsigned int index,
  911. int zebra_flags,
  912. int metric)
  913. {
  914. int ret;
  915. caddr_t pnt;
  916. struct interface *ifp;
  917. /* Sequencial number of routing message. */
  918. static int msg_seq = 0;
  919. /* Struct of rt_msghdr and buffer for storing socket's data. */
  920. struct
  921. {
  922. struct rt_msghdr rtm;
  923. char buf[512];
  924. } msg;
  925. if (routing_sock < 0)
  926. return ZEBRA_ERR_EPERM;
  927. /* Clear and set rt_msghdr values */
  928. memset (&msg, 0, sizeof (struct rt_msghdr));
  929. msg.rtm.rtm_version = RTM_VERSION;
  930. msg.rtm.rtm_type = message;
  931. msg.rtm.rtm_seq = msg_seq++;
  932. msg.rtm.rtm_addrs = RTA_DST;
  933. msg.rtm.rtm_addrs |= RTA_GATEWAY;
  934. msg.rtm.rtm_flags = RTF_UP;
  935. msg.rtm.rtm_index = index;
  936. if (metric != 0)
  937. {
  938. msg.rtm.rtm_rmx.rmx_hopcount = metric;
  939. msg.rtm.rtm_inits |= RTV_HOPCOUNT;
  940. }
  941. ifp = if_lookup_by_index (index);
  942. if (gate && message == RTM_ADD)
  943. msg.rtm.rtm_flags |= RTF_GATEWAY;
  944. /* When RTF_CLONING is unavailable on BSD, should we set some
  945. * other flag instead?
  946. */
  947. #ifdef RTF_CLONING
  948. if (! gate && message == RTM_ADD && ifp &&
  949. (ifp->flags & IFF_POINTOPOINT) == 0)
  950. msg.rtm.rtm_flags |= RTF_CLONING;
  951. #endif /* RTF_CLONING */
  952. /* If no protocol specific gateway is specified, use link
  953. address for gateway. */
  954. if (! gate)
  955. {
  956. if (!ifp)
  957. {
  958. char dest_buf[INET_ADDRSTRLEN] = "NULL", mask_buf[INET_ADDRSTRLEN] = "255.255.255.255";
  959. if (dest)
  960. inet_ntop (AF_INET, &dest->sin.sin_addr, dest_buf, INET_ADDRSTRLEN);
  961. if (mask)
  962. inet_ntop (AF_INET, &mask->sin.sin_addr, mask_buf, INET_ADDRSTRLEN);
  963. zlog_warn ("%s: %s/%s: gate == NULL and no gateway found for ifindex %d",
  964. __func__, dest_buf, mask_buf, index);
  965. return -1;
  966. }
  967. gate = (union sockunion *) & ifp->sdl;
  968. }
  969. if (mask)
  970. msg.rtm.rtm_addrs |= RTA_NETMASK;
  971. else if (message == RTM_ADD)
  972. msg.rtm.rtm_flags |= RTF_HOST;
  973. /* Tagging route with flags */
  974. msg.rtm.rtm_flags |= (RTF_PROTO1);
  975. /* Additional flags. */
  976. if (zebra_flags & ZEBRA_FLAG_BLACKHOLE)
  977. msg.rtm.rtm_flags |= RTF_BLACKHOLE;
  978. if (zebra_flags & ZEBRA_FLAG_REJECT)
  979. msg.rtm.rtm_flags |= RTF_REJECT;
  980. #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
  981. #define SOCKADDRSET(X,R) \
  982. if (msg.rtm.rtm_addrs & (R)) \
  983. { \
  984. int len = ROUNDUP ((X)->sa.sa_len); \
  985. memcpy (pnt, (caddr_t)(X), len); \
  986. pnt += len; \
  987. }
  988. #else
  989. #define SOCKADDRSET(X,R) \
  990. if (msg.rtm.rtm_addrs & (R)) \
  991. { \
  992. int len = SAROUNDUP (X); \
  993. memcpy (pnt, (caddr_t)(X), len); \
  994. pnt += len; \
  995. }
  996. #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
  997. pnt = (caddr_t) msg.buf;
  998. /* Write each socket data into rtm message buffer */
  999. SOCKADDRSET (dest, RTA_DST);
  1000. SOCKADDRSET (gate, RTA_GATEWAY);
  1001. SOCKADDRSET (mask, RTA_NETMASK);
  1002. msg.rtm.rtm_msglen = pnt - (caddr_t) &msg;
  1003. ret = write (routing_sock, &msg, msg.rtm.rtm_msglen);
  1004. if (ret != msg.rtm.rtm_msglen)
  1005. {
  1006. if (errno == EEXIST)
  1007. return ZEBRA_ERR_RTEXIST;
  1008. if (errno == ENETUNREACH)
  1009. return ZEBRA_ERR_RTUNREACH;
  1010. if (errno == ESRCH)
  1011. return ZEBRA_ERR_RTNOEXIST;
  1012. zlog_warn ("%s: write : %s (%d)", __func__, safe_strerror (errno), errno);
  1013. return ZEBRA_ERR_KERNEL;
  1014. }
  1015. return ZEBRA_ERR_NOERROR;
  1016. }
  1017. #include "thread.h"
  1018. #include "zebra/zserv.h"
  1019. /* For debug purpose. */
  1020. static void
  1021. rtmsg_debug (struct rt_msghdr *rtm)
  1022. {
  1023. zlog_debug ("Kernel: Len: %d Type: %s", rtm->rtm_msglen, lookup (rtm_type_str, rtm->rtm_type));
  1024. rtm_flag_dump (rtm->rtm_flags);
  1025. zlog_debug ("Kernel: message seq %d", rtm->rtm_seq);
  1026. zlog_debug ("Kernel: pid %d, rtm_addrs 0x%x", rtm->rtm_pid, rtm->rtm_addrs);
  1027. }
  1028. /* This is pretty gross, better suggestions welcome -- mhandler */
  1029. #ifndef RTAX_MAX
  1030. #ifdef RTA_NUMBITS
  1031. #define RTAX_MAX RTA_NUMBITS
  1032. #else
  1033. #define RTAX_MAX 8
  1034. #endif /* RTA_NUMBITS */
  1035. #endif /* RTAX_MAX */
  1036. /* Kernel routing table and interface updates via routing socket. */
  1037. static int
  1038. kernel_read (struct thread *thread)
  1039. {
  1040. int sock;
  1041. int nbytes;
  1042. struct rt_msghdr *rtm;
  1043. /*
  1044. * This must be big enough for any message the kernel might send.
  1045. * Rather than determining how many sockaddrs of what size might be
  1046. * in each particular message, just use RTAX_MAX of sockaddr_storage
  1047. * for each. Note that the sockaddrs must be after each message
  1048. * definition, or rather after whichever happens to be the largest,
  1049. * since the buffer needs to be big enough for a message and the
  1050. * sockaddrs together.
  1051. */
  1052. union
  1053. {
  1054. /* Routing information. */
  1055. struct
  1056. {
  1057. struct rt_msghdr rtm;
  1058. struct sockaddr_storage addr[RTAX_MAX];
  1059. } r;
  1060. /* Interface information. */
  1061. struct
  1062. {
  1063. struct if_msghdr ifm;
  1064. struct sockaddr_storage addr[RTAX_MAX];
  1065. } im;
  1066. /* Interface address information. */
  1067. struct
  1068. {
  1069. struct ifa_msghdr ifa;
  1070. struct sockaddr_storage addr[RTAX_MAX];
  1071. } ia;
  1072. #ifdef RTM_IFANNOUNCE
  1073. /* Interface arrival/departure */
  1074. struct
  1075. {
  1076. struct if_announcemsghdr ifan;
  1077. struct sockaddr_storage addr[RTAX_MAX];
  1078. } ian;
  1079. #endif /* RTM_IFANNOUNCE */
  1080. } buf;
  1081. /* Fetch routing socket. */
  1082. sock = THREAD_FD (thread);
  1083. nbytes= read (sock, &buf, sizeof buf);
  1084. if (nbytes <= 0)
  1085. {
  1086. if (nbytes < 0 && errno != EWOULDBLOCK && errno != EAGAIN)
  1087. zlog_warn ("routing socket error: %s", safe_strerror (errno));
  1088. return 0;
  1089. }
  1090. thread_add_read (zebrad.master, kernel_read, NULL, sock);
  1091. if (IS_ZEBRA_DEBUG_KERNEL)
  1092. rtmsg_debug (&buf.r.rtm);
  1093. rtm = &buf.r.rtm;
  1094. /*
  1095. * Ensure that we didn't drop any data, so that processing routines
  1096. * can assume they have the whole message.
  1097. */
  1098. if (rtm->rtm_msglen != nbytes)
  1099. {
  1100. zlog_warn ("kernel_read: rtm->rtm_msglen %d, nbytes %d, type %d\n",
  1101. rtm->rtm_msglen, nbytes, rtm->rtm_type);
  1102. return -1;
  1103. }
  1104. switch (rtm->rtm_type)
  1105. {
  1106. case RTM_ADD:
  1107. case RTM_DELETE:
  1108. case RTM_CHANGE:
  1109. rtm_read (rtm);
  1110. break;
  1111. case RTM_IFINFO:
  1112. ifm_read (&buf.im.ifm);
  1113. break;
  1114. case RTM_NEWADDR:
  1115. case RTM_DELADDR:
  1116. ifam_read (&buf.ia.ifa);
  1117. break;
  1118. #ifdef RTM_IFANNOUNCE
  1119. case RTM_IFANNOUNCE:
  1120. ifan_read (&buf.ian.ifan);
  1121. break;
  1122. #endif /* RTM_IFANNOUNCE */
  1123. default:
  1124. if (IS_ZEBRA_DEBUG_KERNEL)
  1125. zlog_debug("Unprocessed RTM_type: %d", rtm->rtm_type);
  1126. break;
  1127. }
  1128. return 0;
  1129. }
  1130. /* Make routing socket. */
  1131. static void
  1132. routing_socket (void)
  1133. {
  1134. if ( zserv_privs.change (ZPRIVS_RAISE) )
  1135. zlog_err ("routing_socket: Can't raise privileges");
  1136. routing_sock = socket (AF_ROUTE, SOCK_RAW, 0);
  1137. if (routing_sock < 0)
  1138. {
  1139. if ( zserv_privs.change (ZPRIVS_LOWER) )
  1140. zlog_err ("routing_socket: Can't lower privileges");
  1141. zlog_warn ("Can't init kernel routing socket");
  1142. return;
  1143. }
  1144. /* XXX: Socket should be NONBLOCK, however as we currently
  1145. * discard failed writes, this will lead to inconsistencies.
  1146. * For now, socket must be blocking.
  1147. */
  1148. /*if (fcntl (routing_sock, F_SETFL, O_NONBLOCK) < 0)
  1149. zlog_warn ("Can't set O_NONBLOCK to routing socket");*/
  1150. if ( zserv_privs.change (ZPRIVS_LOWER) )
  1151. zlog_err ("routing_socket: Can't lower privileges");
  1152. /* kernel_read needs rewrite. */
  1153. thread_add_read (zebrad.master, kernel_read, NULL, routing_sock);
  1154. }
  1155. /* Exported interface function. This function simply calls
  1156. routing_socket (). */
  1157. void
  1158. kernel_init (void)
  1159. {
  1160. routing_socket ();
  1161. }