kernel_socket.c 40 KB

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