kernel_socket.c 39 KB

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