if_bridge.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. /* $OpenBSD: if_bridge.h,v 1.44 2015/07/02 09:40:02 mpi Exp $ */
  2. /*
  3. * Copyright (c) 1999, 2000 Jason L. Wright (jason@thought.net)
  4. * Copyright (c) 2006 Andrew Thompson (thompsa@FreeBSD.org)
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  17. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
  20. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  22. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  23. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  24. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  25. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  26. * POSSIBILITY OF SUCH DAMAGE.
  27. *
  28. * Effort sponsored in part by the Defense Advanced Research Projects
  29. * Agency (DARPA) and Air Force Research Laboratory, Air Force
  30. * Materiel Command, USAF, under agreement number F30602-01-2-0537.
  31. *
  32. */
  33. #ifndef _NET_IF_BRIDGE_H_
  34. #define _NET_IF_BRIDGE_H_
  35. #include <sys/timeout.h>
  36. #include <net/pfvar.h>
  37. /*
  38. * Bridge control request: add/delete member interfaces.
  39. */
  40. struct ifbreq {
  41. char ifbr_name[IFNAMSIZ]; /* bridge ifs name */
  42. char ifbr_ifsname[IFNAMSIZ]; /* member ifs name */
  43. u_int32_t ifbr_ifsflags; /* member ifs flags */
  44. u_int8_t ifbr_portno; /* member port number */
  45. u_int8_t ifbr_state; /* member stp state */
  46. u_int8_t ifbr_priority; /* member stp priority */
  47. u_int32_t ifbr_path_cost; /* member stp path cost */
  48. u_int32_t ifbr_stpflags; /* member stp flags */
  49. u_int8_t ifbr_proto; /* member stp protocol */
  50. u_int8_t ifbr_role; /* member stp role */
  51. u_int32_t ifbr_fwd_trans; /* member stp fwd transitions */
  52. u_int64_t ifbr_desg_bridge; /* member stp designated bridge */
  53. u_int32_t ifbr_desg_port; /* member stp designated port */
  54. u_int64_t ifbr_root_bridge; /* member stp root bridge */
  55. u_int32_t ifbr_root_cost; /* member stp root cost */
  56. u_int32_t ifbr_root_port; /* member stp root port */
  57. };
  58. /* SIOCBRDGIFFLGS, SIOCBRDGIFFLGS */
  59. #define IFBIF_LEARNING 0x0001 /* ifs can learn */
  60. #define IFBIF_DISCOVER 0x0002 /* ifs sends packets w/unknown dest */
  61. #define IFBIF_BLOCKNONIP 0x0004 /* ifs blocks non-IP/ARP in/out */
  62. #define IFBIF_STP 0x0008 /* ifs participates in spanning tree */
  63. #define IFBIF_BSTP_EDGE 0x0010 /* member stp edge port */
  64. #define IFBIF_BSTP_AUTOEDGE 0x0020 /* member stp autoedge enabled */
  65. #define IFBIF_BSTP_PTP 0x0040 /* member stp ptp */
  66. #define IFBIF_BSTP_AUTOPTP 0x0080 /* member stp autoptp enabled */
  67. #define IFBIF_SPAN 0x0100 /* ifs is a span port (ro) */
  68. #define IFBIF_RO_MASK 0xff00 /* read only bits */
  69. /* SIOCBRDGFLUSH */
  70. #define IFBF_FLUSHDYN 0x0 /* flush dynamic addresses only */
  71. #define IFBF_FLUSHALL 0x1 /* flush all addresses from cache */
  72. /* port states */
  73. #define BSTP_IFSTATE_DISABLED 0
  74. #define BSTP_IFSTATE_LISTENING 1
  75. #define BSTP_IFSTATE_LEARNING 2
  76. #define BSTP_IFSTATE_FORWARDING 3
  77. #define BSTP_IFSTATE_BLOCKING 4
  78. #define BSTP_IFSTATE_DISCARDING 5
  79. #define BSTP_TCSTATE_ACTIVE 1
  80. #define BSTP_TCSTATE_DETECTED 2
  81. #define BSTP_TCSTATE_INACTIVE 3
  82. #define BSTP_TCSTATE_LEARNING 4
  83. #define BSTP_TCSTATE_PROPAG 5
  84. #define BSTP_TCSTATE_ACK 6
  85. #define BSTP_TCSTATE_TC 7
  86. #define BSTP_TCSTATE_TCN 8
  87. #define BSTP_ROLE_DISABLED 0
  88. #define BSTP_ROLE_ROOT 1
  89. #define BSTP_ROLE_DESIGNATED 2
  90. #define BSTP_ROLE_ALTERNATE 3
  91. #define BSTP_ROLE_BACKUP 4
  92. /*
  93. * Interface list structure
  94. */
  95. struct ifbifconf {
  96. char ifbic_name[IFNAMSIZ]; /* bridge ifs name */
  97. u_int32_t ifbic_len; /* buffer size */
  98. union {
  99. caddr_t ifbicu_buf;
  100. struct ifbreq *ifbicu_req;
  101. } ifbic_ifbicu;
  102. #define ifbic_buf ifbic_ifbicu.ifbicu_buf
  103. #define ifbic_req ifbic_ifbicu.ifbicu_req
  104. };
  105. /*
  106. * Bridge address request
  107. */
  108. struct ifbareq {
  109. char ifba_name[IFNAMSIZ]; /* bridge name */
  110. char ifba_ifsname[IFNAMSIZ]; /* destination ifs */
  111. u_int8_t ifba_age; /* address age */
  112. u_int8_t ifba_flags; /* address flags */
  113. struct ether_addr ifba_dst; /* destination addr */
  114. struct sockaddr_storage ifba_dstsa; /* tunnel endpoint */
  115. };
  116. #define IFBAF_TYPEMASK 0x03 /* address type mask */
  117. #define IFBAF_DYNAMIC 0x00 /* dynamically learned */
  118. #define IFBAF_STATIC 0x01 /* static address */
  119. struct ifbaconf {
  120. char ifbac_name[IFNAMSIZ]; /* bridge ifs name */
  121. u_int32_t ifbac_len; /* buffer size */
  122. union {
  123. caddr_t ifbacu_buf; /* buffer */
  124. struct ifbareq *ifbacu_req; /* request pointer */
  125. } ifbac_ifbacu;
  126. #define ifbac_buf ifbac_ifbacu.ifbacu_buf
  127. #define ifbac_req ifbac_ifbacu.ifbacu_req
  128. };
  129. struct ifbrparam {
  130. char ifbrp_name[IFNAMSIZ];
  131. union {
  132. u_int32_t ifbrpu_csize; /* cache size */
  133. int ifbrpu_ctime; /* cache time (sec) */
  134. u_int16_t ifbrpu_prio; /* bridge priority */
  135. u_int8_t ifbrpu_hellotime; /* hello time (sec) */
  136. u_int8_t ifbrpu_fwddelay; /* fwd delay (sec) */
  137. u_int8_t ifbrpu_maxage; /* max age (sec) */
  138. u_int8_t ifbrpu_proto; /* bridge protocol */
  139. u_int8_t ifbrpu_txhc; /* bpdu tx holdcount */
  140. } ifbrp_ifbrpu;
  141. };
  142. #define ifbrp_csize ifbrp_ifbrpu.ifbrpu_csize
  143. #define ifbrp_ctime ifbrp_ifbrpu.ifbrpu_ctime
  144. #define ifbrp_prio ifbrp_ifbrpu.ifbrpu_prio
  145. #define ifbrp_proto ifbrp_ifbrpu.ifbrpu_proto
  146. #define ifbrp_txhc ifbrp_ifbrpu.ifbrpu_txhc
  147. #define ifbrp_hellotime ifbrp_ifbrpu.ifbrpu_hellotime
  148. #define ifbrp_fwddelay ifbrp_ifbrpu.ifbrpu_fwddelay
  149. #define ifbrp_maxage ifbrp_ifbrpu.ifbrpu_maxage
  150. /* Protocol versions */
  151. #define BSTP_PROTO_ID 0x00
  152. #define BSTP_PROTO_STP 0x00
  153. #define BSTP_PROTO_RSTP 0x02
  154. #define BSTP_PROTO_MAX BSTP_PROTO_RSTP
  155. /*
  156. * Bridge current operational parameters structure.
  157. */
  158. struct ifbropreq {
  159. char ifbop_name[IFNAMSIZ];
  160. u_int8_t ifbop_holdcount;
  161. u_int8_t ifbop_maxage;
  162. u_int8_t ifbop_hellotime;
  163. u_int8_t ifbop_fwddelay;
  164. u_int8_t ifbop_protocol;
  165. u_int16_t ifbop_priority;
  166. u_int64_t ifbop_root_bridge;
  167. u_int16_t ifbop_root_port;
  168. u_int32_t ifbop_root_path_cost;
  169. u_int64_t ifbop_desg_bridge;
  170. struct timeval ifbop_last_tc_time;
  171. };
  172. /*
  173. * Bridge mac rules
  174. */
  175. struct ifbrlreq {
  176. char ifbr_name[IFNAMSIZ]; /* bridge ifs name */
  177. char ifbr_ifsname[IFNAMSIZ]; /* member ifs name */
  178. u_int8_t ifbr_action; /* disposition */
  179. u_int8_t ifbr_flags; /* flags */
  180. struct ether_addr ifbr_src; /* source mac */
  181. struct ether_addr ifbr_dst; /* destination mac */
  182. char ifbr_tagname[PF_TAG_NAME_SIZE]; /* pf tagname */
  183. };
  184. #define BRL_ACTION_BLOCK 0x01 /* block frame */
  185. #define BRL_ACTION_PASS 0x02 /* pass frame */
  186. #define BRL_FLAG_IN 0x08 /* input rule */
  187. #define BRL_FLAG_OUT 0x04 /* output rule */
  188. #define BRL_FLAG_SRCVALID 0x02 /* src valid */
  189. #define BRL_FLAG_DSTVALID 0x01 /* dst valid */
  190. struct ifbrlconf {
  191. char ifbrl_name[IFNAMSIZ]; /* bridge ifs name */
  192. char ifbrl_ifsname[IFNAMSIZ];/* member ifs name */
  193. u_int32_t ifbrl_len; /* buffer size */
  194. union {
  195. caddr_t ifbrlu_buf;
  196. struct ifbrlreq *ifbrlu_req;
  197. } ifbrl_ifbrlu;
  198. #define ifbrl_buf ifbrl_ifbrlu.ifbrlu_buf
  199. #define ifbrl_req ifbrl_ifbrlu.ifbrlu_req
  200. };
  201. #ifdef _KERNEL
  202. /* STP port flags */
  203. #define BSTP_PORT_CANMIGRATE 0x0001
  204. #define BSTP_PORT_NEWINFO 0x0002
  205. #define BSTP_PORT_DISPUTED 0x0004
  206. #define BSTP_PORT_ADMCOST 0x0008
  207. #define BSTP_PORT_AUTOEDGE 0x0010
  208. #define BSTP_PORT_AUTOPTP 0x0020
  209. /* BPDU priority */
  210. #define BSTP_PDU_SUPERIOR 1
  211. #define BSTP_PDU_REPEATED 2
  212. #define BSTP_PDU_INFERIOR 3
  213. #define BSTP_PDU_INFERIORALT 4
  214. #define BSTP_PDU_OTHER 5
  215. /* BPDU flags */
  216. #define BSTP_PDU_PRMASK 0x0c /* Port Role */
  217. #define BSTP_PDU_PRSHIFT 2 /* Port Role offset */
  218. #define BSTP_PDU_F_UNKN 0x00 /* Unknown port (00) */
  219. #define BSTP_PDU_F_ALT 0x01 /* Alt/Backup port (01) */
  220. #define BSTP_PDU_F_ROOT 0x02 /* Root port (10) */
  221. #define BSTP_PDU_F_DESG 0x03 /* Designated port (11) */
  222. #define BSTP_PDU_STPMASK 0x81 /* strip unused STP flags */
  223. #define BSTP_PDU_RSTPMASK 0x7f /* strip unused RSTP flags */
  224. #define BSTP_PDU_F_TC 0x01 /* Topology change */
  225. #define BSTP_PDU_F_P 0x02 /* Proposal flag */
  226. #define BSTP_PDU_F_L 0x10 /* Learning flag */
  227. #define BSTP_PDU_F_F 0x20 /* Forwarding flag */
  228. #define BSTP_PDU_F_A 0x40 /* Agreement flag */
  229. #define BSTP_PDU_F_TCA 0x80 /* Topology change ack */
  230. /*
  231. * Bridge filtering rules
  232. */
  233. SIMPLEQ_HEAD(brl_head, brl_node);
  234. struct brl_node {
  235. SIMPLEQ_ENTRY(brl_node) brl_next; /* next rule */
  236. struct ether_addr brl_src; /* source mac address */
  237. struct ether_addr brl_dst; /* destination mac address */
  238. u_int16_t brl_tag; /* pf tag ID */
  239. u_int8_t brl_action; /* what to do with match */
  240. u_int8_t brl_flags; /* comparision flags */
  241. };
  242. struct bstp_timer {
  243. u_int16_t active;
  244. u_int16_t value;
  245. u_int32_t latched;
  246. };
  247. struct bstp_pri_vector {
  248. u_int64_t pv_root_id;
  249. u_int32_t pv_cost;
  250. u_int64_t pv_dbridge_id;
  251. u_int16_t pv_dport_id;
  252. u_int16_t pv_port_id;
  253. };
  254. struct bstp_config_unit {
  255. struct bstp_pri_vector cu_pv;
  256. u_int16_t cu_message_age;
  257. u_int16_t cu_max_age;
  258. u_int16_t cu_forward_delay;
  259. u_int16_t cu_hello_time;
  260. u_int8_t cu_message_type;
  261. u_int8_t cu_topology_change_ack;
  262. u_int8_t cu_topology_change;
  263. u_int8_t cu_proposal;
  264. u_int8_t cu_agree;
  265. u_int8_t cu_learning;
  266. u_int8_t cu_forwarding;
  267. u_int8_t cu_role;
  268. };
  269. struct bstp_tcn_unit {
  270. u_int8_t tu_message_type;
  271. };
  272. struct bstp_port {
  273. LIST_ENTRY(bstp_port) bp_next;
  274. struct ifnet *bp_ifp; /* parent if */
  275. struct bstp_state *bp_bs;
  276. void *bp_lhcookie; /* if linkstate hook */
  277. u_int8_t bp_active;
  278. u_int8_t bp_protover;
  279. u_int32_t bp_flags;
  280. u_int32_t bp_path_cost;
  281. u_int16_t bp_port_msg_age;
  282. u_int16_t bp_port_max_age;
  283. u_int16_t bp_port_fdelay;
  284. u_int16_t bp_port_htime;
  285. u_int16_t bp_desg_msg_age;
  286. u_int16_t bp_desg_max_age;
  287. u_int16_t bp_desg_fdelay;
  288. u_int16_t bp_desg_htime;
  289. struct bstp_timer bp_edge_delay_timer;
  290. struct bstp_timer bp_forward_delay_timer;
  291. struct bstp_timer bp_hello_timer;
  292. struct bstp_timer bp_message_age_timer;
  293. struct bstp_timer bp_migrate_delay_timer;
  294. struct bstp_timer bp_recent_backup_timer;
  295. struct bstp_timer bp_recent_root_timer;
  296. struct bstp_timer bp_tc_timer;
  297. struct bstp_config_unit bp_msg_cu;
  298. struct bstp_pri_vector bp_desg_pv;
  299. struct bstp_pri_vector bp_port_pv;
  300. u_int16_t bp_port_id;
  301. u_int8_t bp_state;
  302. u_int8_t bp_tcstate;
  303. u_int8_t bp_role;
  304. u_int8_t bp_infois;
  305. u_int8_t bp_tc_ack;
  306. u_int8_t bp_tc_prop;
  307. u_int8_t bp_fdbflush;
  308. u_int8_t bp_priority;
  309. u_int8_t bp_ptp_link;
  310. u_int8_t bp_agree;
  311. u_int8_t bp_agreed;
  312. u_int8_t bp_sync;
  313. u_int8_t bp_synced;
  314. u_int8_t bp_proposing;
  315. u_int8_t bp_proposed;
  316. u_int8_t bp_operedge;
  317. u_int8_t bp_reroot;
  318. u_int8_t bp_rcvdtc;
  319. u_int8_t bp_rcvdtca;
  320. u_int8_t bp_rcvdtcn;
  321. u_int32_t bp_forward_transitions;
  322. u_int8_t bp_txcount;
  323. };
  324. /*
  325. * Software state for each bridge STP.
  326. */
  327. struct bstp_state {
  328. struct ifnet *bs_ifp;
  329. struct bstp_pri_vector bs_bridge_pv;
  330. struct bstp_pri_vector bs_root_pv;
  331. struct bstp_port *bs_root_port;
  332. u_int8_t bs_protover;
  333. u_int16_t bs_migration_delay;
  334. u_int16_t bs_edge_delay;
  335. u_int16_t bs_bridge_max_age;
  336. u_int16_t bs_bridge_fdelay;
  337. u_int16_t bs_bridge_htime;
  338. u_int16_t bs_root_msg_age;
  339. u_int16_t bs_root_max_age;
  340. u_int16_t bs_root_fdelay;
  341. u_int16_t bs_root_htime;
  342. u_int16_t bs_hold_time;
  343. u_int16_t bs_bridge_priority;
  344. u_int8_t bs_txholdcount;
  345. u_int8_t bs_allsynced;
  346. struct timeout bs_bstptimeout; /* stp timeout */
  347. struct bstp_timer bs_link_timer;
  348. struct timeval bs_last_tc_time;
  349. LIST_HEAD(, bstp_port) bs_bplist;
  350. };
  351. #define bs_ifflags bs_ifp->if_flags
  352. /*
  353. * Bridge interface list
  354. */
  355. struct bridge_iflist {
  356. TAILQ_ENTRY(bridge_iflist) next; /* next in list */
  357. struct bridge_softc *bridge_sc;
  358. struct bstp_port *bif_stp; /* STP port state */
  359. struct brl_head bif_brlin; /* input rules */
  360. struct brl_head bif_brlout; /* output rules */
  361. struct ifnet *ifp; /* member interface */
  362. u_int32_t bif_flags; /* member flags */
  363. };
  364. #define bif_state bif_stp->bp_state
  365. #define SAME_BRIDGE(_bp1, _bp2) \
  366. (_bp1 && _bp2 && ((struct bridge_iflist *)_bp1)->bridge_sc == \
  367. ((struct bridge_iflist *)_bp2)->bridge_sc)
  368. /*
  369. * Bridge route node
  370. */
  371. struct bridge_rtnode {
  372. LIST_ENTRY(bridge_rtnode) brt_next; /* next in list */
  373. struct ifnet *brt_if; /* destination ifs */
  374. u_int8_t brt_flags; /* address flags */
  375. u_int8_t brt_age; /* age counter */
  376. struct ether_addr brt_addr; /* dst addr */
  377. union pfsockaddr_union brt_tunnel; /* tunnel endpoint */
  378. };
  379. #ifndef BRIDGE_RTABLE_SIZE
  380. #define BRIDGE_RTABLE_SIZE 1024
  381. #endif
  382. #define BRIDGE_RTABLE_MASK (BRIDGE_RTABLE_SIZE - 1)
  383. /*
  384. * Software state for each bridge
  385. */
  386. struct bridge_softc {
  387. struct ifnet sc_if; /* the interface */
  388. LIST_ENTRY(bridge_softc) sc_list; /* all bridges */
  389. struct bridge_iflist *sc_root_port;
  390. u_int32_t sc_brtmax; /* max # addresses */
  391. u_int32_t sc_brtcnt; /* current # addrs */
  392. int sc_brttimeout; /* timeout ticks */
  393. u_int64_t sc_hashkey[2]; /* siphash key */
  394. struct timeout sc_brtimeout; /* timeout state */
  395. struct bstp_state *sc_stp; /* stp state */
  396. TAILQ_HEAD(, bridge_iflist) sc_iflist; /* interface list */
  397. TAILQ_HEAD(, bridge_iflist) sc_spanlist; /* span ports */
  398. LIST_HEAD(, bridge_rtnode) sc_rts[BRIDGE_RTABLE_SIZE]; /* hash table */
  399. };
  400. extern const u_int8_t bstp_etheraddr[];
  401. void bridge_ifdetach(struct ifnet *);
  402. struct mbuf *bridge_input(struct ifnet *, struct mbuf *);
  403. int bridge_output(struct ifnet *, struct mbuf *, struct sockaddr *,
  404. struct rtentry *);
  405. void bridge_update(struct ifnet *, struct ether_addr *, int);
  406. void bridge_rtdelete(struct bridge_softc *, struct ifnet *, int);
  407. void bridge_rtagenode(struct ifnet *, int);
  408. struct sockaddr *bridge_tunnel(struct mbuf *);
  409. struct sockaddr *bridge_tunneltag(struct mbuf *, int);
  410. void bridge_tunneluntag(struct mbuf *);
  411. struct bstp_state *bstp_create(struct ifnet *);
  412. void bstp_destroy(struct bstp_state *);
  413. void bstp_initialization(struct bstp_state *);
  414. void bstp_stop(struct bstp_state *);
  415. int bstp_ioctl(struct ifnet *, u_long, caddr_t);
  416. struct bstp_port *bstp_add(struct bstp_state *, struct ifnet *);
  417. void bstp_delete(struct bstp_port *);
  418. struct mbuf *bstp_input(struct bstp_state *, struct bstp_port *,
  419. struct ether_header *, struct mbuf *);
  420. void bstp_ifstate(void *);
  421. u_int8_t bstp_getstate(struct bstp_state *, struct bstp_port *);
  422. void bstp_ifsflags(struct bstp_port *, u_int);
  423. #endif /* _KERNEL */
  424. #endif /* _NET_IF_BRIDGE_H_ */