kernel_socket.c 38 KB

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