rtadv.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130
  1. /* Router advertisement
  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 "memory.h"
  23. #include "sockopt.h"
  24. #include "thread.h"
  25. #include "if.h"
  26. #include "log.h"
  27. #include "prefix.h"
  28. #include "linklist.h"
  29. #include "command.h"
  30. #include "privs.h"
  31. #include "zebra/interface.h"
  32. #include "zebra/rtadv.h"
  33. #include "zebra/debug.h"
  34. #include "zebra/rib.h"
  35. #include "zebra/zserv.h"
  36. extern struct zebra_privs_t zserv_privs;
  37. #if defined (HAVE_IPV6) && defined (RTADV)
  38. #ifdef OPEN_BSD
  39. #include <netinet/icmp6.h>
  40. #endif
  41. /* If RFC2133 definition is used. */
  42. #ifndef IPV6_JOIN_GROUP
  43. #define IPV6_JOIN_GROUP IPV6_ADD_MEMBERSHIP
  44. #endif
  45. #ifndef IPV6_LEAVE_GROUP
  46. #define IPV6_LEAVE_GROUP IPV6_DROP_MEMBERSHIP
  47. #endif
  48. #define ALLNODE "ff02::1"
  49. #define ALLROUTER "ff02::2"
  50. extern struct zebra_t zebrad;
  51. enum rtadv_event {RTADV_START, RTADV_STOP, RTADV_TIMER, RTADV_READ};
  52. void rtadv_event (enum rtadv_event, int);
  53. int if_join_all_router (int, struct interface *);
  54. int if_leave_all_router (int, struct interface *);
  55. /* Structure which hold status of router advertisement. */
  56. struct rtadv
  57. {
  58. int sock;
  59. int adv_if_count;
  60. struct thread *ra_read;
  61. struct thread *ra_timer;
  62. };
  63. struct rtadv *rtadv = NULL;
  64. struct rtadv *
  65. rtadv_new ()
  66. {
  67. struct rtadv *new;
  68. new = XMALLOC (MTYPE_TMP, sizeof (struct rtadv));
  69. memset (new, 0, sizeof (struct rtadv));
  70. return new;
  71. }
  72. void
  73. rtadv_free (struct rtadv *rtadv)
  74. {
  75. XFREE (MTYPE_TMP, rtadv);
  76. }
  77. int
  78. rtadv_recv_packet (int sock, u_char *buf, int buflen,
  79. struct sockaddr_in6 *from, unsigned int *ifindex,
  80. int *hoplimit)
  81. {
  82. int ret;
  83. struct msghdr msg;
  84. struct iovec iov;
  85. struct cmsghdr *cmsgptr;
  86. struct in6_addr dst;
  87. char adata[1024];
  88. /* Fill in message and iovec. */
  89. msg.msg_name = (void *) from;
  90. msg.msg_namelen = sizeof (struct sockaddr_in6);
  91. msg.msg_iov = &iov;
  92. msg.msg_iovlen = 1;
  93. msg.msg_control = (void *) adata;
  94. msg.msg_controllen = sizeof adata;
  95. iov.iov_base = buf;
  96. iov.iov_len = buflen;
  97. /* If recvmsg fail return minus value. */
  98. ret = recvmsg (sock, &msg, 0);
  99. if (ret < 0)
  100. return ret;
  101. for (cmsgptr = CMSG_FIRSTHDR(&msg); cmsgptr != NULL;
  102. cmsgptr = CMSG_NXTHDR(&msg, cmsgptr))
  103. {
  104. /* I want interface index which this packet comes from. */
  105. if (cmsgptr->cmsg_level == IPPROTO_IPV6 &&
  106. cmsgptr->cmsg_type == IPV6_PKTINFO)
  107. {
  108. struct in6_pktinfo *ptr;
  109. ptr = (struct in6_pktinfo *) CMSG_DATA (cmsgptr);
  110. *ifindex = ptr->ipi6_ifindex;
  111. memcpy(&dst, &ptr->ipi6_addr, sizeof(ptr->ipi6_addr));
  112. }
  113. /* Incoming packet's hop limit. */
  114. if (cmsgptr->cmsg_level == IPPROTO_IPV6 &&
  115. cmsgptr->cmsg_type == IPV6_HOPLIMIT)
  116. *hoplimit = *((int *) CMSG_DATA (cmsgptr));
  117. }
  118. return ret;
  119. }
  120. #define RTADV_MSG_SIZE 4096
  121. /* Send router advertisement packet. */
  122. void
  123. rtadv_send_packet (int sock, struct interface *ifp)
  124. {
  125. struct msghdr msg;
  126. struct iovec iov;
  127. struct cmsghdr *cmsgptr;
  128. struct in6_pktinfo *pkt;
  129. struct sockaddr_in6 addr;
  130. #ifdef HAVE_SOCKADDR_DL
  131. struct sockaddr_dl *sdl;
  132. #endif /* HAVE_SOCKADDR_DL */
  133. char adata [sizeof (struct cmsghdr) + sizeof (struct in6_pktinfo)];
  134. unsigned char buf[RTADV_MSG_SIZE];
  135. struct nd_router_advert *rtadv;
  136. int ret;
  137. int len = 0;
  138. struct zebra_if *zif;
  139. u_char all_nodes_addr[] = {0xff,0x02,0,0,0,0,0,0,0,0,0,0,0,0,0,1};
  140. listnode node;
  141. /* Logging of packet. */
  142. if (IS_ZEBRA_DEBUG_PACKET)
  143. zlog_info ("Router advertisement send to %s", ifp->name);
  144. /* Fill in sockaddr_in6. */
  145. memset (&addr, 0, sizeof (struct sockaddr_in6));
  146. addr.sin6_family = AF_INET6;
  147. #ifdef SIN6_LEN
  148. addr.sin6_len = sizeof (struct sockaddr_in6);
  149. #endif /* SIN6_LEN */
  150. addr.sin6_port = htons (IPPROTO_ICMPV6);
  151. memcpy (&addr.sin6_addr, all_nodes_addr, sizeof (struct in6_addr));
  152. /* Fetch interface information. */
  153. zif = ifp->info;
  154. /* Make router advertisement message. */
  155. rtadv = (struct nd_router_advert *) buf;
  156. rtadv->nd_ra_type = ND_ROUTER_ADVERT;
  157. rtadv->nd_ra_code = 0;
  158. rtadv->nd_ra_cksum = 0;
  159. rtadv->nd_ra_curhoplimit = 64;
  160. rtadv->nd_ra_flags_reserved = 0;
  161. if (zif->rtadv.AdvManagedFlag)
  162. rtadv->nd_ra_flags_reserved |= ND_RA_FLAG_MANAGED;
  163. if (zif->rtadv.AdvOtherConfigFlag)
  164. rtadv->nd_ra_flags_reserved |= ND_RA_FLAG_OTHER;
  165. rtadv->nd_ra_router_lifetime = htons (zif->rtadv.AdvDefaultLifetime);
  166. rtadv->nd_ra_reachable = htonl (zif->rtadv.AdvReachableTime);
  167. rtadv->nd_ra_retransmit = htonl (0);
  168. len = sizeof (struct nd_router_advert);
  169. /* Fill in prefix. */
  170. for (node = listhead (zif->rtadv.AdvPrefixList); node; node = nextnode (node))
  171. {
  172. struct nd_opt_prefix_info *pinfo;
  173. struct rtadv_prefix *rprefix;
  174. rprefix = getdata (node);
  175. pinfo = (struct nd_opt_prefix_info *) (buf + len);
  176. pinfo->nd_opt_pi_type = ND_OPT_PREFIX_INFORMATION;
  177. pinfo->nd_opt_pi_len = 4;
  178. pinfo->nd_opt_pi_prefix_len = rprefix->prefix.prefixlen;
  179. pinfo->nd_opt_pi_flags_reserved = 0;
  180. if (rprefix->AdvOnLinkFlag)
  181. pinfo->nd_opt_pi_flags_reserved |= ND_OPT_PI_FLAG_ONLINK;
  182. if (rprefix->AdvAutonomousFlag)
  183. pinfo->nd_opt_pi_flags_reserved |= ND_OPT_PI_FLAG_AUTO;
  184. pinfo->nd_opt_pi_valid_time = htonl (rprefix->AdvValidLifetime);
  185. pinfo->nd_opt_pi_preferred_time = htonl (rprefix->AdvPreferredLifetime);
  186. pinfo->nd_opt_pi_reserved2 = 0;
  187. memcpy (&pinfo->nd_opt_pi_prefix, &rprefix->prefix.u.prefix6,
  188. sizeof (struct in6_addr));
  189. #ifdef DEBUG
  190. {
  191. u_char buf[INET6_ADDRSTRLEN];
  192. zlog_info ("DEBUG %s", inet_ntop (AF_INET6, &pinfo->nd_opt_pi_prefix, buf, INET6_ADDRSTRLEN));
  193. }
  194. #endif /* DEBUG */
  195. len += sizeof (struct nd_opt_prefix_info);
  196. }
  197. /* Hardware address. */
  198. #ifdef HAVE_SOCKADDR_DL
  199. sdl = &ifp->sdl;
  200. if (sdl != NULL && sdl->sdl_alen != 0)
  201. {
  202. buf[len++] = ND_OPT_SOURCE_LINKADDR;
  203. buf[len++] = (sdl->sdl_alen + 2) >> 3;
  204. memcpy (buf + len, LLADDR (sdl), sdl->sdl_alen);
  205. len += sdl->sdl_alen;
  206. }
  207. #else
  208. if (ifp->hw_addr_len != 0)
  209. {
  210. buf[len++] = ND_OPT_SOURCE_LINKADDR;
  211. buf[len++] = (ifp->hw_addr_len + 2) >> 3;
  212. memcpy (buf + len, ifp->hw_addr, ifp->hw_addr_len);
  213. len += ifp->hw_addr_len;
  214. }
  215. #endif /* HAVE_SOCKADDR_DL */
  216. msg.msg_name = (void *) &addr;
  217. msg.msg_namelen = sizeof (struct sockaddr_in6);
  218. msg.msg_iov = &iov;
  219. msg.msg_iovlen = 1;
  220. msg.msg_control = (void *) adata;
  221. msg.msg_controllen = sizeof adata;
  222. iov.iov_base = buf;
  223. iov.iov_len = len;
  224. cmsgptr = (struct cmsghdr *)adata;
  225. cmsgptr->cmsg_len = sizeof adata;
  226. cmsgptr->cmsg_level = IPPROTO_IPV6;
  227. cmsgptr->cmsg_type = IPV6_PKTINFO;
  228. pkt = (struct in6_pktinfo *) CMSG_DATA (cmsgptr);
  229. memset (&pkt->ipi6_addr, 0, sizeof (struct in6_addr));
  230. pkt->ipi6_ifindex = ifp->ifindex;
  231. ret = sendmsg (sock, &msg, 0);
  232. if (ret < 0)
  233. {
  234. zlog_err ("rtadv_send_packet: sendmsg %d (%s)\n",
  235. errno, strerror(errno));
  236. }
  237. }
  238. int
  239. rtadv_timer (struct thread *thread)
  240. {
  241. listnode node;
  242. struct interface *ifp;
  243. struct zebra_if *zif;
  244. rtadv->ra_timer = NULL;
  245. rtadv_event (RTADV_TIMER, 1);
  246. for (node = listhead (iflist); node; nextnode (node))
  247. {
  248. ifp = getdata (node);
  249. if (if_is_loopback (ifp))
  250. continue;
  251. zif = ifp->info;
  252. if (zif->rtadv.AdvSendAdvertisements)
  253. if (--zif->rtadv.AdvIntervalTimer <= 0)
  254. {
  255. zif->rtadv.AdvIntervalTimer = zif->rtadv.MaxRtrAdvInterval;
  256. rtadv_send_packet (rtadv->sock, ifp);
  257. }
  258. }
  259. return 0;
  260. }
  261. void
  262. rtadv_process_solicit (struct interface *ifp)
  263. {
  264. zlog_info ("Router solicitation received on %s", ifp->name);
  265. rtadv_send_packet (rtadv->sock, ifp);
  266. }
  267. void
  268. rtadv_process_advert ()
  269. {
  270. zlog_info ("Router advertisement received");
  271. }
  272. void
  273. rtadv_process_packet (u_char *buf, int len, unsigned int ifindex, int hoplimit)
  274. {
  275. struct icmp6_hdr *icmph;
  276. struct interface *ifp;
  277. struct zebra_if *zif;
  278. /* Interface search. */
  279. ifp = if_lookup_by_index (ifindex);
  280. if (ifp == NULL)
  281. {
  282. zlog_warn ("Unknown interface index: %d", ifindex);
  283. return;
  284. }
  285. if (if_is_loopback (ifp))
  286. return;
  287. /* Check interface configuration. */
  288. zif = ifp->info;
  289. if (! zif->rtadv.AdvSendAdvertisements)
  290. return;
  291. /* ICMP message length check. */
  292. if (len < sizeof (struct icmp6_hdr))
  293. {
  294. zlog_warn ("Invalid ICMPV6 packet length: %d", len);
  295. return;
  296. }
  297. icmph = (struct icmp6_hdr *) buf;
  298. /* ICMP message type check. */
  299. if (icmph->icmp6_type != ND_ROUTER_SOLICIT &&
  300. icmph->icmp6_type != ND_ROUTER_ADVERT)
  301. {
  302. zlog_warn ("Unwanted ICMPV6 message type: %d", icmph->icmp6_type);
  303. return;
  304. }
  305. /* Hoplimit check. */
  306. if (hoplimit >= 0 && hoplimit != 255)
  307. {
  308. zlog_warn ("Invalid hoplimit %d for router advertisement ICMP packet",
  309. hoplimit);
  310. return;
  311. }
  312. /* Check ICMP message type. */
  313. if (icmph->icmp6_type == ND_ROUTER_SOLICIT)
  314. rtadv_process_solicit (ifp);
  315. else if (icmph->icmp6_type == ND_ROUTER_ADVERT)
  316. rtadv_process_advert ();
  317. return;
  318. }
  319. int
  320. rtadv_read (struct thread *thread)
  321. {
  322. int sock;
  323. int len;
  324. u_char buf[RTADV_MSG_SIZE];
  325. struct sockaddr_in6 from;
  326. unsigned int ifindex;
  327. int hoplimit = -1;
  328. sock = THREAD_FD (thread);
  329. rtadv->ra_read = NULL;
  330. /* Register myself. */
  331. rtadv_event (RTADV_READ, sock);
  332. len = rtadv_recv_packet (sock, buf, BUFSIZ, &from, &ifindex, &hoplimit);
  333. if (len < 0)
  334. {
  335. zlog_warn ("router solicitation recv failed: %s.", strerror (errno));
  336. return len;
  337. }
  338. rtadv_process_packet (buf, len, ifindex, hoplimit);
  339. return 0;
  340. }
  341. int
  342. rtadv_make_socket (void)
  343. {
  344. int sock;
  345. int ret;
  346. struct icmp6_filter filter;
  347. if ( zserv_privs.change (ZPRIVS_RAISE) )
  348. zlog_err ("rtadv_make_socket: could not raise privs, %s",
  349. strerror (errno) );
  350. sock = socket (AF_INET6, SOCK_RAW, IPPROTO_ICMPV6);
  351. if ( zserv_privs.change (ZPRIVS_LOWER) )
  352. zlog_err ("rtadv_make_socket: could not lower privs, %s",
  353. strerror (errno) );
  354. /* When we can't make ICMPV6 socket simply back. Router
  355. advertisement feature will not be supported. */
  356. if (sock < 0)
  357. return -1;
  358. ret = setsockopt_ipv6_pktinfo (sock, 1);
  359. if (ret < 0)
  360. return ret;
  361. ret = setsockopt_ipv6_multicast_loop (sock, 0);
  362. if (ret < 0)
  363. return ret;
  364. ret = setsockopt_ipv6_unicast_hops (sock, 255);
  365. if (ret < 0)
  366. return ret;
  367. ret = setsockopt_ipv6_multicast_hops (sock, 255);
  368. if (ret < 0)
  369. return ret;
  370. ret = setsockopt_ipv6_hoplimit (sock, 1);
  371. if (ret < 0)
  372. return ret;
  373. ICMP6_FILTER_SETBLOCKALL(&filter);
  374. ICMP6_FILTER_SETPASS (ND_ROUTER_SOLICIT, &filter);
  375. ICMP6_FILTER_SETPASS (ND_ROUTER_ADVERT, &filter);
  376. ret = setsockopt (sock, IPPROTO_ICMPV6, ICMP6_FILTER, &filter,
  377. sizeof (struct icmp6_filter));
  378. if (ret < 0)
  379. {
  380. zlog_info ("ICMP6_FILTER set fail: %s", strerror (errno));
  381. return ret;
  382. }
  383. return sock;
  384. }
  385. struct rtadv_prefix *
  386. rtadv_prefix_new ()
  387. {
  388. struct rtadv_prefix *new;
  389. new = XMALLOC (MTYPE_RTADV_PREFIX, sizeof (struct rtadv_prefix));
  390. memset (new, 0, sizeof (struct rtadv_prefix));
  391. return new;
  392. }
  393. void
  394. rtadv_prefix_free (struct rtadv_prefix *rtadv_prefix)
  395. {
  396. XFREE (MTYPE_RTADV_PREFIX, rtadv_prefix);
  397. }
  398. struct rtadv_prefix *
  399. rtadv_prefix_lookup (list rplist, struct prefix *p)
  400. {
  401. listnode node;
  402. struct rtadv_prefix *rprefix;
  403. for (node = listhead (rplist); node; node = nextnode (node))
  404. {
  405. rprefix = getdata (node);
  406. if (prefix_same (&rprefix->prefix, p))
  407. return rprefix;
  408. }
  409. return NULL;
  410. }
  411. struct rtadv_prefix *
  412. rtadv_prefix_get (list rplist, struct prefix *p)
  413. {
  414. struct rtadv_prefix *rprefix;
  415. rprefix = rtadv_prefix_lookup (rplist, p);
  416. if (rprefix)
  417. return rprefix;
  418. rprefix = rtadv_prefix_new ();
  419. memcpy (&rprefix->prefix, p, sizeof (struct prefix));
  420. listnode_add (rplist, rprefix);
  421. return rprefix;
  422. }
  423. void
  424. rtadv_prefix_set (struct zebra_if *zif, struct rtadv_prefix *rp)
  425. {
  426. struct rtadv_prefix *rprefix;
  427. rprefix = rtadv_prefix_get (zif->rtadv.AdvPrefixList, &rp->prefix);
  428. /* Set parameters. */
  429. rprefix->AdvValidLifetime = rp->AdvValidLifetime;
  430. rprefix->AdvPreferredLifetime = rp->AdvPreferredLifetime;
  431. rprefix->AdvOnLinkFlag = rp->AdvOnLinkFlag;
  432. rprefix->AdvAutonomousFlag = rp->AdvAutonomousFlag;
  433. }
  434. int
  435. rtadv_prefix_reset (struct zebra_if *zif, struct rtadv_prefix *rp)
  436. {
  437. struct rtadv_prefix *rprefix;
  438. rprefix = rtadv_prefix_lookup (zif->rtadv.AdvPrefixList, &rp->prefix);
  439. if (rprefix != NULL)
  440. {
  441. listnode_delete (zif->rtadv.AdvPrefixList, (void *) rprefix);
  442. rtadv_prefix_free (rprefix);
  443. return 1;
  444. }
  445. else
  446. return 0;
  447. }
  448. DEFUN (ipv6_nd_suppress_ra,
  449. ipv6_nd_suppress_ra_cmd,
  450. "ipv6 nd suppress-ra",
  451. IP_STR
  452. "Neighbor discovery\n"
  453. "Suppress Router Advertisement\n")
  454. {
  455. struct interface *ifp;
  456. struct zebra_if *zif;
  457. ifp = vty->index;
  458. zif = ifp->info;
  459. if (if_is_loopback (ifp))
  460. {
  461. vty_out (vty, "Invalid interface%s", VTY_NEWLINE);
  462. return CMD_WARNING;
  463. }
  464. if (zif->rtadv.AdvSendAdvertisements)
  465. {
  466. zif->rtadv.AdvSendAdvertisements = 0;
  467. zif->rtadv.AdvIntervalTimer = 0;
  468. rtadv->adv_if_count--;
  469. if_leave_all_router (rtadv->sock, ifp);
  470. if (rtadv->adv_if_count == 0)
  471. rtadv_event (RTADV_STOP, 0);
  472. }
  473. return CMD_SUCCESS;
  474. }
  475. ALIAS (ipv6_nd_suppress_ra,
  476. no_ipv6_nd_send_ra_cmd,
  477. "no ipv6 nd send-ra",
  478. NO_STR
  479. IP_STR
  480. "Neighbor discovery\n"
  481. "Send Router Advertisement\n")
  482. DEFUN (no_ipv6_nd_suppress_ra,
  483. no_ipv6_nd_suppress_ra_cmd,
  484. "no ipv6 nd suppress-ra",
  485. NO_STR
  486. IP_STR
  487. "Neighbor discovery\n"
  488. "Suppress Router Advertisement\n")
  489. {
  490. struct interface *ifp;
  491. struct zebra_if *zif;
  492. ifp = vty->index;
  493. zif = ifp->info;
  494. if (if_is_loopback (ifp))
  495. {
  496. vty_out (vty, "Invalid interface%s", VTY_NEWLINE);
  497. return CMD_WARNING;
  498. }
  499. if (! zif->rtadv.AdvSendAdvertisements)
  500. {
  501. zif->rtadv.AdvSendAdvertisements = 1;
  502. zif->rtadv.AdvIntervalTimer = 0;
  503. rtadv->adv_if_count++;
  504. if_join_all_router (rtadv->sock, ifp);
  505. if (rtadv->adv_if_count == 1)
  506. rtadv_event (RTADV_START, rtadv->sock);
  507. }
  508. return CMD_SUCCESS;
  509. }
  510. ALIAS (no_ipv6_nd_suppress_ra,
  511. ipv6_nd_send_ra_cmd,
  512. "ipv6 nd send-ra",
  513. IP_STR
  514. "Neighbor discovery\n"
  515. "Send Router Advertisement\n")
  516. DEFUN (ipv6_nd_ra_interval,
  517. ipv6_nd_ra_interval_cmd,
  518. "ipv6 nd ra-interval SECONDS",
  519. IP_STR
  520. "Neighbor discovery\n"
  521. "Router Advertisement interval\n"
  522. "Router Advertisement interval in seconds\n")
  523. {
  524. int interval;
  525. struct interface *ifp;
  526. struct zebra_if *zif;
  527. ifp = (struct interface *) vty->index;
  528. zif = ifp->info;
  529. interval = atoi (argv[0]);
  530. if (interval < 0)
  531. {
  532. vty_out (vty, "Invalid Router Advertisement Interval%s", VTY_NEWLINE);
  533. return CMD_WARNING;
  534. }
  535. zif->rtadv.MaxRtrAdvInterval = interval;
  536. zif->rtadv.MinRtrAdvInterval = 0.33 * interval;
  537. zif->rtadv.AdvIntervalTimer = 0;
  538. return CMD_SUCCESS;
  539. }
  540. DEFUN (no_ipv6_nd_ra_interval,
  541. no_ipv6_nd_ra_interval_cmd,
  542. "no ipv6 nd ra-interval",
  543. NO_STR
  544. IP_STR
  545. "Neighbor discovery\n"
  546. "Router Advertisement interval\n")
  547. {
  548. struct interface *ifp;
  549. struct zebra_if *zif;
  550. ifp = (struct interface *) vty->index;
  551. zif = ifp->info;
  552. zif->rtadv.MaxRtrAdvInterval = RTADV_MAX_RTR_ADV_INTERVAL;
  553. zif->rtadv.MinRtrAdvInterval = RTADV_MIN_RTR_ADV_INTERVAL;
  554. zif->rtadv.AdvIntervalTimer = zif->rtadv.MaxRtrAdvInterval;
  555. return CMD_SUCCESS;
  556. }
  557. DEFUN (ipv6_nd_ra_lifetime,
  558. ipv6_nd_ra_lifetime_cmd,
  559. "ipv6 nd ra-lifetime SECONDS",
  560. IP_STR
  561. "Neighbor discovery\n"
  562. "Router lifetime\n"
  563. "Router lifetime in seconds\n")
  564. {
  565. int lifetime;
  566. struct interface *ifp;
  567. struct zebra_if *zif;
  568. ifp = (struct interface *) vty->index;
  569. zif = ifp->info;
  570. lifetime = atoi (argv[0]);
  571. if (lifetime < 0 || lifetime > 0xffff)
  572. {
  573. vty_out (vty, "Invalid Router Lifetime%s", VTY_NEWLINE);
  574. return CMD_WARNING;
  575. }
  576. zif->rtadv.AdvDefaultLifetime = lifetime;
  577. return CMD_SUCCESS;
  578. }
  579. DEFUN (no_ipv6_nd_ra_lifetime,
  580. no_ipv6_nd_ra_lifetime_cmd,
  581. "no ipv6 nd ra-lifetime",
  582. NO_STR
  583. IP_STR
  584. "Neighbor discovery\n"
  585. "Router lifetime\n")
  586. {
  587. struct interface *ifp;
  588. struct zebra_if *zif;
  589. ifp = (struct interface *) vty->index;
  590. zif = ifp->info;
  591. zif->rtadv.AdvDefaultLifetime = RTADV_ADV_DEFAULT_LIFETIME;
  592. return CMD_SUCCESS;
  593. }
  594. DEFUN (ipv6_nd_reachable_time,
  595. ipv6_nd_reachable_time_cmd,
  596. "ipv6 nd reachable-time MILLISECONDS",
  597. IP_STR
  598. "Neighbor discovery\n"
  599. "Reachable time\n"
  600. "Reachable time in milliseconds\n")
  601. {
  602. u_int32_t rtime;
  603. struct interface *ifp;
  604. struct zebra_if *zif;
  605. ifp = (struct interface *) vty->index;
  606. zif = ifp->info;
  607. rtime = (u_int32_t) atol (argv[0]);
  608. if (rtime > RTADV_MAX_REACHABLE_TIME)
  609. {
  610. vty_out (vty, "Invalid Reachable time%s", VTY_NEWLINE);
  611. return CMD_WARNING;
  612. }
  613. zif->rtadv.AdvReachableTime = rtime;
  614. return CMD_SUCCESS;
  615. }
  616. DEFUN (no_ipv6_nd_reachable_time,
  617. no_ipv6_nd_reachable_time_cmd,
  618. "no ipv6 nd reachable-time",
  619. NO_STR
  620. IP_STR
  621. "Neighbor discovery\n"
  622. "Reachable time\n")
  623. {
  624. struct interface *ifp;
  625. struct zebra_if *zif;
  626. ifp = (struct interface *) vty->index;
  627. zif = ifp->info;
  628. zif->rtadv.AdvReachableTime = 0;
  629. return CMD_SUCCESS;
  630. }
  631. DEFUN (ipv6_nd_managed_config_flag,
  632. ipv6_nd_managed_config_flag_cmd,
  633. "ipv6 nd managed-config-flag",
  634. IP_STR
  635. "Neighbor discovery\n"
  636. "Managed address configuration flag\n")
  637. {
  638. struct interface *ifp;
  639. struct zebra_if *zif;
  640. ifp = (struct interface *) vty->index;
  641. zif = ifp->info;
  642. zif->rtadv.AdvManagedFlag = 1;
  643. return CMD_SUCCESS;
  644. }
  645. DEFUN (no_ipv6_nd_managed_config_flag,
  646. no_ipv6_nd_managed_config_flag_cmd,
  647. "no ipv6 nd managed-config-flag",
  648. NO_STR
  649. IP_STR
  650. "Neighbor discovery\n"
  651. "Managed address configuration flag\n")
  652. {
  653. struct interface *ifp;
  654. struct zebra_if *zif;
  655. ifp = (struct interface *) vty->index;
  656. zif = ifp->info;
  657. zif->rtadv.AdvManagedFlag = 0;
  658. return CMD_SUCCESS;
  659. }
  660. DEFUN (ipv6_nd_other_config_flag,
  661. ipv6_nd_other_config_flag_cmd,
  662. "ipv6 nd other-config-flag",
  663. IP_STR
  664. "Neighbor discovery\n"
  665. "Other statefull configuration flag\n")
  666. {
  667. struct interface *ifp;
  668. struct zebra_if *zif;
  669. ifp = (struct interface *) vty->index;
  670. zif = ifp->info;
  671. zif->rtadv.AdvOtherConfigFlag = 1;
  672. return CMD_SUCCESS;
  673. }
  674. DEFUN (no_ipv6_nd_other_config_flag,
  675. no_ipv6_nd_other_config_flag_cmd,
  676. "no ipv6 nd other-config-flag",
  677. NO_STR
  678. IP_STR
  679. "Neighbor discovery\n"
  680. "Other statefull configuration flag\n")
  681. {
  682. struct interface *ifp;
  683. struct zebra_if *zif;
  684. ifp = (struct interface *) vty->index;
  685. zif = ifp->info;
  686. zif->rtadv.AdvOtherConfigFlag = 0;
  687. return CMD_SUCCESS;
  688. }
  689. DEFUN (ipv6_nd_prefix_advertisement,
  690. ipv6_nd_prefix_advertisement_cmd,
  691. "ipv6 nd prefix-advertisement IPV6PREFIX VALID PREFERRED [onlink] [autoconfig]",
  692. IP_STR
  693. "Neighbor discovery\n"
  694. "Prefix information\n"
  695. "IPv6 prefix\n"
  696. "Valid lifetime in seconds\n"
  697. "Preferred lifetime in seconds\n"
  698. "On link flag\n"
  699. "Autonomous address-configuration flag\n")
  700. {
  701. int i;
  702. int ret;
  703. struct interface *ifp;
  704. struct zebra_if *zebra_if;
  705. struct rtadv_prefix rp;
  706. ifp = (struct interface *) vty->index;
  707. zebra_if = ifp->info;
  708. ret = str2prefix_ipv6 (argv[0], (struct prefix_ipv6 *) &rp.prefix);
  709. if (!ret)
  710. {
  711. vty_out (vty, "Malformed IPv6 prefix%s", VTY_NEWLINE);
  712. return CMD_WARNING;
  713. }
  714. if (argc == 1)
  715. {
  716. rp.AdvValidLifetime = RTADV_VALID_LIFETIME;
  717. rp.AdvPreferredLifetime = RTADV_PREFERRED_LIFETIME;
  718. rp.AdvOnLinkFlag = 1;
  719. rp.AdvAutonomousFlag = 1;
  720. }
  721. else
  722. {
  723. rp.AdvValidLifetime = (u_int32_t) atol (argv[1]);
  724. rp.AdvPreferredLifetime = (u_int32_t) atol (argv[2]);
  725. if (rp.AdvPreferredLifetime > rp.AdvValidLifetime)
  726. {
  727. vty_out (vty, "Invalid preferred lifetime%s", VTY_NEWLINE);
  728. return CMD_WARNING;
  729. }
  730. rp.AdvOnLinkFlag = 0;
  731. rp.AdvAutonomousFlag = 0;
  732. for (i = 3; i < argc; i++)
  733. {
  734. if (! strcmp (argv[i], "onlink"))
  735. rp.AdvOnLinkFlag = 1;
  736. else if (! strcmp (argv[i], "autoconfig"))
  737. rp.AdvAutonomousFlag = 1;
  738. }
  739. }
  740. rtadv_prefix_set (zebra_if, &rp);
  741. return CMD_SUCCESS;
  742. }
  743. ALIAS (ipv6_nd_prefix_advertisement,
  744. ipv6_nd_prefix_advertisement_no_val_cmd,
  745. "ipv6 nd prefix-advertisement IPV6PREFIX",
  746. IP_STR
  747. "Neighbor discovery\n"
  748. "Prefix information\n"
  749. "IPv6 prefix\n")
  750. DEFUN (no_ipv6_nd_prefix_advertisement,
  751. no_ipv6_nd_prefix_advertisement_cmd,
  752. "no ipv6 nd prefix-advertisement IPV6PREFIX",
  753. NO_STR
  754. IP_STR
  755. "Neighbor discovery\n"
  756. "Prefix information\n"
  757. "IPv6 prefix\n")
  758. {
  759. int ret;
  760. struct interface *ifp;
  761. struct zebra_if *zebra_if;
  762. struct rtadv_prefix rp;
  763. ifp = (struct interface *) vty->index;
  764. zebra_if = ifp->info;
  765. ret = str2prefix_ipv6 (argv[0], (struct prefix_ipv6 *) &rp.prefix);
  766. if (!ret)
  767. {
  768. vty_out (vty, "Malformed IPv6 prefix%s", VTY_NEWLINE);
  769. return CMD_WARNING;
  770. }
  771. ret = rtadv_prefix_reset (zebra_if, &rp);
  772. if (!ret)
  773. {
  774. vty_out (vty, "Non-exist IPv6 prefix%s", VTY_NEWLINE);
  775. return CMD_WARNING;
  776. }
  777. return CMD_SUCCESS;
  778. }
  779. /* Write configuration about router advertisement. */
  780. void
  781. rtadv_config_write (struct vty *vty, struct interface *ifp)
  782. {
  783. struct zebra_if *zif;
  784. listnode node;
  785. struct rtadv_prefix *rprefix;
  786. u_char buf[INET6_ADDRSTRLEN];
  787. if (! rtadv)
  788. return;
  789. zif = ifp->info;
  790. if (! if_is_loopback (ifp))
  791. {
  792. if (zif->rtadv.AdvSendAdvertisements)
  793. vty_out (vty, " no ipv6 nd suppress-ra%s", VTY_NEWLINE);
  794. else
  795. vty_out (vty, " ipv6 nd suppress-ra%s", VTY_NEWLINE);
  796. }
  797. if (zif->rtadv.MaxRtrAdvInterval != RTADV_MAX_RTR_ADV_INTERVAL)
  798. vty_out (vty, " ipv6 nd ra-interval %d%s", zif->rtadv.MaxRtrAdvInterval,
  799. VTY_NEWLINE);
  800. if (zif->rtadv.AdvDefaultLifetime != RTADV_ADV_DEFAULT_LIFETIME)
  801. vty_out (vty, " ipv6 nd ra-lifetime %d%s", zif->rtadv.AdvDefaultLifetime,
  802. VTY_NEWLINE);
  803. if (zif->rtadv.AdvReachableTime)
  804. vty_out (vty, " ipv6 nd reachable-time %d%s", zif->rtadv.AdvReachableTime,
  805. VTY_NEWLINE);
  806. if (zif->rtadv.AdvManagedFlag)
  807. vty_out (vty, " ipv6 nd managed-config-flag%s", VTY_NEWLINE);
  808. if (zif->rtadv.AdvOtherConfigFlag)
  809. vty_out (vty, " ipv6 nd other-config-flag%s", VTY_NEWLINE);
  810. for (node = listhead(zif->rtadv.AdvPrefixList); node; node = nextnode (node))
  811. {
  812. rprefix = getdata (node);
  813. vty_out (vty, " ipv6 nd prefix-advertisement %s/%d %d %d",
  814. inet_ntop (AF_INET6, &rprefix->prefix.u.prefix6,
  815. buf, INET6_ADDRSTRLEN),
  816. rprefix->prefix.prefixlen,
  817. rprefix->AdvValidLifetime,
  818. rprefix->AdvPreferredLifetime);
  819. if (rprefix->AdvOnLinkFlag)
  820. vty_out (vty, " onlink");
  821. if (rprefix->AdvAutonomousFlag)
  822. vty_out (vty, " autoconfig");
  823. vty_out (vty, "%s", VTY_NEWLINE);
  824. }
  825. }
  826. void
  827. rtadv_event (enum rtadv_event event, int val)
  828. {
  829. switch (event)
  830. {
  831. case RTADV_START:
  832. if (! rtadv->ra_read)
  833. rtadv->ra_read = thread_add_read (zebrad.master, rtadv_read, NULL, val);
  834. if (! rtadv->ra_timer)
  835. rtadv->ra_timer = thread_add_event (zebrad.master, rtadv_timer, NULL, 0);
  836. break;
  837. case RTADV_STOP:
  838. if (rtadv->ra_timer)
  839. {
  840. thread_cancel (rtadv->ra_timer);
  841. rtadv->ra_timer = NULL;
  842. }
  843. if (rtadv->ra_read)
  844. {
  845. thread_cancel (rtadv->ra_read);
  846. rtadv->ra_read = NULL;
  847. }
  848. break;
  849. case RTADV_TIMER:
  850. if (! rtadv->ra_timer)
  851. rtadv->ra_timer = thread_add_timer (zebrad.master, rtadv_timer, NULL, val);
  852. break;
  853. case RTADV_READ:
  854. if (! rtadv->ra_read)
  855. rtadv->ra_read = thread_add_read (zebrad.master, rtadv_read, NULL, val);
  856. break;
  857. default:
  858. break;
  859. }
  860. return;
  861. }
  862. void
  863. rtadv_init ()
  864. {
  865. int sock;
  866. sock = rtadv_make_socket ();
  867. if (sock < 0)
  868. return;
  869. rtadv = rtadv_new ();
  870. rtadv->sock = sock;
  871. install_element (INTERFACE_NODE, &ipv6_nd_suppress_ra_cmd);
  872. install_element (INTERFACE_NODE, &no_ipv6_nd_suppress_ra_cmd);
  873. install_element (INTERFACE_NODE, &ipv6_nd_send_ra_cmd);
  874. install_element (INTERFACE_NODE, &no_ipv6_nd_send_ra_cmd);
  875. install_element (INTERFACE_NODE, &ipv6_nd_ra_interval_cmd);
  876. install_element (INTERFACE_NODE, &no_ipv6_nd_ra_interval_cmd);
  877. install_element (INTERFACE_NODE, &ipv6_nd_ra_lifetime_cmd);
  878. install_element (INTERFACE_NODE, &no_ipv6_nd_ra_lifetime_cmd);
  879. install_element (INTERFACE_NODE, &ipv6_nd_reachable_time_cmd);
  880. install_element (INTERFACE_NODE, &no_ipv6_nd_reachable_time_cmd);
  881. install_element (INTERFACE_NODE, &ipv6_nd_managed_config_flag_cmd);
  882. install_element (INTERFACE_NODE, &no_ipv6_nd_managed_config_flag_cmd);
  883. install_element (INTERFACE_NODE, &ipv6_nd_other_config_flag_cmd);
  884. install_element (INTERFACE_NODE, &no_ipv6_nd_other_config_flag_cmd);
  885. install_element (INTERFACE_NODE, &ipv6_nd_prefix_advertisement_cmd);
  886. install_element (INTERFACE_NODE, &ipv6_nd_prefix_advertisement_no_val_cmd);
  887. install_element (INTERFACE_NODE, &no_ipv6_nd_prefix_advertisement_cmd);
  888. }
  889. int
  890. if_join_all_router (int sock, struct interface *ifp)
  891. {
  892. int ret;
  893. struct ipv6_mreq mreq;
  894. memset (&mreq, 0, sizeof (struct ipv6_mreq));
  895. inet_pton (AF_INET6, ALLROUTER, &mreq.ipv6mr_multiaddr);
  896. mreq.ipv6mr_interface = ifp->ifindex;
  897. ret = setsockopt (sock, IPPROTO_IPV6, IPV6_JOIN_GROUP,
  898. (char *) &mreq, sizeof mreq);
  899. if (ret < 0)
  900. zlog_warn ("can't setsockopt IPV6_JOIN_GROUP: %s", strerror (errno));
  901. zlog_info ("rtadv: %s join to all-routers multicast group", ifp->name);
  902. return 0;
  903. }
  904. int
  905. if_leave_all_router (int sock, struct interface *ifp)
  906. {
  907. int ret;
  908. struct ipv6_mreq mreq;
  909. memset (&mreq, 0, sizeof (struct ipv6_mreq));
  910. inet_pton (AF_INET6, ALLROUTER, &mreq.ipv6mr_multiaddr);
  911. mreq.ipv6mr_interface = ifp->ifindex;
  912. ret = setsockopt (sock, IPPROTO_IPV6, IPV6_LEAVE_GROUP,
  913. (char *) &mreq, sizeof mreq);
  914. if (ret < 0)
  915. zlog_warn ("can't setsockopt IPV6_LEAVE_GROUP: %s", strerror (errno));
  916. zlog_info ("rtadv: %s leave from all-routers multicast group", ifp->name);
  917. return 0;
  918. }
  919. #else
  920. void
  921. rtadv_init ()
  922. {
  923. /* Empty.*/;
  924. }
  925. #endif /* RTADV && HAVE_IPV6 */