bgp_network.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  1. /* BGP network related fucntions
  2. Copyright (C) 1999 Kunihiro Ishiguro
  3. This file is part of GNU Zebra.
  4. GNU Zebra is free software; you can redistribute it and/or modify it
  5. under the terms of the GNU General Public License as published by the
  6. Free Software Foundation; either version 2, or (at your option) any
  7. later version.
  8. GNU Zebra is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Zebra; see the file COPYING. If not, write to the Free
  14. Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  15. 02111-1307, USA. */
  16. #include <zebra.h>
  17. #include "thread.h"
  18. #include "sockunion.h"
  19. #include "sockopt.h"
  20. #include "memory.h"
  21. #include "log.h"
  22. #include "if.h"
  23. #include "prefix.h"
  24. #include "command.h"
  25. #include "privs.h"
  26. #include "linklist.h"
  27. #include "bgpd/bgpd.h"
  28. #include "bgpd/bgp_fsm.h"
  29. #include "bgpd/bgp_attr.h"
  30. #include "bgpd/bgp_debug.h"
  31. #include "bgpd/bgp_network.h"
  32. extern struct zebra_privs_t bgpd_privs;
  33. /*
  34. * Set MD5 key for the socket, for the given IPv4 peer address.
  35. * If the password is NULL or zero-length, the option will be disabled.
  36. */
  37. static int
  38. bgp_md5_set_socket (int socket, union sockunion *su, const char *password)
  39. {
  40. int ret = -1;
  41. int en = ENOSYS;
  42. assert (socket >= 0);
  43. #if HAVE_DECL_TCP_MD5SIG
  44. ret = sockopt_tcp_signature (socket, su, password);
  45. en = errno;
  46. #endif /* HAVE_TCP_MD5SIG */
  47. if (ret < 0)
  48. zlog (NULL, LOG_WARNING, "can't set TCP_MD5SIG option on socket %d: %s",
  49. socket, safe_strerror (en));
  50. return ret;
  51. }
  52. /* Helper for bgp_connect */
  53. static int
  54. bgp_md5_set_connect (int socket, union sockunion *su, const char *password)
  55. {
  56. int ret = -1;
  57. #if HAVE_DECL_TCP_MD5SIG
  58. if ( bgpd_privs.change (ZPRIVS_RAISE) )
  59. {
  60. zlog_err ("%s: could not raise privs", __func__);
  61. return ret;
  62. }
  63. ret = bgp_md5_set_socket (socket, su, password);
  64. if (bgpd_privs.change (ZPRIVS_LOWER) )
  65. zlog_err ("%s: could not lower privs", __func__);
  66. #endif /* HAVE_TCP_MD5SIG */
  67. return ret;
  68. }
  69. int
  70. bgp_md5_set (struct peer *peer)
  71. {
  72. struct listnode *node;
  73. int fret = 0, ret;
  74. int *socket;
  75. if ( bgpd_privs.change (ZPRIVS_RAISE) )
  76. {
  77. zlog_err ("%s: could not raise privs", __func__);
  78. return -1;
  79. }
  80. /* Just set the password on the listen socket(s). Outbound connections
  81. * are taken care of in bgp_connect() below.
  82. */
  83. for (ALL_LIST_ELEMENTS_RO(bm->listen_sockets, node, socket))
  84. {
  85. ret = bgp_md5_set_socket ((int )socket, &peer->su, peer->password);
  86. if (ret < 0)
  87. fret = ret;
  88. }
  89. if (bgpd_privs.change (ZPRIVS_LOWER) )
  90. zlog_err ("%s: could not lower privs", __func__);
  91. return fret;
  92. }
  93. /* Accept bgp connection. */
  94. static int
  95. bgp_accept (struct thread *thread)
  96. {
  97. int bgp_sock;
  98. int accept_sock;
  99. union sockunion su;
  100. struct peer *peer;
  101. struct peer *peer1;
  102. struct bgp *bgp;
  103. char buf[SU_ADDRSTRLEN];
  104. /* Regiser accept thread. */
  105. accept_sock = THREAD_FD (thread);
  106. bgp = THREAD_ARG (thread);
  107. if (accept_sock < 0)
  108. {
  109. zlog_err ("accept_sock is nevative value %d", accept_sock);
  110. return -1;
  111. }
  112. thread_add_read (master, bgp_accept, bgp, accept_sock);
  113. /* Accept client connection. */
  114. bgp_sock = sockunion_accept (accept_sock, &su);
  115. if (bgp_sock < 0)
  116. {
  117. zlog_err ("[Error] BGP socket accept failed (%s)", safe_strerror (errno));
  118. return -1;
  119. }
  120. if (BGP_DEBUG (events, EVENTS))
  121. zlog_debug ("[Event] BGP connection from host %s", inet_sutop (&su, buf));
  122. /* Check remote IP address */
  123. peer1 = peer_lookup (bgp, &su);
  124. if (! peer1 || peer1->status == Idle)
  125. {
  126. if (BGP_DEBUG (events, EVENTS))
  127. {
  128. if (! peer1)
  129. zlog_debug ("[Event] BGP connection IP address %s is not configured",
  130. inet_sutop (&su, buf));
  131. else
  132. zlog_debug ("[Event] BGP connection IP address %s is Idle state",
  133. inet_sutop (&su, buf));
  134. }
  135. close (bgp_sock);
  136. return -1;
  137. }
  138. /* In case of peer is EBGP, we should set TTL for this connection. */
  139. if (peer_sort (peer1) == BGP_PEER_EBGP)
  140. sockopt_ttl (peer1->su.sa.sa_family, bgp_sock, peer1->ttl);
  141. if (! bgp)
  142. bgp = peer1->bgp;
  143. /* Make dummy peer until read Open packet. */
  144. if (BGP_DEBUG (events, EVENTS))
  145. zlog_debug ("[Event] Make dummy peer structure until read Open packet");
  146. {
  147. char buf[SU_ADDRSTRLEN + 1];
  148. peer = peer_create_accept (bgp);
  149. SET_FLAG (peer->sflags, PEER_STATUS_ACCEPT_PEER);
  150. peer->su = su;
  151. peer->fd = bgp_sock;
  152. peer->status = Active;
  153. peer->local_id = peer1->local_id;
  154. /* Make peer's address string. */
  155. sockunion2str (&su, buf, SU_ADDRSTRLEN);
  156. peer->host = XSTRDUP (MTYPE_BGP_PEER_HOST, buf);
  157. }
  158. BGP_EVENT_ADD (peer, TCP_connection_open);
  159. return 0;
  160. }
  161. /* BGP socket bind. */
  162. static int
  163. bgp_bind (struct peer *peer)
  164. {
  165. #ifdef SO_BINDTODEVICE
  166. int ret;
  167. struct ifreq ifreq;
  168. if (! peer->ifname)
  169. return 0;
  170. strncpy ((char *)&ifreq.ifr_name, peer->ifname, sizeof (ifreq.ifr_name));
  171. if ( bgpd_privs.change (ZPRIVS_RAISE) )
  172. zlog_err ("bgp_bind: could not raise privs");
  173. ret = setsockopt (peer->fd, SOL_SOCKET, SO_BINDTODEVICE,
  174. &ifreq, sizeof (ifreq));
  175. if (bgpd_privs.change (ZPRIVS_LOWER) )
  176. zlog_err ("bgp_bind: could not lower privs");
  177. if (ret < 0)
  178. {
  179. zlog (peer->log, LOG_INFO, "bind to interface %s failed", peer->ifname);
  180. return ret;
  181. }
  182. #endif /* SO_BINDTODEVICE */
  183. return 0;
  184. }
  185. static int
  186. bgp_bind_address (int sock, struct in_addr *addr)
  187. {
  188. int ret;
  189. struct sockaddr_in local;
  190. memset (&local, 0, sizeof (struct sockaddr_in));
  191. local.sin_family = AF_INET;
  192. #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
  193. local.sin_len = sizeof(struct sockaddr_in);
  194. #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
  195. memcpy (&local.sin_addr, addr, sizeof (struct in_addr));
  196. if ( bgpd_privs.change (ZPRIVS_RAISE) )
  197. zlog_err ("bgp_bind_address: could not raise privs");
  198. ret = bind (sock, (struct sockaddr *)&local, sizeof (struct sockaddr_in));
  199. if (ret < 0)
  200. ;
  201. if (bgpd_privs.change (ZPRIVS_LOWER) )
  202. zlog_err ("bgp_bind_address: could not lower privs");
  203. return 0;
  204. }
  205. static struct in_addr *
  206. bgp_update_address (struct interface *ifp)
  207. {
  208. struct prefix_ipv4 *p;
  209. struct connected *connected;
  210. struct listnode *node;
  211. for (ALL_LIST_ELEMENTS_RO (ifp->connected, node, connected))
  212. {
  213. p = (struct prefix_ipv4 *) connected->address;
  214. if (p->family == AF_INET)
  215. return &p->prefix;
  216. }
  217. return NULL;
  218. }
  219. /* Update source selection. */
  220. static void
  221. bgp_update_source (struct peer *peer)
  222. {
  223. struct interface *ifp;
  224. struct in_addr *addr;
  225. /* Source is specified with interface name. */
  226. if (peer->update_if)
  227. {
  228. ifp = if_lookup_by_name (peer->update_if);
  229. if (! ifp)
  230. return;
  231. addr = bgp_update_address (ifp);
  232. if (! addr)
  233. return;
  234. bgp_bind_address (peer->fd, addr);
  235. }
  236. /* Source is specified with IP address. */
  237. if (peer->update_source)
  238. sockunion_bind (peer->fd, peer->update_source, 0, peer->update_source);
  239. }
  240. /* BGP try to connect to the peer. */
  241. int
  242. bgp_connect (struct peer *peer)
  243. {
  244. unsigned int ifindex = 0;
  245. /* Make socket for the peer. */
  246. peer->fd = sockunion_socket (&peer->su);
  247. if (peer->fd < 0)
  248. return -1;
  249. /* If we can get socket for the peer, adjest TTL and make connection. */
  250. if (peer_sort (peer) == BGP_PEER_EBGP)
  251. sockopt_ttl (peer->su.sa.sa_family, peer->fd, peer->ttl);
  252. sockopt_reuseaddr (peer->fd);
  253. sockopt_reuseport (peer->fd);
  254. #ifdef IPTOS_PREC_INTERNETCONTROL
  255. if (sockunion_family (&peer->su) == AF_INET)
  256. setsockopt_ipv4_tos (peer->fd, IPTOS_PREC_INTERNETCONTROL);
  257. #endif
  258. if (peer->password)
  259. bgp_md5_set_connect (peer->fd, &peer->su, peer->password);
  260. /* Bind socket. */
  261. bgp_bind (peer);
  262. /* Update source bind. */
  263. bgp_update_source (peer);
  264. #ifdef HAVE_IPV6
  265. if (peer->ifname)
  266. ifindex = if_nametoindex (peer->ifname);
  267. #endif /* HAVE_IPV6 */
  268. if (BGP_DEBUG (events, EVENTS))
  269. plog_debug (peer->log, "%s [Event] Connect start to %s fd %d",
  270. peer->host, peer->host, peer->fd);
  271. /* Connect to the remote peer. */
  272. return sockunion_connect (peer->fd, &peer->su, htons (peer->port), ifindex);
  273. }
  274. /* After TCP connection is established. Get local address and port. */
  275. void
  276. bgp_getsockname (struct peer *peer)
  277. {
  278. if (peer->su_local)
  279. {
  280. sockunion_free (peer->su_local);
  281. peer->su_local = NULL;
  282. }
  283. if (peer->su_remote)
  284. {
  285. sockunion_free (peer->su_remote);
  286. peer->su_remote = NULL;
  287. }
  288. peer->su_local = sockunion_getsockname (peer->fd);
  289. peer->su_remote = sockunion_getpeername (peer->fd);
  290. bgp_nexthop_set (peer->su_local, peer->su_remote, &peer->nexthop, peer);
  291. }
  292. /* IPv6 supported version of BGP server socket setup. */
  293. #if defined (HAVE_IPV6) && ! defined (NRL)
  294. int
  295. bgp_socket (struct bgp *bgp, unsigned short port, char *address)
  296. {
  297. int ret, en;
  298. struct addrinfo req;
  299. struct addrinfo *ainfo;
  300. struct addrinfo *ainfo_save;
  301. int sock = 0;
  302. char port_str[BUFSIZ];
  303. memset (&req, 0, sizeof (struct addrinfo));
  304. req.ai_flags = AI_PASSIVE;
  305. req.ai_family = AF_UNSPEC;
  306. req.ai_socktype = SOCK_STREAM;
  307. snprintf (port_str, sizeof(port_str), "%d", port);
  308. port_str[sizeof (port_str) - 1] = '\0';
  309. ret = getaddrinfo (address, port_str, &req, &ainfo);
  310. if (ret != 0)
  311. {
  312. zlog_err ("getaddrinfo: %s", gai_strerror (ret));
  313. return -1;
  314. }
  315. ainfo_save = ainfo;
  316. do
  317. {
  318. if (ainfo->ai_family != AF_INET && ainfo->ai_family != AF_INET6)
  319. continue;
  320. sock = socket (ainfo->ai_family, ainfo->ai_socktype, ainfo->ai_protocol);
  321. if (sock < 0)
  322. {
  323. zlog_err ("socket: %s", safe_strerror (errno));
  324. continue;
  325. }
  326. sockopt_reuseaddr (sock);
  327. sockopt_reuseport (sock);
  328. #ifdef IPTOS_PREC_INTERNETCONTROL
  329. if (ainfo->ai_family == AF_INET)
  330. setsockopt_ipv4_tos (sock, IPTOS_PREC_INTERNETCONTROL);
  331. #endif
  332. if (bgpd_privs.change (ZPRIVS_RAISE) )
  333. zlog_err ("bgp_socket: could not raise privs");
  334. ret = bind (sock, ainfo->ai_addr, ainfo->ai_addrlen);
  335. en = errno;
  336. if (bgpd_privs.change (ZPRIVS_LOWER) )
  337. zlog_err ("bgp_bind_address: could not lower privs");
  338. if (ret < 0)
  339. {
  340. zlog_err ("bind: %s", safe_strerror (en));
  341. close(sock);
  342. continue;
  343. }
  344. ret = listen (sock, 3);
  345. if (ret < 0)
  346. {
  347. zlog_err ("listen: %s", safe_strerror (errno));
  348. close (sock);
  349. continue;
  350. }
  351. listnode_add (bm->listen_sockets, (void *)sock);
  352. thread_add_read (master, bgp_accept, bgp, sock);
  353. }
  354. while ((ainfo = ainfo->ai_next) != NULL);
  355. freeaddrinfo (ainfo_save);
  356. return sock;
  357. }
  358. #else
  359. /* Traditional IPv4 only version. */
  360. int
  361. bgp_socket (struct bgp *bgp, unsigned short port, char *address)
  362. {
  363. int sock;
  364. int socklen;
  365. struct sockaddr_in sin;
  366. int ret, en;
  367. sock = socket (AF_INET, SOCK_STREAM, 0);
  368. if (sock < 0)
  369. {
  370. zlog_err ("socket: %s", safe_strerror (errno));
  371. return sock;
  372. }
  373. sockopt_reuseaddr (sock);
  374. sockopt_reuseport (sock);
  375. #ifdef IPTOS_PREC_INTERNETCONTROL
  376. setsockopt_ipv4_tos (sock, IPTOS_PREC_INTERNETCONTROL);
  377. #endif
  378. memset (&sin, 0, sizeof (struct sockaddr_in));
  379. sin.sin_family = AF_INET;
  380. sin.sin_port = htons (port);
  381. socklen = sizeof (struct sockaddr_in);
  382. if (address && ((ret = inet_aton(address, &sin.sin_addr)) < 1))
  383. {
  384. zlog_err("bgp_socket: could not parse ip address %s: %s",
  385. address, safe_strerror (errno));
  386. return ret;
  387. }
  388. #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
  389. sin.sin_len = socklen;
  390. #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
  391. if ( bgpd_privs.change (ZPRIVS_RAISE) )
  392. zlog_err ("bgp_socket: could not raise privs");
  393. ret = bind (sock, (struct sockaddr *) &sin, socklen);
  394. en = errno;
  395. if (bgpd_privs.change (ZPRIVS_LOWER) )
  396. zlog_err ("bgp_socket: could not lower privs");
  397. if (ret < 0)
  398. {
  399. zlog_err ("bind: %s", safe_strerror (en));
  400. close (sock);
  401. return ret;
  402. }
  403. ret = listen (sock, 3);
  404. if (ret < 0)
  405. {
  406. zlog_err ("listen: %s", safe_strerror (errno));
  407. close (sock);
  408. return ret;
  409. }
  410. thread_add_read (bm->master, bgp_accept, bgp, sock);
  411. return sock;
  412. }
  413. #endif /* HAVE_IPV6 && !NRL */