diff -uar bgpd/bgp_network.c bgpd/bgp_network.c --- bgpd/bgp_network.c Sat Oct 29 22:44:50 2005 +++ bgpd/bgp_network.c Tue Feb 13 13:15:38 2007 @@ -38,6 +38,56 @@ extern struct zebra_privs_t bgpd_privs; +#if defined(HAVE_TCP_MD5) && defined(GNU_LINUX) +/* Set MD5 key to the socket. */ +int +bgp_md5_set (int sock, struct peer *peer, char *password) +{ + int ret; + struct tcp_rfc2385_cmd cmd; + struct in_addr *addr = &peer->su.sin.sin_addr; + + cmd.command = TCP_MD5_AUTH_ADD; + cmd.address = addr->s_addr; + cmd.keylen = strlen (password); + cmd.key = password; + + if ( bgpd_privs.change (ZPRIVS_RAISE) ) + zlog_err ("bgp_md5_set: could not raise privs"); + + ret = setsockopt (sock, IPPROTO_TCP, TCP_MD5_AUTH, &cmd, sizeof cmd); + + if (bgpd_privs.change (ZPRIVS_LOWER) ) + zlog_err ("bgp_md5_set: could not lower privs"); + + return ret; +} + +/* Unset MD5 key from the socket. */ +int +bgp_md5_unset (int sock, struct peer *peer, char *password) +{ + int ret; + struct tcp_rfc2385_cmd cmd; + struct in_addr *addr = &peer->su.sin.sin_addr; + + cmd.command = TCP_MD5_AUTH_DEL; + cmd.address = addr->s_addr; + cmd.keylen = strlen (password); + cmd.key = password; + + if ( bgpd_privs.change (ZPRIVS_RAISE) ) + zlog_err ("bgp_md5_unset: could not raise privs"); + + ret = setsockopt (sock, IPPROTO_TCP, TCP_MD5_AUTH, &cmd, sizeof cmd); + + if (bgpd_privs.change (ZPRIVS_LOWER) ) + zlog_err ("bgp_md5_unset: could not lower privs"); + + return ret; +} +#endif /* defined(HAVE_TCP_MD5) && defined(GNU_LINUX) */ + /* Accept bgp connection. */ static int bgp_accept (struct thread *thread) @@ -238,6 +288,12 @@ sockopt_reuseaddr (peer->fd); sockopt_reuseport (peer->fd); +#ifdef HAVE_TCP_MD5 + if (CHECK_FLAG (peer->flags, PEER_FLAG_PASSWORD)) + if (sockunion_family (&peer->su) == AF_INET) + bgp_md5_set (peer->fd, peer, peer->password); +#endif /* HAVE_TCP_MD5 */ + /* Bind socket. */ bgp_bind (peer); @@ -346,6 +402,10 @@ continue; } +#ifdef HAVE_TCP_MD5 + bm->sock = sock; +#endif /* HAVE_TCP_MD5 */ + thread_add_read (master, bgp_accept, bgp, sock); } while ((ainfo = ainfo->ai_next) != NULL); @@ -406,6 +466,9 @@ close (sock); return ret; } +#ifdef HAVE_TCP_MD5 + bm->sock = sock; +#endif /* HAVE_TCP_MD5 */ thread_add_read (bm->master, bgp_accept, bgp, sock); diff -uar bgpd/bgp_network.h bgpd/bgp_network.h --- bgpd/bgp_network.h Tue Jun 28 15:38:12 2005 +++ bgpd/bgp_network.h Tue Feb 13 13:16:23 2007 @@ -21,6 +21,28 @@ #ifndef _QUAGGA_BGP_NETWORK_H #define _QUAGGA_BGP_NETWORK_H +#if defined(HAVE_TCP_MD5) && defined(GNU_LINUX) +/* setsockopt Number */ +#define TCP_MD5_AUTH 13 + +/* Commands (used in the structure passed from userland) */ +#define TCP_MD5_AUTH_ADD 1 +#define TCP_MD5_AUTH_DEL 2 + +struct tcp_rfc2385_cmd { + u_int8_t command; /* Command - Add/Delete */ + u_int32_t address; /* IPV4 address associated */ + u_int8_t keylen; /* MD5 Key len (do NOT assume 0 terminated ascii) */ + void *key; /* MD5 Key */ +}; +#endif /* defined(HAVE_TCP_MD5) && defined(GNU_LINUX) */ + +#ifdef HAVE_TCP_MD5 +int bgp_md5_set (int sock, struct peer *, char *); +int bgp_md5_unset (int sock, struct peer *, char *); +#endif /* HAVE_TCP_MD5 */ + + extern int bgp_socket (struct bgp *, unsigned short); extern int bgp_connect (struct peer *); extern void bgp_getsockname (struct peer *); diff -uar bgpd/bgp_vty.c bgpd/bgp_vty.c --- bgpd/bgp_vty.c Thu Sep 14 02:57:05 2006 +++ bgpd/bgp_vty.c Tue Feb 13 13:15:38 2007 @@ -1479,6 +1479,46 @@ "AS number used as local AS\n" "Do not prepend local-as to updates from ebgp peers\n") +#ifdef HAVE_TCP_MD5 +DEFUN (neighbor_password, + neighbor_password_cmd, + NEIGHBOR_CMD2 "password LINE", + NEIGHBOR_STR + NEIGHBOR_ADDR_STR2 + "Set a password\n" + "The password\n") +{ + struct peer *peer; + int ret; + + peer = peer_and_group_lookup_vty (vty, argv[0]); + if (! peer) + return CMD_WARNING; + + ret = peer_password_set (peer, argv[1]); + return bgp_vty_return (vty, ret); +} + +DEFUN (no_neighbor_password, + no_neighbor_password_cmd, + NO_NEIGHBOR_CMD2 "password", + NO_STR + NEIGHBOR_STR + NEIGHBOR_ADDR_STR2 + "Set a password\n") +{ + struct peer *peer; + int ret; + + peer = peer_and_group_lookup_vty (vty, argv[0]); + if (! peer) + return CMD_WARNING; + + ret = peer_password_unset (peer); + return bgp_vty_return (vty, ret); +} +#endif /* HAVE_TCP_MD5 */ + DEFUN (neighbor_activate, neighbor_activate_cmd, NEIGHBOR_CMD2 "activate", @@ -8883,6 +8923,12 @@ install_element (BGP_NODE, &no_neighbor_local_as_cmd); install_element (BGP_NODE, &no_neighbor_local_as_val_cmd); install_element (BGP_NODE, &no_neighbor_local_as_val2_cmd); + +#ifdef HAVE_TCP_MD5 + /* "neighbor password" commands. */ + install_element (BGP_NODE, &neighbor_password_cmd); + install_element (BGP_NODE, &no_neighbor_password_cmd); +#endif /* HAVE_TCP_MD5 */ /* "neighbor activate" commands. */ install_element (BGP_NODE, &neighbor_activate_cmd); diff -uar bgpd/bgpd.c bgpd/bgpd.c --- bgpd/bgpd.c Fri Dec 8 00:24:44 2006 +++ bgpd/bgpd.c Tue Feb 13 13:55:11 2007 @@ -788,6 +788,7 @@ peer->status = Idle; peer->ostatus = Idle; peer->weight = 0; + peer->password = NULL; peer->bgp = bgp; peer = peer_lock (peer); /* initial reference */ @@ -1202,6 +1203,18 @@ peer->last_reset = PEER_DOWN_NEIGHBOR_DELETE; bgp_stop (peer); bgp_fsm_change_status (peer, Deleted); + +#ifdef HAVE_TCP_MD5 + /* Password configuration */ + if (peer->password) + { + if (! CHECK_FLAG (peer->sflags, PEER_STATUS_GROUP) + && sockunion_family (&peer->su) == AF_INET) + bgp_md5_unset (bm->sock, peer, peer->password); + free (peer->password); + } +#endif /* HAVE_TCP_MD5 */ + bgp_timer_set (peer); /* stops all timers for Deleted */ /* Delete from all peer list. */ @@ -1417,6 +1430,26 @@ else peer->v_routeadv = BGP_DEFAULT_EBGP_ROUTEADV; +#ifdef HAVE_TCP_MD5 + /* password apply */ + if (CHECK_FLAG (conf->flags, PEER_FLAG_PASSWORD)) + { + if (peer->password) + free (peer->password); + peer->password = strdup (conf->password); + + if (sockunion_family (&peer->su) == AF_INET) + bgp_md5_set (bm->sock, peer, peer->password); + } + else if (peer->password) + { + if (sockunion_family (&peer->su) == AF_INET) + bgp_md5_unset (bm->sock, peer, peer->password); + free (peer->password); + peer->password = NULL; + } +#endif /* HAVE_TCP_MD5 */ + /* maximum-prefix */ peer->pmax[afi][safi] = conf->pmax[afi][safi]; peer->pmax_threshold[afi][safi] = conf->pmax_threshold[afi][safi]; @@ -3379,6 +3412,119 @@ return 0; } +#ifdef HAVE_TCP_MD5 +/* Set password for authenticating with the peer. */ +int +peer_password_set (struct peer *peer, const char *password) +{ + struct peer_group *group; + struct listnode *nn, *nn2; + + if (peer->password && strcmp (peer->password, password) == 0 + && ! CHECK_FLAG (peer->sflags, PEER_STATUS_GROUP)) + return 0; + + SET_FLAG (peer->flags, PEER_FLAG_PASSWORD); + if (peer->password) + free (peer->password); + peer->password = strdup (password); + + if (! CHECK_FLAG (peer->sflags, PEER_STATUS_GROUP)) + { + if (peer->status == Established) + bgp_notify_send (peer, BGP_NOTIFY_CEASE, BGP_NOTIFY_CEASE_CONFIG_CHANGE); + else + BGP_EVENT_ADD (peer, BGP_Stop); + + if (sockunion_family (&peer->su) == AF_INET) + bgp_md5_set (bm->sock, peer, peer->password); + return 0; + } + + group = peer->group; + for (ALL_LIST_ELEMENTS(group->peer, nn, nn2, peer)) + { + if (peer->password && strcmp (peer->password, password) == 0) + continue; + + SET_FLAG (peer->flags, PEER_FLAG_PASSWORD); + if (peer->password) + free (peer->password); + peer->password = strdup (password); + + if (peer->status == Established) + bgp_notify_send (peer, BGP_NOTIFY_CEASE, BGP_NOTIFY_CEASE_CONFIG_CHANGE); + else + BGP_EVENT_ADD (peer, BGP_Stop); + + if (sockunion_family (&peer->su) == AF_INET) + bgp_md5_set (bm->sock, peer, peer->password); + } + + return 0; +} + +int +peer_password_unset (struct peer *peer) +{ + struct peer_group *group; + struct listnode *nn, *nn2; + + if (! CHECK_FLAG (peer->flags, PEER_FLAG_PASSWORD) + && ! CHECK_FLAG (peer->sflags, PEER_STATUS_GROUP)) + return 0; + + if (! CHECK_FLAG (peer->sflags, PEER_STATUS_GROUP)) + { + if (peer_group_active (peer) + && CHECK_FLAG (peer->group->conf->flags, PEER_FLAG_PASSWORD)) + return BGP_ERR_PEER_GROUP_HAS_THE_FLAG; + + if (peer->status == Established) + bgp_notify_send (peer, BGP_NOTIFY_CEASE, BGP_NOTIFY_CEASE_CONFIG_CHANGE); + else + BGP_EVENT_ADD (peer, BGP_Stop); + + if (sockunion_family (&peer->su) == AF_INET) + bgp_md5_unset (bm->sock, peer, peer->password); + + UNSET_FLAG (peer->flags, PEER_FLAG_PASSWORD); + if (peer->password) + free (peer->password); + peer->password = NULL; + + return 0; + } + + UNSET_FLAG (peer->flags, PEER_FLAG_PASSWORD); + if (peer->password) + free (peer->password); + peer->password = NULL; + + group = peer->group; + for (ALL_LIST_ELEMENTS(group->peer, nn, nn2, peer)) + { + if (! CHECK_FLAG (peer->flags, PEER_FLAG_PASSWORD)) + continue; + + if (peer->status == Established) + bgp_notify_send (peer, BGP_NOTIFY_CEASE, BGP_NOTIFY_CEASE_CONFIG_CHANGE); + else + BGP_EVENT_ADD (peer, BGP_Stop); + + if (sockunion_family (&peer->su) == AF_INET) + bgp_md5_unset (bm->sock, peer, peer->password); + + UNSET_FLAG (peer->flags, PEER_FLAG_PASSWORD); + if (peer->password) + free (peer->password); + peer->password = NULL; + } + + return 0; +} +#endif /* HAVE_TCP_MD5 */ + /* Set distribute list to the peer. */ int peer_distribute_set (struct peer *peer, afi_t afi, safi_t safi, int direct, @@ -4416,6 +4562,16 @@ ! CHECK_FLAG (g_peer->flags, PEER_FLAG_SHUTDOWN)) vty_out (vty, " neighbor %s shutdown%s", addr, VTY_NEWLINE); +#ifdef HAVE_TCP_MD5 + /* Password. */ + if (CHECK_FLAG (peer->flags, PEER_FLAG_PASSWORD)) + if (! peer_group_active (peer) + || ! CHECK_FLAG (g_peer->flags, PEER_FLAG_PASSWORD) + || strcmp (peer->password, g_peer->password) != 0) + vty_out (vty, " neighbor %s password %s%s", addr, peer->password, + VTY_NEWLINE); +#endif /* HAVE_TCP_MD5 */ + /* BGP port. */ if (peer->port != BGP_PORT_DEFAULT) vty_out (vty, " neighbor %s port %d%s", addr, peer->port, @@ -4943,6 +5099,9 @@ bm->port = BGP_PORT_DEFAULT; bm->master = thread_master_create (); bm->start_time = time (NULL); +#ifdef HAVE_TCP_MD5 + bm->sock = -1; +#endif /* HAVE_TCP_MD5 */ } diff -uar bgpd/bgpd.h bgpd/bgpd.h --- bgpd/bgpd.h Thu Sep 14 02:57:05 2006 +++ bgpd/bgpd.h Tue Feb 13 13:24:36 2007 @@ -52,6 +52,11 @@ #define BGP_OPT_NO_FIB (1 << 0) #define BGP_OPT_MULTIPLE_INSTANCE (1 << 1) #define BGP_OPT_CONFIG_CISCO (1 << 2) + +#ifdef HAVE_TCP_MD5 + /* bgp receive socket */ + int sock; +#endif /* HAVE_TCP_MD5 */ }; /* BGP instance structure. */ @@ -346,6 +351,8 @@ #define PEER_FLAG_DYNAMIC_CAPABILITY (1 << 5) /* dynamic capability */ #define PEER_FLAG_DISABLE_CONNECTED_CHECK (1 << 6) /* disable-connected-check */ #define PEER_FLAG_LOCAL_AS_NO_PREPEND (1 << 7) /* local-as no-prepend */ +#define PEER_FLAG_PASSWORD (1 << 8) /* password */ + /* NSF mode (graceful restart) */ u_char nsf[AFI_MAX][SAFI_MAX]; @@ -370,6 +377,9 @@ #define PEER_FLAG_MAX_PREFIX_WARNING (1 << 15) /* maximum prefix warning-only */ #define PEER_FLAG_NEXTHOP_LOCAL_UNCHANGED (1 << 16) /* leave link-local nexthop unchanged */ + /* MD5 password */ + char *password; + /* default-originate route-map. */ struct { @@ -916,6 +926,11 @@ extern int peer_maximum_prefix_set (struct peer *, afi_t, safi_t, u_int32_t, u_char, int, u_int16_t); extern int peer_maximum_prefix_unset (struct peer *, afi_t, safi_t); + +#ifdef HAVE_TCP_MD5 +int peer_password_set (struct peer *, const char *); +int peer_password_unset (struct peer *); +#endif /* HAVE_TCP_MD5 */ extern int peer_clear (struct peer *); extern int peer_clear_soft (struct peer *, afi_t, safi_t, enum bgp_clear_type); diff -uar configure.ac configure.ac --- configure.ac Fri Dec 8 20:31:44 2006 +++ configure.ac Tue Feb 13 13:15:38 2007 @@ -233,6 +233,8 @@ [ --enable-isis-topology enable IS-IS topology generator]) AC_ARG_ENABLE(capabilities, [ --disable-capabilities disable using POSIX capabilities]) +AC_ARG_ENABLE(tcp-md5, +[ --enable-tcp-md5 enable TCP MD5 Signature Option (RFC2385)]) AC_ARG_ENABLE(gcc_ultra_verbose, [ --enable-gcc-ultra-verbose enable ultra verbose GCC warnings]) AC_ARG_ENABLE(gcc-rdynamic, @@ -281,6 +283,11 @@ AC_DEFINE(HAVE_OPAQUE_LSA,,OSPF Opaque LSA) AC_DEFINE(HAVE_OSPF_TE,,OSPF TE) fi + +if test "${enable_tcp_md5}" = "yes"; then + AC_DEFINE(HAVE_TCP_MD5,,Linux TCP MD5 Signature Option) +fi + AC_MSG_CHECKING(if zebra should be configurable to send Route Advertisements) if test "${enable_rtadv}" != "no"; then