📄 ospf_interface.c
字号:
p.prefixlen = IPV4_MAX_PREFIXLEN; p.prefix = addr; rn = route_node_lookup (IF_OIFS_PARAMS (ifp), (struct prefix*)&p); if (rn) { route_unlock_node (rn); return rn->info; } return NULL;}struct ospf_if_params *ospf_get_if_params (struct interface *ifp, struct in_addr addr){ struct prefix_ipv4 p; struct route_node *rn; p.family = AF_INET; p.prefixlen = IPV4_MAX_PREFIXLEN; p.prefix = addr; rn = route_node_get (IF_OIFS_PARAMS (ifp), (struct prefix*)&p); if (rn->info == NULL) rn->info = ospf_new_if_params (); else route_unlock_node (rn); return rn->info;}voidospf_if_update_params (struct interface *ifp, struct in_addr addr){ struct route_node *rn; struct ospf_interface *oi; for (rn = route_top (IF_OIFS (ifp)); rn; rn = route_next (rn)) { if ((oi = rn->info) == NULL) continue; if (IPV4_ADDR_SAME (&oi->address->u.prefix4, &addr)) oi->params = ospf_lookup_if_params (ifp, oi->address->u.prefix4); }}intospf_if_new_hook (struct interface *ifp){ int rc = 0; ifp->info = XMALLOC (MTYPE_OSPF_IF_INFO, sizeof (struct ospf_if_info)); memset (ifp->info, 0, sizeof (struct ospf_if_info)); IF_OIFS (ifp) = route_table_init (); IF_OIFS_PARAMS (ifp) = route_table_init (); IF_DEF_PARAMS (ifp) = ospf_new_if_params (); SET_IF_PARAM (IF_DEF_PARAMS (ifp), transmit_delay); IF_DEF_PARAMS (ifp)->transmit_delay = OSPF_TRANSMIT_DELAY_DEFAULT; SET_IF_PARAM (IF_DEF_PARAMS (ifp), retransmit_interval); IF_DEF_PARAMS (ifp)->retransmit_interval = OSPF_RETRANSMIT_INTERVAL_DEFAULT; SET_IF_PARAM (IF_DEF_PARAMS (ifp), priority); IF_DEF_PARAMS (ifp)->priority = OSPF_ROUTER_PRIORITY_DEFAULT; SET_IF_PARAM (IF_DEF_PARAMS (ifp), passive_interface); IF_DEF_PARAMS (ifp)->passive_interface = OSPF_IF_ACTIVE; SET_IF_PARAM (IF_DEF_PARAMS (ifp), v_hello); IF_DEF_PARAMS (ifp)->v_hello = OSPF_HELLO_INTERVAL_DEFAULT; SET_IF_PARAM (IF_DEF_PARAMS (ifp), v_wait); IF_DEF_PARAMS (ifp)->v_wait = OSPF_ROUTER_DEAD_INTERVAL_DEFAULT; SET_IF_PARAM (IF_DEF_PARAMS (ifp), auth_simple); memset (IF_DEF_PARAMS (ifp)->auth_simple, 0, OSPF_AUTH_SIMPLE_SIZE); SET_IF_PARAM (IF_DEF_PARAMS (ifp), auth_crypt); IF_DEF_PARAMS (ifp)->auth_crypt = list_new (); SET_IF_PARAM (IF_DEF_PARAMS (ifp), auth_type); IF_DEF_PARAMS (ifp)->auth_type = OSPF_AUTH_NOTSET; #ifdef HAVE_OPAQUE_LSA rc = ospf_opaque_new_if (ifp);#endif /* HAVE_OPAQUE_LSA */ return rc;}intospf_if_delete_hook (struct interface *ifp){ int rc = 0;#ifdef HAVE_OPAQUE_LSA rc = ospf_opaque_del_if (ifp);#endif /* HAVE_OPAQUE_LSA */ route_table_finish (IF_OIFS (ifp)); route_table_finish (IF_OIFS_PARAMS (ifp)); XFREE (MTYPE_OSPF_IF_INFO, ifp->info); ifp->info = NULL; return rc;}intospf_if_is_enable (struct ospf_interface *oi){ if (!if_is_loopback (oi->ifp)) if (if_is_up (oi->ifp)) return 1; return 0;}intospf_if_up (struct ospf_interface *oi){ if (oi == NULL) return 0; if (oi->type == OSPF_IFTYPE_LOOPBACK) OSPF_ISM_EVENT_SCHEDULE (oi, ISM_LoopInd); else { if (oi->type != OSPF_IFTYPE_VIRTUALLINK) ospf_if_add_allspfrouters (oi->ospf, oi->address, oi->ifp->ifindex); ospf_if_stream_set (oi); OSPF_ISM_EVENT_SCHEDULE (oi, ISM_InterfaceUp); } return 1;}intospf_if_down (struct ospf_interface *oi){ if (oi == NULL) return 0; OSPF_ISM_EVENT_EXECUTE (oi, ISM_InterfaceDown); /* Shutdown packet reception and sending */ ospf_if_stream_unset (oi); if (oi->type != OSPF_IFTYPE_VIRTUALLINK) ospf_if_drop_allspfrouters (oi->ospf, oi->address, oi->ifp->ifindex); return 1;}/* Virtual Link related functions. */struct ospf_vl_data *ospf_vl_data_new (struct ospf_area *area, struct in_addr vl_peer){ struct ospf_vl_data *vl_data; vl_data = XMALLOC (MTYPE_OSPF_VL_DATA, sizeof (struct ospf_vl_data)); memset (vl_data, 0, sizeof (struct ospf_vl_data)); vl_data->vl_peer.s_addr = vl_peer.s_addr; vl_data->vl_area_id = area->area_id; vl_data->format = area->format; return vl_data;}voidospf_vl_data_free (struct ospf_vl_data *vl_data){ XFREE (MTYPE_OSPF_VL_DATA, vl_data);}u_int vlink_count = 0;struct ospf_interface * ospf_vl_new (struct ospf *ospf, struct ospf_vl_data *vl_data){ struct ospf_interface * voi; struct interface * vi; char ifname[INTERFACE_NAMSIZ + 1]; struct ospf_area *area; struct in_addr area_id; struct connected *co; struct prefix_ipv4 *p; if (IS_DEBUG_OSPF_EVENT) zlog_info ("ospf_vl_new(): Start"); if (vlink_count == OSPF_VL_MAX_COUNT) { if (IS_DEBUG_OSPF_EVENT) zlog_info ("ospf_vl_new(): Alarm: " "cannot create more than OSPF_MAX_VL_COUNT virtual links"); return NULL; } if (IS_DEBUG_OSPF_EVENT) zlog_info ("ospf_vl_new(): creating pseudo zebra interface"); vi = if_create (); co = connected_new (); co->ifp = vi; listnode_add (vi->connected, co); p = prefix_ipv4_new (); p->family = AF_INET; p->prefix.s_addr = 0; p->prefixlen = 0; co->address = (struct prefix *)p; voi = ospf_if_new (ospf, vi, co->address); if (voi == NULL) { if (IS_DEBUG_OSPF_EVENT) zlog_info ("ospf_vl_new(): Alarm: OSPF int structure is not created"); return NULL; } voi->connected = co; voi->vl_data = vl_data; voi->ifp->mtu = OSPF_VL_MTU; voi->type = OSPF_IFTYPE_VIRTUALLINK; sprintf (ifname, "VLINK%d", vlink_count++); if (IS_DEBUG_OSPF_EVENT) zlog_info ("ospf_vl_new(): Created name: %s", ifname); strncpy (vi->name, ifname, IFNAMSIZ); if (IS_DEBUG_OSPF_EVENT) zlog_info ("ospf_vl_new(): set if->name to %s", vi->name); area_id.s_addr = 0; area = ospf_area_get (ospf, area_id, OSPF_AREA_ID_FORMAT_ADDRESS); voi->area = area; if (IS_DEBUG_OSPF_EVENT) zlog_info ("ospf_vl_new(): set associated area to the backbone"); ospf_area_add_if (voi->area, voi); ospf_if_stream_set (voi); if (IS_DEBUG_OSPF_EVENT) zlog_info ("ospf_vl_new(): Stop"); return voi;}voidospf_vl_if_delete (struct ospf_vl_data *vl_data){ struct interface *ifp = vl_data->vl_oi->ifp; vl_data->vl_oi->address->u.prefix4.s_addr = 0; vl_data->vl_oi->address->prefixlen = 0; ospf_if_free (vl_data->vl_oi); if_delete (ifp); vlink_count--;}struct ospf_vl_data *ospf_vl_lookup (struct ospf_area *area, struct in_addr vl_peer){ struct ospf_vl_data *vl_data; listnode node; for (node = listhead (area->ospf->vlinks); node; nextnode (node)) if ((vl_data = getdata (node)) != NULL) if (vl_data->vl_peer.s_addr == vl_peer.s_addr && IPV4_ADDR_SAME (&vl_data->vl_area_id, &area->area_id)) return vl_data; return NULL;}void ospf_vl_shutdown (struct ospf_vl_data *vl_data){ struct ospf_interface *oi; if ((oi = vl_data->vl_oi) == NULL) return; oi->address->u.prefix4.s_addr = 0; oi->address->prefixlen = 0; UNSET_FLAG (oi->ifp->flags, IFF_UP); /* OSPF_ISM_EVENT_SCHEDULE (oi, ISM_InterfaceDown); */ OSPF_ISM_EVENT_EXECUTE (oi, ISM_InterfaceDown);}voidospf_vl_add (struct ospf *ospf, struct ospf_vl_data *vl_data){ listnode_add (ospf->vlinks, vl_data);#ifdef HAVE_SNMP ospf_snmp_vl_add (vl_data);#endif /* HAVE_SNMP */}voidospf_vl_delete (struct ospf *ospf, struct ospf_vl_data *vl_data){ ospf_vl_shutdown (vl_data); ospf_vl_if_delete (vl_data);#ifdef HAVE_SNMP ospf_snmp_vl_delete (vl_data);#endif /* HAVE_SNMP */ listnode_delete (ospf->vlinks, vl_data); ospf_vl_data_free (vl_data);}voidospf_vl_set_params (struct ospf_vl_data *vl_data, struct vertex *v){ int changed = 0; struct ospf_interface *voi; listnode node; struct vertex_nexthop *nh; int i; struct router_lsa *rl; voi = vl_data->vl_oi; if (voi->output_cost != v->distance) { voi->output_cost = v->distance; changed = 1; } for (node = listhead (v->nexthop); node; nextnode (node)) if ((nh = getdata (node)) != NULL) { vl_data->out_oi = (struct ospf_interface *) nh->oi; voi->address->u.prefix4 = vl_data->out_oi->address->u.prefix4; voi->address->prefixlen = vl_data->out_oi->address->prefixlen; break; /* We take the first interface. */ } rl = (struct router_lsa *)v->lsa; for (i = 0; i < ntohs (rl->links); i++) { switch (rl->link[i].type) { case LSA_LINK_TYPE_VIRTUALLINK: if (IS_DEBUG_OSPF_EVENT) zlog_info ("found back link through VL"); case LSA_LINK_TYPE_TRANSIT: case LSA_LINK_TYPE_POINTOPOINT: vl_data->peer_addr = rl->link[i].link_data; if (IS_DEBUG_OSPF_EVENT) zlog_info ("%s peer address is %s\n", vl_data->vl_oi->ifp->name, inet_ntoa(vl_data->peer_addr)); return; } }}voidospf_vl_up_check (struct ospf_area *area, struct in_addr rid, struct vertex *v){ struct ospf *ospf = area->ospf; listnode node; struct ospf_vl_data *vl_data; struct ospf_interface *oi; if (IS_DEBUG_OSPF_EVENT) { zlog_info ("ospf_vl_up_check(): Start"); zlog_info ("ospf_vl_up_check(): Router ID is %s", inet_ntoa (rid)); zlog_info ("ospf_vl_up_check(): Area is %s", inet_ntoa (area->area_id)); } for (node = listhead (ospf->vlinks); node; nextnode (node)) { if ((vl_data = getdata (node)) == NULL) continue; if (IS_DEBUG_OSPF_EVENT) { zlog_info ("ospf_vl_up_check(): considering VL, name: %s", vl_data->vl_oi->ifp->name); zlog_info ("ospf_vl_up_check(): VL area: %s, peer ID: %s", inet_ntoa (vl_data->vl_area_id), inet_ntoa (vl_data->vl_peer)); } if (IPV4_ADDR_SAME (&vl_data->vl_peer, &rid) && IPV4_ADDR_SAME (&vl_data->vl_area_id, &area->area_id)) { oi = vl_data->vl_oi; SET_FLAG (vl_data->flags, OSPF_VL_FLAG_APPROVED); if (IS_DEBUG_OSPF_EVENT) zlog_info ("ospf_vl_up_check(): this VL matched"); if (oi->state == ISM_Down) { if (IS_DEBUG_OSPF_EVENT) zlog_info ("ospf_vl_up_check(): VL is down, waking it up"); SET_FLAG (oi->ifp->flags, IFF_UP); OSPF_ISM_EVENT_SCHEDULE (oi, ISM_InterfaceUp); } ospf_vl_set_params (vl_data, v); } }}voidospf_vl_unapprove (struct ospf *ospf){ listnode node; struct ospf_vl_data *vl_data; for (node = listhead (ospf->vlinks); node; nextnode (node)) if ((vl_data = getdata (node)) != NULL) UNSET_FLAG (vl_data->flags, OSPF_VL_FLAG_APPROVED);}voidospf_vl_shut_unapproved (struct ospf *ospf){ listnode node; struct ospf_vl_data *vl_data; for (node = listhead (ospf->vlinks); node; nextnode (node)) if ((vl_data = getdata (node)) != NULL) if (!CHECK_FLAG (vl_data->flags, OSPF_VL_FLAG_APPROVED)) ospf_vl_shutdown (vl_data);}intospf_full_virtual_nbrs (struct ospf_area *area){ if (IS_DEBUG_OSPF_EVENT) { zlog_info ("counting fully adjacent virtual neighbors in area %s", inet_ntoa (area->area_id)); zlog_info ("there are %d of them", area->full_vls); } return area->full_vls;}intospf_vls_in_area (struct ospf_area *area){ listnode node; struct ospf_vl_data *vl_data; int c = 0; for (node = listhead (area->ospf->vlinks); node; nextnode (node)) if ((vl_data = getdata (node)) != NULL) if (IPV4_ADDR_SAME (&vl_data->vl_area_id, &area->area_id)) c++; return c;}struct crypt_key *ospf_crypt_key_new (){ struct crypt_key *ck; ck = XMALLOC (MTYPE_OSPF_CRYPT_KEY, sizeof (struct crypt_key)); memset (ck, 0, sizeof (struct crypt_key)); return ck;}voidospf_crypt_key_add (list crypt, struct crypt_key *ck){ listnode_add (crypt, ck);}struct crypt_key *ospf_crypt_key_lookup (list auth_crypt, u_char key_id){ listnode node; struct crypt_key *ck; for (node = listhead (auth_crypt); node; nextnode (node)) { ck = getdata (node); if (ck->key_id == key_id) return ck; } return NULL;}intospf_crypt_key_delete (list auth_crypt, u_char key_id){ listnode node; struct crypt_key *ck; for (node = listhead (auth_crypt); node; nextnode (node)) { ck = getdata (node); if (ck->key_id == key_id) { listnode_delete (auth_crypt, ck); return 1; } } return 0;}voidospf_if_init (){ /* Initialize Zebra interface data structure. */ if_init (); om->iflist = iflist; if_add_hook (IF_NEW_HOOK, ospf_if_new_hook); if_add_hook (IF_DELETE_HOOK, ospf_if_delete_hook);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -