rt_socket.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. /*
  2. * Kernel routing table updates by routing socket.
  3. * Copyright (C) 1997, 98 Kunihiro Ishiguro
  4. *
  5. * This file is part of GNU Zebra.
  6. *
  7. * GNU Zebra is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License as published by the
  9. * Free Software Foundation; either version 2, or (at your option) any
  10. * later version.
  11. *
  12. * GNU Zebra is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with GNU Zebra; see the file COPYING. If not, write to the Free
  19. * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  20. * 02111-1307, USA.
  21. */
  22. #include <zebra.h>
  23. #include "if.h"
  24. #include "prefix.h"
  25. #include "sockunion.h"
  26. #include "log.h"
  27. #include "str.h"
  28. #include "privs.h"
  29. #include "zebra/debug.h"
  30. #include "zebra/rib.h"
  31. #include "zebra/rt.h"
  32. #include "zebra/kernel_socket.h"
  33. extern struct zebra_privs_t zserv_privs;
  34. /* kernel socket export */
  35. extern int rtm_write (int message, union sockunion *dest,
  36. union sockunion *mask, union sockunion *gate,
  37. unsigned int index, int zebra_flags, int metric);
  38. /* Adjust netmask socket length. Return value is a adjusted sin_len
  39. value. */
  40. static int
  41. sin_masklen (struct in_addr mask)
  42. {
  43. char *p, *lim;
  44. int len;
  45. struct sockaddr_in sin;
  46. if (mask.s_addr == 0)
  47. return sizeof (long);
  48. sin.sin_addr = mask;
  49. len = sizeof (struct sockaddr_in);
  50. lim = (char *) &sin.sin_addr;
  51. p = lim + sizeof (sin.sin_addr);
  52. while (*--p == 0 && p >= lim)
  53. len--;
  54. return len;
  55. }
  56. /* Interface between zebra message and rtm message. */
  57. static int
  58. kernel_rtm_ipv4 (int cmd, struct prefix *p, struct rib *rib, int family)
  59. {
  60. struct sockaddr_in *mask = NULL;
  61. struct sockaddr_in sin_dest, sin_mask, sin_gate;
  62. struct nexthop *nexthop, *tnexthop;
  63. int recursing;
  64. int nexthop_num = 0;
  65. unsigned int ifindex = 0;
  66. int gate = 0;
  67. int error;
  68. char prefix_buf[PREFIX_STRLEN];
  69. if (IS_ZEBRA_DEBUG_RIB)
  70. prefix2str (p, prefix_buf, sizeof(prefix_buf));
  71. memset (&sin_dest, 0, sizeof (struct sockaddr_in));
  72. sin_dest.sin_family = AF_INET;
  73. #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
  74. sin_dest.sin_len = sizeof (struct sockaddr_in);
  75. #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
  76. sin_dest.sin_addr = p->u.prefix4;
  77. memset (&sin_mask, 0, sizeof (struct sockaddr_in));
  78. memset (&sin_gate, 0, sizeof (struct sockaddr_in));
  79. sin_gate.sin_family = AF_INET;
  80. #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
  81. sin_gate.sin_len = sizeof (struct sockaddr_in);
  82. #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
  83. /* Make gateway. */
  84. for (ALL_NEXTHOPS_RO(rib->nexthop, nexthop, tnexthop, recursing))
  85. {
  86. if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_RECURSIVE))
  87. continue;
  88. gate = 0;
  89. char gate_buf[INET_ADDRSTRLEN] = "NULL";
  90. /*
  91. * XXX We need to refrain from kernel operations in some cases,
  92. * but this if statement seems overly cautious - what about
  93. * other than ADD and DELETE?
  94. */
  95. if ((cmd == RTM_ADD
  96. && CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE))
  97. || (cmd == RTM_DELETE
  98. && CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB)
  99. ))
  100. {
  101. if (nexthop->type == NEXTHOP_TYPE_IPV4 ||
  102. nexthop->type == NEXTHOP_TYPE_IPV4_IFINDEX)
  103. {
  104. sin_gate.sin_addr = nexthop->gate.ipv4;
  105. gate = 1;
  106. }
  107. if (nexthop->type == NEXTHOP_TYPE_IFINDEX
  108. || nexthop->type == NEXTHOP_TYPE_IFNAME
  109. || nexthop->type == NEXTHOP_TYPE_IPV4_IFINDEX)
  110. ifindex = nexthop->ifindex;
  111. if (nexthop->type == NEXTHOP_TYPE_BLACKHOLE)
  112. {
  113. struct in_addr loopback;
  114. loopback.s_addr = htonl (INADDR_LOOPBACK);
  115. sin_gate.sin_addr = loopback;
  116. gate = 1;
  117. }
  118. if (gate && p->prefixlen == 32)
  119. mask = NULL;
  120. else
  121. {
  122. masklen2ip (p->prefixlen, &sin_mask.sin_addr);
  123. sin_mask.sin_family = AF_INET;
  124. #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
  125. sin_mask.sin_len = sin_masklen (sin_mask.sin_addr);
  126. #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
  127. mask = &sin_mask;
  128. }
  129. error = rtm_write (cmd,
  130. (union sockunion *)&sin_dest,
  131. (union sockunion *)mask,
  132. gate ? (union sockunion *)&sin_gate : NULL,
  133. ifindex,
  134. rib->flags,
  135. rib->metric);
  136. if (IS_ZEBRA_DEBUG_RIB)
  137. {
  138. if (!gate)
  139. {
  140. zlog_debug ("%s: %s: attention! gate not found for rib %p",
  141. __func__, prefix_buf, rib);
  142. rib_dump (p, rib);
  143. }
  144. else
  145. inet_ntop (AF_INET, &sin_gate.sin_addr, gate_buf, INET_ADDRSTRLEN);
  146. }
  147. switch (error)
  148. {
  149. /* We only flag nexthops as being in FIB if rtm_write() did its work. */
  150. case ZEBRA_ERR_NOERROR:
  151. nexthop_num++;
  152. if (IS_ZEBRA_DEBUG_RIB)
  153. zlog_debug ("%s: %s: successfully did NH %s",
  154. __func__, prefix_buf, gate_buf);
  155. if (cmd == RTM_ADD)
  156. SET_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB);
  157. break;
  158. /* The only valid case for this error is kernel's failure to install
  159. * a multipath route, which is common for FreeBSD. This should be
  160. * ignored silently, but logged as an error otherwise.
  161. */
  162. case ZEBRA_ERR_RTEXIST:
  163. if (cmd != RTM_ADD)
  164. zlog_err ("%s: rtm_write() returned %d for command %d",
  165. __func__, error, cmd);
  166. continue;
  167. break;
  168. /* Given that our NEXTHOP_FLAG_FIB matches real kernel FIB, it isn't
  169. * normal to get any other messages in ANY case.
  170. */
  171. case ZEBRA_ERR_RTNOEXIST:
  172. case ZEBRA_ERR_RTUNREACH:
  173. default:
  174. zlog_err ("%s: %s: rtm_write() unexpectedly returned %d for command %s",
  175. __func__, prefix2str(p, prefix_buf, sizeof(prefix_buf)),
  176. error, lookup (rtm_type_str, cmd));
  177. break;
  178. }
  179. } /* if (cmd and flags make sense) */
  180. else
  181. if (IS_ZEBRA_DEBUG_RIB)
  182. zlog_debug ("%s: odd command %s for flags %d",
  183. __func__, lookup (rtm_type_str, cmd), nexthop->flags);
  184. } /* for (ALL_NEXTHOPS_RO(...))*/
  185. /* If there was no useful nexthop, then complain. */
  186. if (nexthop_num == 0 && IS_ZEBRA_DEBUG_KERNEL)
  187. zlog_debug ("%s: No useful nexthops were found in RIB entry %p", __func__, rib);
  188. return 0; /*XXX*/
  189. }
  190. int
  191. kernel_add_ipv4 (struct prefix *p, struct rib *rib)
  192. {
  193. int route;
  194. if (zserv_privs.change(ZPRIVS_RAISE))
  195. zlog (NULL, LOG_ERR, "Can't raise privileges");
  196. route = kernel_rtm_ipv4 (RTM_ADD, p, rib, AF_INET);
  197. if (zserv_privs.change(ZPRIVS_LOWER))
  198. zlog (NULL, LOG_ERR, "Can't lower privileges");
  199. return route;
  200. }
  201. int
  202. kernel_delete_ipv4 (struct prefix *p, struct rib *rib)
  203. {
  204. int route;
  205. if (zserv_privs.change(ZPRIVS_RAISE))
  206. zlog (NULL, LOG_ERR, "Can't raise privileges");
  207. route = kernel_rtm_ipv4 (RTM_DELETE, p, rib, AF_INET);
  208. if (zserv_privs.change(ZPRIVS_LOWER))
  209. zlog (NULL, LOG_ERR, "Can't lower privileges");
  210. return route;
  211. }
  212. #ifdef HAVE_IPV6
  213. /* Calculate sin6_len value for netmask socket value. */
  214. static int
  215. sin6_masklen (struct in6_addr mask)
  216. {
  217. struct sockaddr_in6 sin6;
  218. char *p, *lim;
  219. int len;
  220. if (IN6_IS_ADDR_UNSPECIFIED (&mask))
  221. return sizeof (long);
  222. sin6.sin6_addr = mask;
  223. len = sizeof (struct sockaddr_in6);
  224. lim = (char *) & sin6.sin6_addr;
  225. p = lim + sizeof (sin6.sin6_addr);
  226. while (*--p == 0 && p >= lim)
  227. len--;
  228. return len;
  229. }
  230. /* Interface between zebra message and rtm message. */
  231. static int
  232. kernel_rtm_ipv6_multipath (int cmd, struct prefix *p, struct rib *rib,
  233. int family)
  234. {
  235. struct sockaddr_in6 *mask;
  236. struct sockaddr_in6 sin_dest, sin_mask, sin_gate;
  237. struct nexthop *nexthop, *tnexthop;
  238. int recursing;
  239. int nexthop_num = 0;
  240. unsigned int ifindex = 0;
  241. int gate = 0;
  242. int error;
  243. memset (&sin_dest, 0, sizeof (struct sockaddr_in6));
  244. sin_dest.sin6_family = AF_INET6;
  245. #ifdef SIN6_LEN
  246. sin_dest.sin6_len = sizeof (struct sockaddr_in6);
  247. #endif /* SIN6_LEN */
  248. sin_dest.sin6_addr = p->u.prefix6;
  249. memset (&sin_mask, 0, sizeof (struct sockaddr_in6));
  250. memset (&sin_gate, 0, sizeof (struct sockaddr_in6));
  251. sin_gate.sin6_family = AF_INET6;
  252. #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
  253. sin_gate.sin6_len = sizeof (struct sockaddr_in6);
  254. #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
  255. /* Make gateway. */
  256. for (ALL_NEXTHOPS_RO(rib->nexthop, nexthop, tnexthop, recursing))
  257. {
  258. if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_RECURSIVE))
  259. continue;
  260. gate = 0;
  261. if ((cmd == RTM_ADD
  262. && CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE))
  263. || (cmd == RTM_DELETE
  264. #if 0
  265. && CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB)
  266. #endif
  267. ))
  268. {
  269. if (nexthop->type == NEXTHOP_TYPE_IPV6
  270. || nexthop->type == NEXTHOP_TYPE_IPV6_IFNAME
  271. || nexthop->type == NEXTHOP_TYPE_IPV6_IFINDEX)
  272. {
  273. sin_gate.sin6_addr = nexthop->gate.ipv6;
  274. gate = 1;
  275. }
  276. if (nexthop->type == NEXTHOP_TYPE_IFINDEX
  277. || nexthop->type == NEXTHOP_TYPE_IFNAME
  278. || nexthop->type == NEXTHOP_TYPE_IPV6_IFNAME
  279. || nexthop->type == NEXTHOP_TYPE_IPV6_IFINDEX)
  280. ifindex = nexthop->ifindex;
  281. if (cmd == RTM_ADD)
  282. SET_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB);
  283. }
  284. /* Under kame set interface index to link local address. */
  285. #ifdef KAME
  286. #define SET_IN6_LINKLOCAL_IFINDEX(a, i) \
  287. do { \
  288. (a).s6_addr[2] = ((i) >> 8) & 0xff; \
  289. (a).s6_addr[3] = (i) & 0xff; \
  290. } while (0)
  291. if (gate && IN6_IS_ADDR_LINKLOCAL(&sin_gate.sin6_addr))
  292. SET_IN6_LINKLOCAL_IFINDEX (sin_gate.sin6_addr, ifindex);
  293. #endif /* KAME */
  294. if (gate && p->prefixlen == 128)
  295. mask = NULL;
  296. else
  297. {
  298. masklen2ip6 (p->prefixlen, &sin_mask.sin6_addr);
  299. sin_mask.sin6_family = AF_INET6;
  300. #ifdef SIN6_LEN
  301. sin_mask.sin6_len = sin6_masklen (sin_mask.sin6_addr);
  302. #endif /* SIN6_LEN */
  303. mask = &sin_mask;
  304. }
  305. error = rtm_write (cmd,
  306. (union sockunion *) &sin_dest,
  307. (union sockunion *) mask,
  308. gate ? (union sockunion *)&sin_gate : NULL,
  309. ifindex,
  310. rib->flags,
  311. rib->metric);
  312. #if 0
  313. if (error)
  314. {
  315. zlog_info ("kernel_rtm_ipv6_multipath(): nexthop %d add error=%d.",
  316. nexthop_num, error);
  317. }
  318. #endif
  319. nexthop_num++;
  320. }
  321. /* If there is no useful nexthop then return. */
  322. if (nexthop_num == 0)
  323. {
  324. if (IS_ZEBRA_DEBUG_KERNEL)
  325. zlog_debug ("kernel_rtm_ipv6_multipath(): No useful nexthop.");
  326. return 0;
  327. }
  328. return 0; /*XXX*/
  329. }
  330. int
  331. kernel_add_ipv6 (struct prefix *p, struct rib *rib)
  332. {
  333. int route;
  334. if (zserv_privs.change(ZPRIVS_RAISE))
  335. zlog (NULL, LOG_ERR, "Can't raise privileges");
  336. route = kernel_rtm_ipv6_multipath (RTM_ADD, p, rib, AF_INET6);
  337. if (zserv_privs.change(ZPRIVS_LOWER))
  338. zlog (NULL, LOG_ERR, "Can't lower privileges");
  339. return route;
  340. }
  341. int
  342. kernel_delete_ipv6 (struct prefix *p, struct rib *rib)
  343. {
  344. int route;
  345. if (zserv_privs.change(ZPRIVS_RAISE))
  346. zlog (NULL, LOG_ERR, "Can't raise privileges");
  347. route = kernel_rtm_ipv6_multipath (RTM_DELETE, p, rib, AF_INET6);
  348. if (zserv_privs.change(ZPRIVS_LOWER))
  349. zlog (NULL, LOG_ERR, "Can't lower privileges");
  350. return route;
  351. }
  352. #endif /* HAVE_IPV6 */