No it was not legal, it clearly states in the 10th Amendment "The powers not delegated to the United States by the Constitution, nor prohibited by it to the States, are reserved to the States respectively, or to the people." Meaning that they could secede if the state wanted to since the federal goverment wasn't reserved that power.
Yes it was illegal. It was considered a violation of the amendments and that's why war was necessary to make people agree on something for once. The Confederate States made their own amendments once they seceded but technically, they never actually left the north so those new amendments were considered null and void.