⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 irdp_interface.c

📁 linux 路由软件 可支持RIP OSPF BGP等
💻 C
📖 第 1 页 / 共 2 页
字号:
/* * * Copyright (C) 2000  Robert Olsson. * Swedish University of Agricultural Sciences * * This file is part of GNU Zebra. * * GNU Zebra is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the * Free Software Foundation; either version 2, or (at your option) any * later version. * * GNU Zebra is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU * General Public License for more details. * * You should have received a copy of the GNU General Public License * along with GNU Zebra; see the file COPYING.  If not, write to the Free * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA * 02111-1307, USA.   *//*  * This work includes work with the following copywrite: * * Copyright (C) 1997, 2000 Kunihiro Ishiguro * *//*  * Thanks to Jens L邋s at Swedish University of Agricultural Sciences * for reviewing and tests. */#include <zebra.h>#ifdef HAVE_IRDP #include "if.h"#include "vty.h"#include "sockunion.h"#include "prefix.h"#include "command.h"#include "memory.h"#include "stream.h"#include "ioctl.h"#include "connected.h"#include "log.h"#include "zclient.h"#include "thread.h"#include "zebra/interface.h"#include "zebra/rtadv.h"#include "zebra/rib.h"#include "zebra/zserv.h"#include "zebra/redistribute.h"#include "zebra/irdp.h"#include <netinet/ip_icmp.h>#include "if.h"#include "sockunion.h"#include "log.h"/* Master of threads. */extern struct thread_master *master;int in_cksum (void *ptr, int nbytes);extern int irdp_sock;int irdp_send_thread(struct thread *t_advert);char *inet_2a(u_int32_t a, char *b);void irdp_advert_off(struct interface *ifp);char b1[16], b2[16], b3[16], b4[16];  /* For inet_2a */struct prefix *irdp_get_prefix(struct interface *ifp){  listnode node;  struct connected *ifc;    if(ifp->connected)     for (node = listhead (ifp->connected); node; nextnode (node)) {      ifc = getdata (node);      return ifc->address;    }  return NULL;}/* Join to the add/leave multicast group. */int if_group (struct interface *ifp, 	  int sock, 	  u_int32_t group, 	  int add_leave){  struct zebra_if *zi;  struct ip_mreq m;  struct prefix *p;  int ret;  zi = ifp->info;  bzero (&m, sizeof (m));  m.imr_multiaddr.s_addr = htonl (group);  p = irdp_get_prefix(ifp);  if(!p) {        zlog_warn ("IRDP: can't get address for %s", ifp->name);	return 1;  }  m.imr_interface = p->u.prefix4;  ret = setsockopt (sock, IPPROTO_IP, add_leave,		    (char *) &m, sizeof (struct ip_mreq));  if (ret < 0)    zlog_warn ("IRDP: %s can't setsockopt %s: %s",	       add_leave == IP_ADD_MEMBERSHIP? "join group":"leave group",                inet_2a(group, b1),               strerror (errno));  return ret;}int if_add_group (struct interface *ifp){  struct zebra_if *zi= ifp->info;  struct irdp_interface *irdp = &zi->irdp;  int ret;  ret = if_group (ifp, irdp_sock, INADDR_ALLRTRS_GROUP, IP_ADD_MEMBERSHIP);  if (ret < 0) {    return ret;  }  if(irdp->flags & IF_DEBUG_MISC )    zlog_warn("IRDP: Adding group %s for %s\n", 	      inet_2a(htonl(INADDR_ALLRTRS_GROUP), b1),	      ifp->name);  return 0;}int if_drop_group (struct interface *ifp){  struct zebra_if *zi= ifp->info;  struct irdp_interface *irdp = &zi->irdp;  int ret;  ret = if_group (ifp, irdp_sock, INADDR_ALLRTRS_GROUP, IP_DROP_MEMBERSHIP);  if (ret < 0)    return ret;  if(irdp->flags & IF_DEBUG_MISC)    zlog_warn("IRDP: Leaving group %s for %s\n", 	      inet_2a(htonl(INADDR_ALLRTRS_GROUP), b1),	      ifp->name);  return 0;}struct interface *get_iflist_ifp(int idx){  listnode node;  struct interface *ifp;  for (node = listhead (iflist); node; nextnode (node)) {      ifp = getdata (node);      if(ifp->ifindex == idx) return ifp;    }  return NULL;}voidif_set_defaults(struct interface *ifp){  struct zebra_if *zi=ifp->info;  struct irdp_interface *irdp=&zi->irdp;  irdp->MaxAdvertInterval = IRDP_MAXADVERTINTERVAL;  irdp->MinAdvertInterval = IRDP_MINADVERTINTERVAL;  irdp->Preference = IRDP_PREFERENCE;  irdp->Lifetime = IRDP_LIFETIME;}struct Adv *Adv_new (){  struct Adv *new;  new = XMALLOC (MTYPE_TMP, sizeof (struct Adv));  memset (new, 0, sizeof (struct Adv));  return new;}void Adv_free (struct Adv *adv){  XFREE (MTYPE_TMP, adv);}void irdp_if_start(struct interface *ifp, int multicast, int set_defaults){  struct zebra_if *zi= ifp->info;  struct irdp_interface *irdp = &zi->irdp;  listnode node;  u_int32_t timer, seed;  if (irdp->flags & IF_ACTIVE ) {    zlog_warn("IRDP: Interface is already active %s\n", ifp->name);    return;  }  irdp->flags |= IF_ACTIVE;  if(!multicast)     irdp->flags |= IF_BROADCAST;  if_add_update(ifp);  if (! (ifp->flags & IFF_UP)) {    zlog_warn("IRDP: Interface is down %s\n", ifp->name);  }  /* Shall we cancel if_start if if_add_group fails? */  if( multicast) {    if_add_group(ifp);        if (! (ifp->flags & (IFF_MULTICAST|IFF_ALLMULTI))) {      zlog_warn("IRDP: Interface not multicast enabled %s\n", ifp->name);    }  }  if(set_defaults)     if_set_defaults(ifp);  irdp->irdp_sent = 0;  /* The spec suggests this for randomness */  seed = 0;  if( ifp->connected) 	  for (node = listhead (ifp->connected); node; nextnode (node)) 	  {		  struct connected *ifc = getdata (node);		  seed = ifc->address->u.prefix4.s_addr;	  }    srandom(seed);  timer =  (random () % IRDP_DEFAULT_INTERVAL) + 1;   irdp->AdvPrefList = list_new();  irdp->AdvPrefList->del =  (void *) Adv_free; /* Destructor */  /* And this for startup. Speed limit from 1991 :-). But it's OK*/  if(irdp->irdp_sent < MAX_INITIAL_ADVERTISEMENTS &&     timer > MAX_INITIAL_ADVERT_INTERVAL ) 	  timer= MAX_INITIAL_ADVERT_INTERVAL;    if(irdp->flags & IF_DEBUG_MISC)    zlog_warn("IRDP: Init timer for %s set to %u\n", 	      ifp->name, 	      timer);  irdp->t_advertise = thread_add_timer(master, 				       irdp_send_thread, 				       ifp, 				       timer);}void irdp_if_stop(struct interface *ifp){  struct zebra_if *zi=ifp->info;  struct irdp_interface *irdp=&zi->irdp;    if (irdp == NULL) {    zlog_warn ("Interface %s structure is NULL", ifp->name);    return;  }  if (! (irdp->flags & IF_ACTIVE )) {    zlog_warn("Interface is not active %s\n", ifp->name);    return;  }  if(! (irdp->flags & IF_BROADCAST))     if_drop_group(ifp);  irdp_advert_off(ifp);  list_delete(irdp->AdvPrefList);  irdp->AdvPrefList=NULL;  irdp->flags = 0;}void irdp_if_shutdown(struct interface *ifp){  struct zebra_if *zi= ifp->info;  struct irdp_interface *irdp = &zi->irdp;  if (irdp->flags & IF_SHUTDOWN ) {    zlog_warn("IRDP: Interface is already shutdown %s\n", ifp->name);    return;  }  irdp->flags |= IF_SHUTDOWN;  irdp->flags &= ~IF_ACTIVE;  if(! (irdp->flags & IF_BROADCAST))     if_drop_group(ifp);    /* Tell the hosts we are out of service */  irdp_advert_off(ifp);}void irdp_if_no_shutdown(struct interface *ifp){  struct zebra_if *zi= ifp->info;  struct irdp_interface *irdp = &zi->irdp;  if (! (irdp->flags & IF_SHUTDOWN )) {    zlog_warn("IRDP: Interface is not shutdown %s\n", ifp->name);    return;  }  irdp->flags &= ~IF_SHUTDOWN;  irdp_if_start(ifp, irdp->flags & IF_BROADCAST? FALSE : TRUE, FALSE); }/* Write configuration to user */void irdp_config_write (struct vty *vty, struct interface *ifp){  struct zebra_if *zi=ifp->info;  struct irdp_interface *irdp=&zi->irdp;  struct Adv *adv;  listnode node;  if(irdp->flags & IF_ACTIVE || irdp->flags & IF_SHUTDOWN) {    if( irdp->flags & IF_SHUTDOWN)       vty_out (vty, " ip irdp shutdown %s",  VTY_NEWLINE);    if( irdp->flags & IF_BROADCAST)       vty_out (vty, " ip irdp broadcast%s",  VTY_NEWLINE);    else       vty_out (vty, " ip irdp multicast%s",  VTY_NEWLINE);    vty_out (vty, " ip irdp preference %ld%s",  	     irdp->Preference, VTY_NEWLINE);    for (node = listhead (irdp->AdvPrefList); node; nextnode (node)) {	    adv = getdata (node);	    vty_out (vty, " ip irdp address %s preference %d%s",		     inet_2a(adv->ip.s_addr, b1),		     adv->pref,		     VTY_NEWLINE);    }    vty_out (vty, " ip irdp holdtime %d%s",  	     irdp->Lifetime, VTY_NEWLINE);    vty_out (vty, " ip irdp minadvertinterval %ld%s",  	     irdp->MinAdvertInterval, VTY_NEWLINE);    vty_out (vty, " ip irdp maxadvertinterval %ld%s",  	     irdp->MaxAdvertInterval, VTY_NEWLINE);  }}DEFUN (ip_irdp_multicast,       ip_irdp_multicast_cmd,       "ip irdp multicast",       IP_STR       "ICMP Router discovery on this interface using multicast\n"){  struct interface *ifp;  ifp = (struct interface *) vty->index;  if(!ifp) {	  return CMD_WARNING;  }  irdp_if_start(ifp, TRUE, TRUE);  return CMD_SUCCESS;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -