#ifndef __BRIDGESTP_H__
#define __BRIDGESTP_H__
#include <sys/queue.h>
#include <kern/locks.h>
#define BSTP_IFSTATE_DISABLED 0
#define BSTP_IFSTATE_LISTENING 1
#define BSTP_IFSTATE_LEARNING 2
#define BSTP_IFSTATE_FORWARDING 3
#define BSTP_IFSTATE_BLOCKING 4
#define BSTP_IFSTATE_DISCARDING 5
#define BSTP_TCSTATE_ACTIVE 1
#define BSTP_TCSTATE_DETECTED 2
#define BSTP_TCSTATE_INACTIVE 3
#define BSTP_TCSTATE_LEARNING 4
#define BSTP_TCSTATE_PROPAG 5
#define BSTP_TCSTATE_ACK 6
#define BSTP_TCSTATE_TC 7
#define BSTP_TCSTATE_TCN 8
#define BSTP_ROLE_DISABLED 0
#define BSTP_ROLE_ROOT 1
#define BSTP_ROLE_DESIGNATED 2
#define BSTP_ROLE_ALTERNATE 3
#define BSTP_ROLE_BACKUP 4
#ifdef XNU_KERNEL_PRIVATE
#define BSTP_PORT_CANMIGRATE 0x0001
#define BSTP_PORT_NEWINFO 0x0002
#define BSTP_PORT_DISPUTED 0x0004
#define BSTP_PORT_ADMCOST 0x0008
#define BSTP_PORT_AUTOEDGE 0x0010
#define BSTP_PORT_AUTOPTP 0x0020
#define BSTP_PORT_ADMEDGE 0x0040
#define BSTP_PORT_PNDCOST 0x0080
#define BSTP_PDU_SUPERIOR 1
#define BSTP_PDU_REPEATED 2
#define BSTP_PDU_INFERIOR 3
#define BSTP_PDU_INFERIORALT 4
#define BSTP_PDU_OTHER 5
#define BSTP_PDU_PRMASK 0x0c
#define BSTP_PDU_PRSHIFT 2
#define BSTP_PDU_F_UNKN 0x00
#define BSTP_PDU_F_ALT 0x01
#define BSTP_PDU_F_ROOT 0x02
#define BSTP_PDU_F_DESG 0x03
#define BSTP_PDU_STPMASK 0x81
#define BSTP_PDU_RSTPMASK 0x7f
#define BSTP_PDU_F_TC 0x01
#define BSTP_PDU_F_P 0x02
#define BSTP_PDU_F_L 0x10
#define BSTP_PDU_F_F 0x20
#define BSTP_PDU_F_A 0x40
#define BSTP_PDU_F_TCA 0x80
#define BSTP_DEFAULT_MAX_AGE (20 * 256)
#define BSTP_DEFAULT_HELLO_TIME (2 * 256)
#define BSTP_DEFAULT_FORWARD_DELAY (15 * 256)
#define BSTP_DEFAULT_HOLD_TIME (1 * 256)
#define BSTP_DEFAULT_MIGRATE_DELAY (3 * 256)
#define BSTP_DEFAULT_HOLD_COUNT 6
#define BSTP_DEFAULT_BRIDGE_PRIORITY 0x8000
#define BSTP_DEFAULT_PORT_PRIORITY 0x80
#define BSTP_DEFAULT_PATH_COST 55
#define BSTP_MIN_HELLO_TIME (1 * 256)
#define BSTP_MIN_MAX_AGE (6 * 256)
#define BSTP_MIN_FORWARD_DELAY (4 * 256)
#define BSTP_MIN_HOLD_COUNT 1
#define BSTP_MAX_HELLO_TIME (2 * 256)
#define BSTP_MAX_MAX_AGE (40 * 256)
#define BSTP_MAX_FORWARD_DELAY (30 * 256)
#define BSTP_MAX_HOLD_COUNT 10
#define BSTP_MAX_PRIORITY 61440
#define BSTP_MAX_PORT_PRIORITY 240
#define BSTP_MAX_PATH_COST 200000000
#define BSTP_MSGTYPE_CFG 0x00
#define BSTP_MSGTYPE_RSTP 0x02
#define BSTP_MSGTYPE_TCN 0x80
#define BSTP_PROTO_ID 0x00
#define BSTP_PROTO_STP 0x00
#define BSTP_PROTO_RSTP 0x02
#define BSTP_PROTO_MAX BSTP_PROTO_RSTP
#define BSTP_INFO_RECIEVED 1
#define BSTP_INFO_RECEIVED 1
#define BSTP_INFO_MINE 2
#define BSTP_INFO_AGED 3
#define BSTP_INFO_DISABLED 4
#define BSTP_MESSAGE_AGE_INCR (1 * 256)
#define BSTP_TICK_VAL (1 * 256)
#define BSTP_LINK_TIMER (BSTP_TICK_VAL * 15)
typedef void (*bstp_state_cb_t)(struct ifnet *, int);
typedef void (*bstp_rtage_cb_t)(struct ifnet *, int);
struct bstp_cb_ops {
bstp_state_cb_t bcb_state;
bstp_rtage_cb_t bcb_rtage;
};
struct bstp_cbpdu {
uint8_t cbu_dsap;
uint8_t cbu_ssap;
uint8_t cbu_ctl;
uint16_t cbu_protoid;
uint8_t cbu_protover;
uint8_t cbu_bpdutype;
uint8_t cbu_flags;
uint16_t cbu_rootpri;
uint8_t cbu_rootaddr[6];
uint32_t cbu_rootpathcost;
uint16_t cbu_bridgepri;
uint8_t cbu_bridgeaddr[6];
uint16_t cbu_portid;
uint16_t cbu_messageage;
uint16_t cbu_maxage;
uint16_t cbu_hellotime;
uint16_t cbu_forwarddelay;
uint8_t cbu_versionlen;
} __attribute__((__packed__));
#define BSTP_BPDU_STP_LEN (3 + 35)
#define BSTP_BPDU_RSTP_LEN (3 + 36)
struct bstp_tbpdu {
uint8_t tbu_dsap;
uint8_t tbu_ssap;
uint8_t tbu_ctl;
uint16_t tbu_protoid;
uint8_t tbu_protover;
uint8_t tbu_bpdutype;
} __attribute__((__packed__));
typedef void bstp_task_func_t(void *context, int count);
struct bstp_task {
TAILQ_ENTRY(bstp_task) bt_next;
int bt_count;
bstp_task_func_t *bt_func;
void *bt_context;
};
struct bstp_timer {
int active;
int latched;
int value;
};
struct bstp_pri_vector {
uint64_t pv_root_id;
uint32_t pv_cost;
uint64_t pv_dbridge_id;
uint16_t pv_dport_id;
uint16_t pv_port_id;
};
struct bstp_config_unit {
struct bstp_pri_vector cu_pv;
uint16_t cu_message_age;
uint16_t cu_max_age;
uint16_t cu_forward_delay;
uint16_t cu_hello_time;
uint8_t cu_message_type;
uint8_t cu_topology_change_ack;
uint8_t cu_topology_change;
uint8_t cu_proposal;
uint8_t cu_agree;
uint8_t cu_learning;
uint8_t cu_forwarding;
uint8_t cu_role;
};
struct bstp_tcn_unit {
uint8_t tu_message_type;
};
struct bstp_port {
LIST_ENTRY(bstp_port) bp_next;
struct ifnet *bp_ifp;
struct bstp_state *bp_bs;
uint8_t bp_active;
uint8_t bp_protover;
uint32_t bp_flags;
uint32_t bp_path_cost;
uint16_t bp_port_msg_age;
uint16_t bp_port_max_age;
uint16_t bp_port_fdelay;
uint16_t bp_port_htime;
uint16_t bp_desg_msg_age;
uint16_t bp_desg_max_age;
uint16_t bp_desg_fdelay;
uint16_t bp_desg_htime;
struct bstp_timer bp_edge_delay_timer;
struct bstp_timer bp_forward_delay_timer;
struct bstp_timer bp_hello_timer;
struct bstp_timer bp_message_age_timer;
struct bstp_timer bp_migrate_delay_timer;
struct bstp_timer bp_recent_backup_timer;
struct bstp_timer bp_recent_root_timer;
struct bstp_timer bp_tc_timer;
struct bstp_config_unit bp_msg_cu;
struct bstp_pri_vector bp_desg_pv;
struct bstp_pri_vector bp_port_pv;
uint16_t bp_port_id;
uint8_t bp_state;
uint8_t bp_tcstate;
uint8_t bp_role;
uint8_t bp_infois;
uint8_t bp_tc_ack;
uint8_t bp_tc_prop;
uint8_t bp_fdbflush;
uint8_t bp_priority;
uint8_t bp_ptp_link;
uint8_t bp_agree;
uint8_t bp_agreed;
uint8_t bp_sync;
uint8_t bp_synced;
uint8_t bp_proposing;
uint8_t bp_proposed;
uint8_t bp_operedge;
uint8_t bp_reroot;
uint8_t bp_rcvdtc;
uint8_t bp_rcvdtca;
uint8_t bp_rcvdtcn;
uint32_t bp_forward_transitions;
uint8_t bp_txcount;
struct bstp_task bp_statetask;
struct bstp_task bp_rtagetask;
uint32_t bp_if_link_state;
};
#define LINK_STATE_UNKNOWN 0
#define LINK_STATE_DOWN 1
#define LINK_STATE_UP 2
struct bstp_state {
LIST_ENTRY(bstp_state) bs_list;
uint8_t bs_running;
lck_mtx_t *bs_mtx;
struct bstp_pri_vector bs_bridge_pv;
struct bstp_pri_vector bs_root_pv;
struct bstp_port *bs_root_port;
uint8_t bs_protover;
uint16_t bs_migration_delay;
uint16_t bs_edge_delay;
uint16_t bs_bridge_max_age;
uint16_t bs_bridge_fdelay;
uint16_t bs_bridge_htime;
uint16_t bs_root_msg_age;
uint16_t bs_root_max_age;
uint16_t bs_root_fdelay;
uint16_t bs_root_htime;
uint16_t bs_hold_time;
uint16_t bs_bridge_priority;
uint8_t bs_txholdcount;
uint8_t bs_allsynced;
struct bstp_timer bs_link_timer;
struct timeval bs_last_tc_time;
LIST_HEAD(, bstp_port) bs_bplist;
bstp_state_cb_t bs_state_cb;
bstp_rtage_cb_t bs_rtage_cb;
};
void bstp_attach(struct bstp_state *, struct bstp_cb_ops *);
void bstp_detach(struct bstp_state *);
void bstp_init(struct bstp_state *);
void bstp_stop(struct bstp_state *);
int bstp_create(struct bstp_state *, struct bstp_port *, struct ifnet *);
int bstp_enable(struct bstp_port *);
void bstp_disable(struct bstp_port *);
void bstp_destroy(struct bstp_port *);
void bstp_linkstate(struct ifnet *, int);
int bstp_set_htime(struct bstp_state *, int);
int bstp_set_fdelay(struct bstp_state *, int);
int bstp_set_maxage(struct bstp_state *, int);
int bstp_set_holdcount(struct bstp_state *, int);
int bstp_set_protocol(struct bstp_state *, int);
int bstp_set_priority(struct bstp_state *, int);
int bstp_set_port_priority(struct bstp_port *, int);
int bstp_set_path_cost(struct bstp_port *, uint32_t);
int bstp_set_edge(struct bstp_port *, int);
int bstp_set_autoedge(struct bstp_port *, int);
int bstp_set_ptp(struct bstp_port *, int);
int bstp_set_autoptp(struct bstp_port *, int);
struct mbuf *bstp_input(struct bstp_port *, struct ifnet *, struct mbuf *);
void bstp_sys_init(void);
#endif
#endif