kernel_socket.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987
  1. /* Kernel communication using routing socket.
  2. * Copyright (C) 1999 Kunihiro Ishiguro
  3. *
  4. * This file is part of GNU Zebra.
  5. *
  6. * GNU Zebra is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the
  8. * Free Software Foundation; either version 2, or (at your option) any
  9. * later version.
  10. *
  11. * GNU Zebra is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with GNU Zebra; see the file COPYING. If not, write to the Free
  18. * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  19. * 02111-1307, USA.
  20. */
  21. #include <zebra.h>
  22. #include "if.h"
  23. #include "prefix.h"
  24. #include "sockunion.h"
  25. #include "connected.h"
  26. #include "memory.h"
  27. #include "ioctl.h"
  28. #include "log.h"
  29. #include "str.h"
  30. #include "table.h"
  31. #include "rib.h"
  32. #include "privs.h"
  33. #include "zebra/interface.h"
  34. #include "zebra/zserv.h"
  35. #include "zebra/debug.h"
  36. extern struct zebra_privs_t zserv_privs;
  37. extern struct zebra_t zebrad;
  38. /*
  39. * Given a sockaddr length, round it up to include pad bytes following
  40. * it. Assumes the kernel pads to sizeof(long).
  41. *
  42. * XXX: why is ROUNDUP(0) sizeof(long)? 0 is an illegal sockaddr
  43. * length anyway (< sizeof (struct sockaddr)), so this shouldn't
  44. * matter.
  45. */
  46. #define ROUNDUP(a) \
  47. ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
  48. /*
  49. * Given a pointer (sockaddr or void *), return the number of bytes
  50. * taken up by the sockaddr and any padding needed for alignment.
  51. */
  52. #if defined(HAVE_SA_LEN)
  53. #define SAROUNDUP(X) ROUNDUP(((struct sockaddr *)(X))->sa_len)
  54. #elif defined(HAVE_IPV6)
  55. /*
  56. * One would hope all fixed-size structure definitions are aligned,
  57. * but round them up nonetheless.
  58. */
  59. #define SAROUNDUP(X) \
  60. (((struct sockaddr *)(X))->sa_family == AF_INET ? \
  61. ROUNDUP(sizeof(struct sockaddr_in)):\
  62. (((struct sockaddr *)(X))->sa_family == AF_INET6 ? \
  63. ROUNDUP(sizeof(struct sockaddr_in6)) : \
  64. (((struct sockaddr *)(X))->sa_family == AF_LINK ? \
  65. ROUNDUP(sizeof(struct sockaddr_dl)) : sizeof(struct sockaddr))))
  66. #else /* HAVE_IPV6 */
  67. #define SAROUNDUP(X) \
  68. (((struct sockaddr *)(X))->sa_family == AF_INET ? \
  69. ROUNDUP(sizeof(struct sockaddr_in)):\
  70. (((struct sockaddr *)(X))->sa_family == AF_LINK ? \
  71. ROUNDUP(sizeof(struct sockaddr_dl)) : sizeof(struct sockaddr)))
  72. #endif /* HAVE_SA_LEN */
  73. /* Routing socket message types. */
  74. struct message rtm_type_str[] =
  75. {
  76. {RTM_ADD, "RTM_ADD"},
  77. {RTM_DELETE, "RTM_DELETE"},
  78. {RTM_CHANGE, "RTM_CHANGE"},
  79. {RTM_GET, "RTM_GET"},
  80. {RTM_LOSING, "RTM_LOSING"},
  81. {RTM_REDIRECT, "RTM_REDIRECT"},
  82. {RTM_MISS, "RTM_MISS"},
  83. {RTM_LOCK, "RTM_LOCK"},
  84. {RTM_OLDADD, "RTM_OLDADD"},
  85. {RTM_OLDDEL, "RTM_OLDDEL"},
  86. {RTM_RESOLVE, "RTM_RESOLVE"},
  87. {RTM_NEWADDR, "RTM_NEWADDR"},
  88. {RTM_DELADDR, "RTM_DELADDR"},
  89. {RTM_IFINFO, "RTM_IFINFO"},
  90. #ifdef RTM_OIFINFO
  91. {RTM_OIFINFO, "RTM_OIFINFO"},
  92. #endif /* RTM_OIFINFO */
  93. #ifdef RTM_NEWMADDR
  94. {RTM_NEWMADDR, "RTM_NEWMADDR"},
  95. #endif /* RTM_NEWMADDR */
  96. #ifdef RTM_DELMADDR
  97. {RTM_DELMADDR, "RTM_DELMADDR"},
  98. #endif /* RTM_DELMADDR */
  99. #ifdef RTM_IFANNOUNCE
  100. {RTM_IFANNOUNCE, "RTM_IFANNOUNCE"},
  101. #endif /* RTM_IFANNOUNCE */
  102. {0, NULL}
  103. };
  104. struct message rtm_flag_str[] =
  105. {
  106. {RTF_UP, "UP"},
  107. {RTF_GATEWAY, "GATEWAY"},
  108. {RTF_HOST, "HOST"},
  109. {RTF_REJECT, "REJECT"},
  110. {RTF_DYNAMIC, "DYNAMIC"},
  111. {RTF_MODIFIED, "MODIFIED"},
  112. {RTF_DONE, "DONE"},
  113. #ifdef RTF_MASK
  114. {RTF_MASK, "MASK"},
  115. #endif /* RTF_MASK */
  116. {RTF_CLONING, "CLONING"},
  117. {RTF_XRESOLVE, "XRESOLVE"},
  118. {RTF_LLINFO, "LLINFO"},
  119. {RTF_STATIC, "STATIC"},
  120. {RTF_BLACKHOLE, "BLACKHOLE"},
  121. {RTF_PROTO1, "PROTO1"},
  122. {RTF_PROTO2, "PROTO2"},
  123. #ifdef RTF_PRCLONING
  124. {RTF_PRCLONING, "PRCLONING"},
  125. #endif /* RTF_PRCLONING */
  126. #ifdef RTF_WASCLONED
  127. {RTF_WASCLONED, "WASCLONED"},
  128. #endif /* RTF_WASCLONED */
  129. #ifdef RTF_PROTO3
  130. {RTF_PROTO3, "PROTO3"},
  131. #endif /* RTF_PROTO3 */
  132. #ifdef RTF_PINNED
  133. {RTF_PINNED, "PINNED"},
  134. #endif /* RTF_PINNED */
  135. #ifdef RTF_LOCAL
  136. {RTF_LOCAL, "LOCAL"},
  137. #endif /* RTF_LOCAL */
  138. #ifdef RTF_BROADCAST
  139. {RTF_BROADCAST, "BROADCAST"},
  140. #endif /* RTF_BROADCAST */
  141. #ifdef RTF_MULTICAST
  142. {RTF_MULTICAST, "MULTICAST"},
  143. #endif /* RTF_MULTICAST */
  144. {0, NULL}
  145. };
  146. /* Kernel routing update socket. */
  147. int routing_sock = -1;
  148. /* Yes I'm checking ugly routing socket behavior. */
  149. /* #define DEBUG */
  150. /* Supported address family check. */
  151. static int
  152. af_check (int family)
  153. {
  154. if (family == AF_INET)
  155. return 1;
  156. #ifdef HAVE_IPV6
  157. if (family == AF_INET6)
  158. return 1;
  159. #endif /* HAVE_IPV6 */
  160. return 0;
  161. }
  162. /* Dump routing table flag for debug purpose. */
  163. static void
  164. rtm_flag_dump (int flag)
  165. {
  166. struct message *mes;
  167. static char buf[BUFSIZ];
  168. buf[0] = '\0';
  169. for (mes = rtm_flag_str; mes->key != 0; mes++)
  170. {
  171. if (mes->key & flag)
  172. {
  173. strlcat (buf, mes->str, BUFSIZ);
  174. strlcat (buf, " ", BUFSIZ);
  175. }
  176. }
  177. zlog_debug ("Kernel: %s", buf);
  178. }
  179. #ifdef RTM_IFANNOUNCE
  180. /* Interface adding function */
  181. int
  182. ifan_read (struct if_announcemsghdr *ifan)
  183. {
  184. struct interface *ifp;
  185. ifp = if_lookup_by_index (ifan->ifan_index);
  186. if (ifp == NULL && ifan->ifan_what == IFAN_ARRIVAL)
  187. {
  188. /* Create Interface */
  189. ifp = if_get_by_name_len(ifan->ifan_name,
  190. strnlen(ifan->ifan_name,
  191. sizeof(ifan->ifan_name)));
  192. ifp->ifindex = ifan->ifan_index;
  193. if_add_update (ifp);
  194. }
  195. else if (ifp != NULL && ifan->ifan_what == IFAN_DEPARTURE)
  196. {
  197. if_delete_update (ifp);
  198. if_delete (ifp);
  199. }
  200. if_get_flags (ifp);
  201. if_get_mtu (ifp);
  202. if_get_metric (ifp);
  203. if (IS_ZEBRA_DEBUG_KERNEL)
  204. zlog_debug ("interface %s index %d", ifp->name, ifp->ifindex);
  205. return 0;
  206. }
  207. #endif /* RTM_IFANNOUNCE */
  208. /*
  209. * Handle struct if_msghdr obtained from reading routing socket or
  210. * sysctl (from interface_list). There may or may not be sockaddrs
  211. * present after the header.
  212. */
  213. int
  214. ifm_read (struct if_msghdr *ifm)
  215. {
  216. struct interface *ifp = NULL;
  217. struct sockaddr_dl *sdl = NULL;
  218. void *cp;
  219. unsigned int i;
  220. /* paranoia: sanity check structure */
  221. if (ifm->ifm_msglen < sizeof(struct if_msghdr))
  222. {
  223. zlog_err ("ifm_read: ifm->ifm_msglen %d too short\n",
  224. ifm->ifm_msglen);
  225. return -1;
  226. }
  227. /*
  228. * Check for a sockaddr_dl following the message. First, point to
  229. * where a socakddr might be if one follows the message.
  230. */
  231. cp = (void *)(ifm + 1);
  232. #ifdef SUNOS_5
  233. /*
  234. * XXX This behavior should be narrowed to only the kernel versions
  235. * for which the structures returned do not match the headers.
  236. *
  237. * if_msghdr_t on 64 bit kernels in Solaris 9 and earlier versions
  238. * is 12 bytes larger than the 32 bit version.
  239. */
  240. if (((struct sockaddr *) cp)->sa_family == AF_UNSPEC)
  241. cp = cp + 12;
  242. #endif
  243. /*
  244. * Check for each sockaddr in turn, advancing over it. After this
  245. * loop, sdl should point to a sockaddr_dl iff one was present.
  246. */
  247. for (i = 1; i != 0; i <<= 1)
  248. {
  249. if (i & ifm->ifm_addrs)
  250. {
  251. if (i == RTA_IFP)
  252. {
  253. sdl = (struct sockaddr_dl *)cp;
  254. break;
  255. }
  256. /* XXX warning: pointer of type `void *' used in arithmetic */
  257. cp += SAROUNDUP(cp);
  258. }
  259. }
  260. /* Ensure that sdl, if present, is actually a sockaddr_dl. */
  261. if (sdl != NULL && sdl->sdl_family != AF_LINK)
  262. {
  263. zlog_err ("ifm_read: sockaddr_dl bad AF %d\n",
  264. sdl->sdl_family);
  265. return -1;
  266. }
  267. /*
  268. * Look up on ifindex first, because ifindices are the primary
  269. * handle for interfaces across the user/kernel boundary. (Some
  270. * messages, such as up/down status changes on NetBSD, do not
  271. * include a sockaddr_dl).
  272. */
  273. ifp = if_lookup_by_index (ifm->ifm_index);
  274. /*
  275. * If lookup by index was unsuccessful and we have a name, try
  276. * looking up by name. Interfaces specified in the configuration
  277. * file for which the ifindex has not been determined will have
  278. * ifindex == IFINDEX_INTERNAL, and such interfaces are found by this search,
  279. * and then their ifindex values can be filled in.
  280. */
  281. if (ifp == NULL && sdl != NULL)
  282. {
  283. /*
  284. * paranoia: sanity check name length. nlen does not include
  285. * trailing zero, but IFNAMSIZ max length does.
  286. *
  287. * XXX Is this test correct? Should it be '>=' or '>'? And is it even
  288. * necessary now that we are using if_lookup_by_name_len?
  289. */
  290. if (sdl->sdl_nlen >= IFNAMSIZ)
  291. {
  292. zlog_err ("ifm_read: illegal sdl_nlen %d\n", sdl->sdl_nlen);
  293. return -1;
  294. }
  295. ifp = if_lookup_by_name_len (sdl->sdl_data, sdl->sdl_nlen);
  296. }
  297. /*
  298. * If ifp does not exist or has an invalid index (IFINDEX_INTERNAL), create or
  299. * fill in an interface.
  300. */
  301. if ((ifp == NULL) || (ifp->ifindex == IFINDEX_INTERNAL))
  302. {
  303. /*
  304. * To create or fill in an interface, a sockaddr_dl (via
  305. * RTA_IFP) is required.
  306. */
  307. if (sdl == NULL)
  308. {
  309. zlog_warn ("Interface index %d (new) missing RTA_IFP sockaddr_dl\n",
  310. ifm->ifm_index);
  311. return -1;
  312. }
  313. if (ifp == NULL)
  314. /* Interface that zebra was not previously aware of, so create. */
  315. ifp = if_create (sdl->sdl_data, sdl->sdl_nlen);
  316. /*
  317. * Fill in newly created interface structure, or larval
  318. * structure with ifindex IFINDEX_INTERNAL.
  319. */
  320. ifp->ifindex = ifm->ifm_index;
  321. ifp->flags = ifm->ifm_flags;
  322. #if defined(__bsdi__)
  323. if_kvm_get_mtu (ifp);
  324. #else
  325. if_get_mtu (ifp);
  326. #endif /* __bsdi__ */
  327. if_get_metric (ifp);
  328. /*
  329. * XXX sockaddr_dl contents can be larger than the structure
  330. * definition, so the user of the stored structure must be
  331. * careful not to read off the end.
  332. */
  333. memcpy (&ifp->sdl, sdl, sizeof (struct sockaddr_dl));
  334. if_add_update (ifp);
  335. }
  336. else
  337. /*
  338. * Interface structure exists. Adjust stored flags from
  339. * notification. If interface has up->down or down->up
  340. * transition, call state change routines (to adjust routes,
  341. * notify routing daemons, etc.). (Other flag changes are stored
  342. * but apparently do not trigger action.)
  343. */
  344. {
  345. if (if_is_up (ifp))
  346. {
  347. ifp->flags = ifm->ifm_flags;
  348. if (! if_is_up (ifp))
  349. if_down (ifp);
  350. }
  351. else
  352. {
  353. ifp->flags = ifm->ifm_flags;
  354. if (if_is_up (ifp))
  355. if_up (ifp);
  356. }
  357. }
  358. #ifdef HAVE_NET_RT_IFLIST
  359. ifp->stats = ifm->ifm_data;
  360. #endif /* HAVE_NET_RT_IFLIST */
  361. if (IS_ZEBRA_DEBUG_KERNEL)
  362. zlog_debug ("interface %s index %d", ifp->name, ifp->ifindex);
  363. return 0;
  364. }
  365. /* Address read from struct ifa_msghdr. */
  366. void
  367. ifam_read_mesg (struct ifa_msghdr *ifm,
  368. union sockunion *addr,
  369. union sockunion *mask,
  370. union sockunion *dest)
  371. {
  372. caddr_t pnt, end;
  373. pnt = (caddr_t)(ifm + 1);
  374. end = ((caddr_t)ifm) + ifm->ifam_msglen;
  375. #define IFAMADDRGET(X,R) \
  376. if (ifm->ifam_addrs & (R)) \
  377. { \
  378. int len = SAROUNDUP(pnt); \
  379. if (((X) != NULL) && af_check (((struct sockaddr *)pnt)->sa_family)) \
  380. memcpy ((caddr_t)(X), pnt, len); \
  381. pnt += len; \
  382. }
  383. #define IFAMMASKGET(X,R) \
  384. if (ifm->ifam_addrs & (R)) \
  385. { \
  386. int len = SAROUNDUP(pnt); \
  387. if ((X) != NULL) \
  388. memcpy ((caddr_t)(X), pnt, len); \
  389. pnt += len; \
  390. }
  391. /* Be sure structure is cleared */
  392. memset (mask, 0, sizeof (union sockunion));
  393. memset (addr, 0, sizeof (union sockunion));
  394. memset (dest, 0, sizeof (union sockunion));
  395. /* We fetch each socket variable into sockunion. */
  396. IFAMADDRGET (NULL, RTA_DST);
  397. IFAMADDRGET (NULL, RTA_GATEWAY);
  398. IFAMMASKGET (mask, RTA_NETMASK);
  399. IFAMADDRGET (NULL, RTA_GENMASK);
  400. IFAMADDRGET (NULL, RTA_IFP);
  401. IFAMADDRGET (addr, RTA_IFA);
  402. IFAMADDRGET (NULL, RTA_AUTHOR);
  403. IFAMADDRGET (dest, RTA_BRD);
  404. /* Assert read up end point matches to end point */
  405. if (pnt != end)
  406. zlog_warn ("ifam_read() does't read all socket data");
  407. }
  408. /* Interface's address information get. */
  409. int
  410. ifam_read (struct ifa_msghdr *ifam)
  411. {
  412. struct interface *ifp;
  413. union sockunion addr, mask, gate;
  414. /* Check does this interface exist or not. */
  415. ifp = if_lookup_by_index (ifam->ifam_index);
  416. if (ifp == NULL)
  417. {
  418. zlog_warn ("no interface for index %d", ifam->ifam_index);
  419. return -1;
  420. }
  421. /* Allocate and read address information. */
  422. ifam_read_mesg (ifam, &addr, &mask, &gate);
  423. /* Check interface flag for implicit up of the interface. */
  424. if_refresh (ifp);
  425. /* Add connected address. */
  426. switch (sockunion_family (&addr))
  427. {
  428. case AF_INET:
  429. if (ifam->ifam_type == RTM_NEWADDR)
  430. connected_add_ipv4 (ifp, 0, &addr.sin.sin_addr,
  431. ip_masklen (mask.sin.sin_addr),
  432. &gate.sin.sin_addr, NULL);
  433. else
  434. connected_delete_ipv4 (ifp, 0, &addr.sin.sin_addr,
  435. ip_masklen (mask.sin.sin_addr),
  436. &gate.sin.sin_addr, NULL);
  437. break;
  438. #ifdef HAVE_IPV6
  439. case AF_INET6:
  440. /* Unset interface index from link-local address when IPv6 stack
  441. is KAME. */
  442. if (IN6_IS_ADDR_LINKLOCAL (&addr.sin6.sin6_addr))
  443. SET_IN6_LINKLOCAL_IFINDEX (addr.sin6.sin6_addr, 0);
  444. if (ifam->ifam_type == RTM_NEWADDR)
  445. connected_add_ipv6 (ifp,
  446. &addr.sin6.sin6_addr,
  447. ip6_masklen (mask.sin6.sin6_addr),
  448. &gate.sin6.sin6_addr);
  449. else
  450. connected_delete_ipv6 (ifp,
  451. &addr.sin6.sin6_addr,
  452. ip6_masklen (mask.sin6.sin6_addr),
  453. &gate.sin6.sin6_addr);
  454. break;
  455. #endif /* HAVE_IPV6 */
  456. default:
  457. /* Unsupported family silently ignore... */
  458. break;
  459. }
  460. return 0;
  461. }
  462. /* Interface function for reading kernel routing table information. */
  463. int
  464. rtm_read_mesg (struct rt_msghdr *rtm,
  465. union sockunion *dest,
  466. union sockunion *mask,
  467. union sockunion *gate)
  468. {
  469. caddr_t pnt, end;
  470. /* Pnt points out socket data start point. */
  471. pnt = (caddr_t)(rtm + 1);
  472. end = ((caddr_t)rtm) + rtm->rtm_msglen;
  473. /* rt_msghdr version check. */
  474. if (rtm->rtm_version != RTM_VERSION)
  475. zlog (NULL, LOG_WARNING,
  476. "Routing message version different %d should be %d."
  477. "This may cause problem\n", rtm->rtm_version, RTM_VERSION);
  478. #define RTMADDRGET(X,R) \
  479. if (rtm->rtm_addrs & (R)) \
  480. { \
  481. int len = SAROUNDUP (pnt); \
  482. if (((X) != NULL) && af_check (((struct sockaddr *)pnt)->sa_family)) \
  483. memcpy ((caddr_t)(X), pnt, len); \
  484. pnt += len; \
  485. }
  486. #define RTMMASKGET(X,R) \
  487. if (rtm->rtm_addrs & (R)) \
  488. { \
  489. int len = SAROUNDUP (pnt); \
  490. if ((X) != NULL) \
  491. memcpy ((caddr_t)(X), pnt, len); \
  492. pnt += len; \
  493. }
  494. /* Be sure structure is cleared */
  495. memset (dest, 0, sizeof (union sockunion));
  496. memset (gate, 0, sizeof (union sockunion));
  497. memset (mask, 0, sizeof (union sockunion));
  498. /* We fetch each socket variable into sockunion. */
  499. RTMADDRGET (dest, RTA_DST);
  500. RTMADDRGET (gate, RTA_GATEWAY);
  501. RTMMASKGET (mask, RTA_NETMASK);
  502. RTMADDRGET (NULL, RTA_GENMASK);
  503. RTMADDRGET (NULL, RTA_IFP);
  504. RTMADDRGET (NULL, RTA_IFA);
  505. RTMADDRGET (NULL, RTA_AUTHOR);
  506. RTMADDRGET (NULL, RTA_BRD);
  507. /* If there is netmask information set it's family same as
  508. destination family*/
  509. if (rtm->rtm_addrs & RTA_NETMASK)
  510. mask->sa.sa_family = dest->sa.sa_family;
  511. /* Assert read up to the end of pointer. */
  512. if (pnt != end)
  513. zlog (NULL, LOG_WARNING, "rtm_read() does't read all socket data.");
  514. return rtm->rtm_flags;
  515. }
  516. void
  517. rtm_read (struct rt_msghdr *rtm)
  518. {
  519. int flags;
  520. u_char zebra_flags;
  521. union sockunion dest, mask, gate;
  522. zebra_flags = 0;
  523. /* Discard self send message. */
  524. if (rtm->rtm_type != RTM_GET
  525. && (rtm->rtm_pid == pid || rtm->rtm_pid == old_pid))
  526. return;
  527. /* Read destination and netmask and gateway from rtm message
  528. structure. */
  529. flags = rtm_read_mesg (rtm, &dest, &mask, &gate);
  530. #ifdef RTF_CLONED /*bsdi, netbsd 1.6*/
  531. if (flags & RTF_CLONED)
  532. return;
  533. #endif
  534. #ifdef RTF_WASCLONED /*freebsd*/
  535. if (flags & RTF_WASCLONED)
  536. return;
  537. #endif
  538. if ((rtm->rtm_type == RTM_ADD) && ! (flags & RTF_UP))
  539. return;
  540. /* This is connected route. */
  541. if (! (flags & RTF_GATEWAY))
  542. return;
  543. if (flags & RTF_PROTO1)
  544. SET_FLAG (zebra_flags, ZEBRA_FLAG_SELFROUTE);
  545. /* This is persistent route. */
  546. if (flags & RTF_STATIC)
  547. SET_FLAG (zebra_flags, ZEBRA_FLAG_STATIC);
  548. /* This is a reject or blackhole route */
  549. if (flags & RTF_REJECT)
  550. SET_FLAG (zebra_flags, ZEBRA_FLAG_REJECT);
  551. if (flags & RTF_BLACKHOLE)
  552. SET_FLAG (zebra_flags, ZEBRA_FLAG_BLACKHOLE);
  553. if (dest.sa.sa_family == AF_INET)
  554. {
  555. struct prefix_ipv4 p;
  556. p.family = AF_INET;
  557. p.prefix = dest.sin.sin_addr;
  558. if (flags & RTF_HOST)
  559. p.prefixlen = IPV4_MAX_PREFIXLEN;
  560. else
  561. p.prefixlen = ip_masklen (mask.sin.sin_addr);
  562. if (rtm->rtm_type == RTM_GET || rtm->rtm_type == RTM_ADD)
  563. rib_add_ipv4 (ZEBRA_ROUTE_KERNEL, zebra_flags,
  564. &p, &gate.sin.sin_addr, 0, 0, 0, 0);
  565. else
  566. rib_delete_ipv4 (ZEBRA_ROUTE_KERNEL, zebra_flags,
  567. &p, &gate.sin.sin_addr, 0, 0);
  568. }
  569. #ifdef HAVE_IPV6
  570. if (dest.sa.sa_family == AF_INET6)
  571. {
  572. struct prefix_ipv6 p;
  573. unsigned int ifindex = 0;
  574. p.family = AF_INET6;
  575. p.prefix = dest.sin6.sin6_addr;
  576. if (flags & RTF_HOST)
  577. p.prefixlen = IPV6_MAX_PREFIXLEN;
  578. else
  579. p.prefixlen = ip6_masklen (mask.sin6.sin6_addr);
  580. #ifdef KAME
  581. if (IN6_IS_ADDR_LINKLOCAL (&gate.sin6.sin6_addr))
  582. {
  583. ifindex = IN6_LINKLOCAL_IFINDEX (gate.sin6.sin6_addr);
  584. SET_IN6_LINKLOCAL_IFINDEX (gate.sin6.sin6_addr, 0);
  585. }
  586. #endif /* KAME */
  587. if (rtm->rtm_type == RTM_GET || rtm->rtm_type == RTM_ADD)
  588. rib_add_ipv6 (ZEBRA_ROUTE_KERNEL, zebra_flags,
  589. &p, &gate.sin6.sin6_addr, ifindex, 0);
  590. else
  591. rib_delete_ipv6 (ZEBRA_ROUTE_KERNEL, zebra_flags,
  592. &p, &gate.sin6.sin6_addr, ifindex, 0);
  593. }
  594. #endif /* HAVE_IPV6 */
  595. }
  596. /* Interface function for the kernel routing table updates. Support
  597. for RTM_CHANGE will be needed. */
  598. int
  599. rtm_write (int message,
  600. union sockunion *dest,
  601. union sockunion *mask,
  602. union sockunion *gate,
  603. unsigned int index,
  604. int zebra_flags,
  605. int metric)
  606. {
  607. int ret;
  608. caddr_t pnt;
  609. struct interface *ifp;
  610. struct sockaddr_in tmp_gate;
  611. #ifdef HAVE_IPV6
  612. struct sockaddr_in6 tmp_gate6;
  613. #endif /* HAVE_IPV6 */
  614. /* Sequencial number of routing message. */
  615. static int msg_seq = 0;
  616. /* Struct of rt_msghdr and buffer for storing socket's data. */
  617. struct
  618. {
  619. struct rt_msghdr rtm;
  620. char buf[512];
  621. } msg;
  622. memset (&tmp_gate, 0, sizeof (struct sockaddr_in));
  623. tmp_gate.sin_family = AF_INET;
  624. #ifdef HAVE_SIN_LEN
  625. tmp_gate.sin_len = sizeof (struct sockaddr_in);
  626. #endif /* HAVE_SIN_LEN */
  627. #ifdef HAVE_IPV6
  628. memset (&tmp_gate6, 0, sizeof (struct sockaddr_in6));
  629. tmp_gate6.sin6_family = AF_INET6;
  630. #ifdef SIN6_LEN
  631. tmp_gate6.sin6_len = sizeof (struct sockaddr_in6);
  632. #endif /* SIN6_LEN */
  633. #endif /* HAVE_IPV6 */
  634. if (routing_sock < 0)
  635. return ZEBRA_ERR_EPERM;
  636. /* Clear and set rt_msghdr values */
  637. memset (&msg, 0, sizeof (struct rt_msghdr));
  638. msg.rtm.rtm_version = RTM_VERSION;
  639. msg.rtm.rtm_type = message;
  640. msg.rtm.rtm_seq = msg_seq++;
  641. msg.rtm.rtm_addrs = RTA_DST;
  642. msg.rtm.rtm_addrs |= RTA_GATEWAY;
  643. msg.rtm.rtm_flags = RTF_UP;
  644. msg.rtm.rtm_index = index;
  645. if (metric != 0)
  646. {
  647. msg.rtm.rtm_rmx.rmx_hopcount = metric;
  648. msg.rtm.rtm_inits |= RTV_HOPCOUNT;
  649. }
  650. ifp = if_lookup_by_index (index);
  651. if (gate && message == RTM_ADD)
  652. msg.rtm.rtm_flags |= RTF_GATEWAY;
  653. if (! gate && message == RTM_ADD && ifp &&
  654. (ifp->flags & IFF_POINTOPOINT) == 0)
  655. msg.rtm.rtm_flags |= RTF_CLONING;
  656. /* If no protocol specific gateway is specified, use link
  657. address for gateway. */
  658. if (! gate)
  659. {
  660. if (!ifp)
  661. {
  662. zlog_warn ("no gateway found for interface index %d", index);
  663. return -1;
  664. }
  665. gate = (union sockunion *) & ifp->sdl;
  666. }
  667. if (mask)
  668. msg.rtm.rtm_addrs |= RTA_NETMASK;
  669. else if (message == RTM_ADD)
  670. msg.rtm.rtm_flags |= RTF_HOST;
  671. /* Tagging route with flags */
  672. msg.rtm.rtm_flags |= (RTF_PROTO1);
  673. /* Additional flags. */
  674. if (zebra_flags & ZEBRA_FLAG_BLACKHOLE)
  675. msg.rtm.rtm_flags |= RTF_BLACKHOLE;
  676. if (zebra_flags & ZEBRA_FLAG_REJECT)
  677. msg.rtm.rtm_flags |= RTF_REJECT;
  678. #ifdef HAVE_SIN_LEN
  679. #define SOCKADDRSET(X,R) \
  680. if (msg.rtm.rtm_addrs & (R)) \
  681. { \
  682. int len = ROUNDUP ((X)->sa.sa_len); \
  683. memcpy (pnt, (caddr_t)(X), len); \
  684. pnt += len; \
  685. }
  686. #else
  687. #define SOCKADDRSET(X,R) \
  688. if (msg.rtm.rtm_addrs & (R)) \
  689. { \
  690. int len = ROUNDUP (sizeof((X)->sa)); \
  691. memcpy (pnt, (caddr_t)(X), len); \
  692. pnt += len; \
  693. }
  694. #endif /* HAVE_SIN_LEN */
  695. pnt = (caddr_t) msg.buf;
  696. /* Write each socket data into rtm message buffer */
  697. SOCKADDRSET (dest, RTA_DST);
  698. SOCKADDRSET (gate, RTA_GATEWAY);
  699. SOCKADDRSET (mask, RTA_NETMASK);
  700. msg.rtm.rtm_msglen = pnt - (caddr_t) &msg;
  701. ret = write (routing_sock, &msg, msg.rtm.rtm_msglen);
  702. if (ret != msg.rtm.rtm_msglen)
  703. {
  704. if (errno == EEXIST)
  705. return ZEBRA_ERR_RTEXIST;
  706. if (errno == ENETUNREACH)
  707. return ZEBRA_ERR_RTUNREACH;
  708. zlog_warn ("write : %s (%d)", safe_strerror (errno), errno);
  709. return -1;
  710. }
  711. return 0;
  712. }
  713. #include "thread.h"
  714. #include "zebra/zserv.h"
  715. /* For debug purpose. */
  716. static void
  717. rtmsg_debug (struct rt_msghdr *rtm)
  718. {
  719. const char *type = "Unknown";
  720. struct message *mes;
  721. for (mes = rtm_type_str; mes->str; mes++)
  722. if (mes->key == rtm->rtm_type)
  723. {
  724. type = mes->str;
  725. break;
  726. }
  727. zlog_debug ("Kernel: Len: %d Type: %s", rtm->rtm_msglen, type);
  728. rtm_flag_dump (rtm->rtm_flags);
  729. zlog_debug ("Kernel: message seq %d", rtm->rtm_seq);
  730. zlog_debug ("Kernel: pid %d", rtm->rtm_pid);
  731. }
  732. /* This is pretty gross, better suggestions welcome -- mhandler */
  733. #ifndef RTAX_MAX
  734. #ifdef RTA_NUMBITS
  735. #define RTAX_MAX RTA_NUMBITS
  736. #else
  737. #define RTAX_MAX 8
  738. #endif /* RTA_NUMBITS */
  739. #endif /* RTAX_MAX */
  740. /* Kernel routing table and interface updates via routing socket. */
  741. int
  742. kernel_read (struct thread *thread)
  743. {
  744. int sock;
  745. int nbytes;
  746. struct rt_msghdr *rtm;
  747. /*
  748. * This must be big enough for any message the kernel might send.
  749. * Rather than determining how many sockaddrs of what size might be
  750. * in each particular message, just use RTAX_MAX of sockaddr_storage
  751. * for each. Note that the sockaddrs must be after each message
  752. * definition, or rather after whichever happens to be the largest,
  753. * since the buffer needs to be big enough for a message and the
  754. * sockaddrs together.
  755. */
  756. union
  757. {
  758. /* Routing information. */
  759. struct
  760. {
  761. struct rt_msghdr rtm;
  762. struct sockaddr_storage addr[RTAX_MAX];
  763. } r;
  764. /* Interface information. */
  765. struct
  766. {
  767. struct if_msghdr ifm;
  768. struct sockaddr_storage addr[RTAX_MAX];
  769. } im;
  770. /* Interface address information. */
  771. struct
  772. {
  773. struct ifa_msghdr ifa;
  774. struct sockaddr_storage addr[RTAX_MAX];
  775. } ia;
  776. #ifdef RTM_IFANNOUNCE
  777. /* Interface arrival/departure */
  778. struct
  779. {
  780. struct if_announcemsghdr ifan;
  781. struct sockaddr_storage addr[RTAX_MAX];
  782. } ian;
  783. #endif /* RTM_IFANNOUNCE */
  784. } buf;
  785. /* Fetch routing socket. */
  786. sock = THREAD_FD (thread);
  787. nbytes= read (sock, &buf, sizeof buf);
  788. if (nbytes <= 0)
  789. {
  790. if (nbytes < 0 && errno != EWOULDBLOCK && errno != EAGAIN)
  791. zlog_warn ("routing socket error: %s", safe_strerror (errno));
  792. return 0;
  793. }
  794. thread_add_read (zebrad.master, kernel_read, NULL, sock);
  795. if (IS_ZEBRA_DEBUG_KERNEL)
  796. rtmsg_debug (&buf.r.rtm);
  797. rtm = &buf.r.rtm;
  798. /*
  799. * Ensure that we didn't drop any data, so that processing routines
  800. * can assume they have the whole message.
  801. */
  802. if (rtm->rtm_msglen != nbytes)
  803. {
  804. zlog_warn ("kernel_read: rtm->rtm_msglen %d, nbytes %d, type %d\n",
  805. rtm->rtm_msglen, nbytes, rtm->rtm_type);
  806. return -1;
  807. }
  808. switch (rtm->rtm_type)
  809. {
  810. case RTM_ADD:
  811. case RTM_DELETE:
  812. rtm_read (rtm);
  813. break;
  814. case RTM_IFINFO:
  815. ifm_read (&buf.im.ifm);
  816. break;
  817. case RTM_NEWADDR:
  818. case RTM_DELADDR:
  819. ifam_read (&buf.ia.ifa);
  820. break;
  821. #ifdef RTM_IFANNOUNCE
  822. case RTM_IFANNOUNCE:
  823. ifan_read (&buf.ian.ifan);
  824. break;
  825. #endif /* RTM_IFANNOUNCE */
  826. default:
  827. if (IS_ZEBRA_DEBUG_KERNEL)
  828. zlog_debug("Unprocessed RTM_type: %d", rtm->rtm_type);
  829. break;
  830. }
  831. return 0;
  832. }
  833. /* Make routing socket. */
  834. void
  835. routing_socket ()
  836. {
  837. if ( zserv_privs.change (ZPRIVS_RAISE) )
  838. zlog_err ("routing_socket: Can't raise privileges");
  839. routing_sock = socket (AF_ROUTE, SOCK_RAW, 0);
  840. if (routing_sock < 0)
  841. {
  842. if ( zserv_privs.change (ZPRIVS_LOWER) )
  843. zlog_err ("routing_socket: Can't lower privileges");
  844. zlog_warn ("Can't init kernel routing socket");
  845. return;
  846. }
  847. /* XXX: Socket should be NONBLOCK, however as we currently
  848. * discard failed writes, this will lead to inconsistencies.
  849. * For now, socket must be blocking.
  850. */
  851. /*if (fcntl (routing_sock, F_SETFL, O_NONBLOCK) < 0)
  852. zlog_warn ("Can't set O_NONBLOCK to routing socket");*/
  853. if ( zserv_privs.change (ZPRIVS_LOWER) )
  854. zlog_err ("routing_socket: Can't lower privileges");
  855. /* kernel_read needs rewrite. */
  856. thread_add_read (zebrad.master, kernel_read, NULL, routing_sock);
  857. }
  858. /* Exported interface function. This function simply calls
  859. routing_socket (). */
  860. void
  861. kernel_init ()
  862. {
  863. routing_socket ();
  864. }