br_private.h 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207
  1. /*
  2. * Linux ethernet bridge
  3. *
  4. * Authors:
  5. * Lennert Buytenhek <buytenh@gnu.org>
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version
  10. * 2 of the License, or (at your option) any later version.
  11. */
  12. #ifndef _BR_PRIVATE_H
  13. #define _BR_PRIVATE_H
  14. #include <linux/netdevice.h>
  15. #include <linux/if_bridge.h>
  16. #include <linux/netpoll.h>
  17. #include <linux/u64_stats_sync.h>
  18. #include <net/route.h>
  19. #include <net/ip6_fib.h>
  20. #include <linux/if_vlan.h>
  21. #include <linux/rhashtable.h>
  22. #include <linux/refcount.h>
  23. #define BR_HASH_BITS 8
  24. #define BR_HASH_SIZE (1 << BR_HASH_BITS)
  25. #define BR_HOLD_TIME (1*HZ)
  26. #define BR_PORT_BITS 10
  27. #define BR_MAX_PORTS (1<<BR_PORT_BITS)
  28. #define BR_VERSION "2.3"
  29. /* Control of forwarding link local multicast */
  30. #define BR_GROUPFWD_DEFAULT 0
  31. /* Don't allow forwarding of control protocols like STP, MAC PAUSE and LACP */
  32. enum {
  33. BR_GROUPFWD_STP = BIT(0),
  34. BR_GROUPFWD_MACPAUSE = BIT(1),
  35. BR_GROUPFWD_LACP = BIT(2),
  36. };
  37. #define BR_GROUPFWD_RESTRICTED (BR_GROUPFWD_STP | BR_GROUPFWD_MACPAUSE | \
  38. BR_GROUPFWD_LACP)
  39. /* The Nearest Customer Bridge Group Address, 01-80-C2-00-00-[00,0B,0C,0D,0F] */
  40. #define BR_GROUPFWD_8021AD 0xB801u
  41. /* Path to usermode spanning tree program */
  42. #define BR_STP_PROG "/sbin/bridge-stp"
  43. typedef struct bridge_id bridge_id;
  44. typedef struct mac_addr mac_addr;
  45. typedef __u16 port_id;
  46. struct bridge_id
  47. {
  48. unsigned char prio[2];
  49. unsigned char addr[ETH_ALEN];
  50. };
  51. struct mac_addr
  52. {
  53. unsigned char addr[ETH_ALEN];
  54. };
  55. #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
  56. /* our own querier */
  57. struct bridge_mcast_own_query {
  58. struct timer_list timer;
  59. u32 startup_sent;
  60. };
  61. /* other querier */
  62. struct bridge_mcast_other_query {
  63. struct timer_list timer;
  64. unsigned long delay_time;
  65. };
  66. /* selected querier */
  67. struct bridge_mcast_querier {
  68. struct br_ip addr;
  69. struct net_bridge_port __rcu *port;
  70. };
  71. /* IGMP/MLD statistics */
  72. struct bridge_mcast_stats {
  73. struct br_mcast_stats mstats;
  74. struct u64_stats_sync syncp;
  75. };
  76. #endif
  77. struct br_vlan_stats {
  78. u64 rx_bytes;
  79. u64 rx_packets;
  80. u64 tx_bytes;
  81. u64 tx_packets;
  82. struct u64_stats_sync syncp;
  83. };
  84. struct br_tunnel_info {
  85. __be64 tunnel_id;
  86. struct metadata_dst *tunnel_dst;
  87. };
  88. /**
  89. * struct net_bridge_vlan - per-vlan entry
  90. *
  91. * @vnode: rhashtable member
  92. * @vid: VLAN id
  93. * @flags: bridge vlan flags
  94. * @stats: per-cpu VLAN statistics
  95. * @br: if MASTER flag set, this points to a bridge struct
  96. * @port: if MASTER flag unset, this points to a port struct
  97. * @refcnt: if MASTER flag set, this is bumped for each port referencing it
  98. * @brvlan: if MASTER flag unset, this points to the global per-VLAN context
  99. * for this VLAN entry
  100. * @vlist: sorted list of VLAN entries
  101. * @rcu: used for entry destruction
  102. *
  103. * This structure is shared between the global per-VLAN entries contained in
  104. * the bridge rhashtable and the local per-port per-VLAN entries contained in
  105. * the port's rhashtable. The union entries should be interpreted depending on
  106. * the entry flags that are set.
  107. */
  108. struct net_bridge_vlan {
  109. struct rhash_head vnode;
  110. struct rhash_head tnode;
  111. u16 vid;
  112. u16 flags;
  113. struct br_vlan_stats __percpu *stats;
  114. union {
  115. struct net_bridge *br;
  116. struct net_bridge_port *port;
  117. };
  118. union {
  119. refcount_t refcnt;
  120. struct net_bridge_vlan *brvlan;
  121. };
  122. struct br_tunnel_info tinfo;
  123. struct list_head vlist;
  124. struct rcu_head rcu;
  125. };
  126. /**
  127. * struct net_bridge_vlan_group
  128. *
  129. * @vlan_hash: VLAN entry rhashtable
  130. * @vlan_list: sorted VLAN entry list
  131. * @num_vlans: number of total VLAN entries
  132. * @pvid: PVID VLAN id
  133. *
  134. * IMPORTANT: Be careful when checking if there're VLAN entries using list
  135. * primitives because the bridge can have entries in its list which
  136. * are just for global context but not for filtering, i.e. they have
  137. * the master flag set but not the brentry flag. If you have to check
  138. * if there're "real" entries in the bridge please test @num_vlans
  139. */
  140. struct net_bridge_vlan_group {
  141. struct rhashtable vlan_hash;
  142. struct rhashtable tunnel_hash;
  143. struct list_head vlan_list;
  144. u16 num_vlans;
  145. u16 pvid;
  146. };
  147. struct net_bridge_fdb_key {
  148. mac_addr addr;
  149. u16 vlan_id;
  150. };
  151. struct net_bridge_fdb_entry {
  152. struct rhash_head rhnode;
  153. struct net_bridge_port *dst;
  154. struct net_bridge_fdb_key key;
  155. struct hlist_node fdb_node;
  156. unsigned char is_local:1,
  157. is_static:1,
  158. added_by_user:1,
  159. added_by_external_learn:1,
  160. offloaded:1;
  161. /* write-heavy members should not affect lookups */
  162. unsigned long updated ____cacheline_aligned_in_smp;
  163. unsigned long used;
  164. struct rcu_head rcu;
  165. };
  166. #define MDB_PG_FLAGS_PERMANENT BIT(0)
  167. #define MDB_PG_FLAGS_OFFLOAD BIT(1)
  168. struct net_bridge_port_group {
  169. struct net_bridge_port *port;
  170. struct net_bridge_port_group __rcu *next;
  171. struct hlist_node mglist;
  172. struct rcu_head rcu;
  173. struct timer_list timer;
  174. struct br_ip addr;
  175. unsigned char flags;
  176. unsigned char eth_addr[ETH_ALEN];
  177. };
  178. struct net_bridge_mdb_entry
  179. {
  180. struct hlist_node hlist[2];
  181. struct net_bridge *br;
  182. struct net_bridge_port_group __rcu *ports;
  183. struct rcu_head rcu;
  184. struct timer_list timer;
  185. struct br_ip addr;
  186. bool host_joined;
  187. };
  188. struct net_bridge_mdb_htable
  189. {
  190. struct hlist_head *mhash;
  191. struct rcu_head rcu;
  192. struct net_bridge_mdb_htable *old;
  193. u32 size;
  194. u32 max;
  195. u32 secret;
  196. u32 ver;
  197. };
  198. struct net_bridge_port {
  199. struct net_bridge *br;
  200. struct net_device *dev;
  201. struct list_head list;
  202. unsigned long flags;
  203. #ifdef CONFIG_BRIDGE_VLAN_FILTERING
  204. struct net_bridge_vlan_group __rcu *vlgrp;
  205. #endif
  206. struct net_bridge_port __rcu *backup_port;
  207. /* STP */
  208. u8 priority;
  209. u8 state;
  210. u16 port_no;
  211. unsigned char topology_change_ack;
  212. unsigned char config_pending;
  213. port_id port_id;
  214. port_id designated_port;
  215. bridge_id designated_root;
  216. bridge_id designated_bridge;
  217. u32 path_cost;
  218. u32 designated_cost;
  219. unsigned long designated_age;
  220. struct timer_list forward_delay_timer;
  221. struct timer_list hold_timer;
  222. struct timer_list message_age_timer;
  223. struct kobject kobj;
  224. struct rcu_head rcu;
  225. #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
  226. struct bridge_mcast_own_query ip4_own_query;
  227. #if IS_ENABLED(CONFIG_IPV6)
  228. struct bridge_mcast_own_query ip6_own_query;
  229. #endif /* IS_ENABLED(CONFIG_IPV6) */
  230. unsigned char multicast_router;
  231. struct bridge_mcast_stats __percpu *mcast_stats;
  232. struct timer_list multicast_router_timer;
  233. struct hlist_head mglist;
  234. struct hlist_node rlist;
  235. #endif
  236. #ifdef CONFIG_SYSFS
  237. char sysfs_name[IFNAMSIZ];
  238. #endif
  239. #ifdef CONFIG_NET_POLL_CONTROLLER
  240. struct netpoll *np;
  241. #endif
  242. #ifdef CONFIG_NET_SWITCHDEV
  243. int offload_fwd_mark;
  244. #endif
  245. u16 group_fwd_mask;
  246. u16 backup_redirected_cnt;
  247. };
  248. #define kobj_to_brport(obj) container_of(obj, struct net_bridge_port, kobj)
  249. #define br_auto_port(p) ((p)->flags & BR_AUTO_MASK)
  250. #define br_promisc_port(p) ((p)->flags & BR_PROMISC)
  251. #define br_port_exists(dev) (dev->priv_flags & IFF_BRIDGE_PORT)
  252. static inline struct net_bridge_port *br_port_get_rcu(const struct net_device *dev)
  253. {
  254. return rcu_dereference(dev->rx_handler_data);
  255. }
  256. static inline struct net_bridge_port *br_port_get_rtnl(const struct net_device *dev)
  257. {
  258. return br_port_exists(dev) ?
  259. rtnl_dereference(dev->rx_handler_data) : NULL;
  260. }
  261. static inline struct net_bridge_port *br_port_get_rtnl_rcu(const struct net_device *dev)
  262. {
  263. return br_port_exists(dev) ?
  264. rcu_dereference_rtnl(dev->rx_handler_data) : NULL;
  265. }
  266. struct net_bridge {
  267. spinlock_t lock;
  268. spinlock_t hash_lock;
  269. struct list_head port_list;
  270. struct net_device *dev;
  271. struct pcpu_sw_netstats __percpu *stats;
  272. /* These fields are accessed on each packet */
  273. #ifdef CONFIG_BRIDGE_VLAN_FILTERING
  274. u8 vlan_enabled;
  275. u8 vlan_stats_enabled;
  276. __be16 vlan_proto;
  277. u16 default_pvid;
  278. struct net_bridge_vlan_group __rcu *vlgrp;
  279. #endif
  280. struct rhashtable fdb_hash_tbl;
  281. #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
  282. union {
  283. struct rtable fake_rtable;
  284. struct rt6_info fake_rt6_info;
  285. };
  286. bool nf_call_iptables;
  287. bool nf_call_ip6tables;
  288. bool nf_call_arptables;
  289. #endif
  290. u16 group_fwd_mask;
  291. u16 group_fwd_mask_required;
  292. /* STP */
  293. bridge_id designated_root;
  294. bridge_id bridge_id;
  295. u32 root_path_cost;
  296. unsigned char topology_change;
  297. unsigned char topology_change_detected;
  298. u16 root_port;
  299. unsigned long max_age;
  300. unsigned long hello_time;
  301. unsigned long forward_delay;
  302. unsigned long ageing_time;
  303. unsigned long bridge_max_age;
  304. unsigned long bridge_hello_time;
  305. unsigned long bridge_forward_delay;
  306. unsigned long bridge_ageing_time;
  307. u8 group_addr[ETH_ALEN];
  308. bool group_addr_set;
  309. enum {
  310. BR_NO_STP, /* no spanning tree */
  311. BR_KERNEL_STP, /* old STP in kernel */
  312. BR_USER_STP, /* new RSTP in userspace */
  313. } stp_enabled;
  314. #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
  315. unsigned char multicast_router;
  316. u8 multicast_disabled:1;
  317. u8 multicast_querier:1;
  318. u8 multicast_query_use_ifaddr:1;
  319. u8 has_ipv6_addr:1;
  320. u8 multicast_stats_enabled:1;
  321. u32 hash_elasticity;
  322. u32 hash_max;
  323. u32 multicast_last_member_count;
  324. u32 multicast_startup_query_count;
  325. u8 multicast_igmp_version;
  326. unsigned long multicast_last_member_interval;
  327. unsigned long multicast_membership_interval;
  328. unsigned long multicast_querier_interval;
  329. unsigned long multicast_query_interval;
  330. unsigned long multicast_query_response_interval;
  331. unsigned long multicast_startup_query_interval;
  332. spinlock_t multicast_lock;
  333. struct net_bridge_mdb_htable __rcu *mdb;
  334. struct hlist_head router_list;
  335. struct timer_list multicast_router_timer;
  336. struct bridge_mcast_other_query ip4_other_query;
  337. struct bridge_mcast_own_query ip4_own_query;
  338. struct bridge_mcast_querier ip4_querier;
  339. struct bridge_mcast_stats __percpu *mcast_stats;
  340. #if IS_ENABLED(CONFIG_IPV6)
  341. struct bridge_mcast_other_query ip6_other_query;
  342. struct bridge_mcast_own_query ip6_own_query;
  343. struct bridge_mcast_querier ip6_querier;
  344. u8 multicast_mld_version;
  345. #endif /* IS_ENABLED(CONFIG_IPV6) */
  346. #endif
  347. struct timer_list hello_timer;
  348. struct timer_list tcn_timer;
  349. struct timer_list topology_change_timer;
  350. struct delayed_work gc_work;
  351. struct kobject *ifobj;
  352. u32 auto_cnt;
  353. #ifdef CONFIG_NET_SWITCHDEV
  354. int offload_fwd_mark;
  355. #endif
  356. bool neigh_suppress_enabled;
  357. bool mtu_set_by_user;
  358. struct hlist_head fdb_list;
  359. };
  360. struct br_input_skb_cb {
  361. struct net_device *brdev;
  362. #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
  363. int igmp;
  364. int mrouters_only;
  365. #endif
  366. bool proxyarp_replied;
  367. bool src_port_isolated;
  368. #ifdef CONFIG_BRIDGE_VLAN_FILTERING
  369. bool vlan_filtered;
  370. #endif
  371. #ifdef CONFIG_NET_SWITCHDEV
  372. int offload_fwd_mark;
  373. #endif
  374. };
  375. #define BR_INPUT_SKB_CB(__skb) ((struct br_input_skb_cb *)(__skb)->cb)
  376. #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
  377. # define BR_INPUT_SKB_CB_MROUTERS_ONLY(__skb) (BR_INPUT_SKB_CB(__skb)->mrouters_only)
  378. #else
  379. # define BR_INPUT_SKB_CB_MROUTERS_ONLY(__skb) (0)
  380. #endif
  381. #define br_printk(level, br, format, args...) \
  382. printk(level "%s: " format, (br)->dev->name, ##args)
  383. #define br_err(__br, format, args...) \
  384. br_printk(KERN_ERR, __br, format, ##args)
  385. #define br_warn(__br, format, args...) \
  386. br_printk(KERN_WARNING, __br, format, ##args)
  387. #define br_notice(__br, format, args...) \
  388. br_printk(KERN_NOTICE, __br, format, ##args)
  389. #define br_info(__br, format, args...) \
  390. br_printk(KERN_INFO, __br, format, ##args)
  391. #define br_debug(br, format, args...) \
  392. pr_debug("%s: " format, (br)->dev->name, ##args)
  393. /* called under bridge lock */
  394. static inline int br_is_root_bridge(const struct net_bridge *br)
  395. {
  396. return !memcmp(&br->bridge_id, &br->designated_root, 8);
  397. }
  398. /* check if a VLAN entry is global */
  399. static inline bool br_vlan_is_master(const struct net_bridge_vlan *v)
  400. {
  401. return v->flags & BRIDGE_VLAN_INFO_MASTER;
  402. }
  403. /* check if a VLAN entry is used by the bridge */
  404. static inline bool br_vlan_is_brentry(const struct net_bridge_vlan *v)
  405. {
  406. return v->flags & BRIDGE_VLAN_INFO_BRENTRY;
  407. }
  408. /* check if we should use the vlan entry, returns false if it's only context */
  409. static inline bool br_vlan_should_use(const struct net_bridge_vlan *v)
  410. {
  411. if (br_vlan_is_master(v)) {
  412. if (br_vlan_is_brentry(v))
  413. return true;
  414. else
  415. return false;
  416. }
  417. return true;
  418. }
  419. /* br_device.c */
  420. void br_dev_setup(struct net_device *dev);
  421. void br_dev_delete(struct net_device *dev, struct list_head *list);
  422. netdev_tx_t br_dev_xmit(struct sk_buff *skb, struct net_device *dev);
  423. #ifdef CONFIG_NET_POLL_CONTROLLER
  424. static inline void br_netpoll_send_skb(const struct net_bridge_port *p,
  425. struct sk_buff *skb)
  426. {
  427. struct netpoll *np = p->np;
  428. if (np)
  429. netpoll_send_skb(np, skb);
  430. }
  431. int br_netpoll_enable(struct net_bridge_port *p);
  432. void br_netpoll_disable(struct net_bridge_port *p);
  433. #else
  434. static inline void br_netpoll_send_skb(const struct net_bridge_port *p,
  435. struct sk_buff *skb)
  436. {
  437. }
  438. static inline int br_netpoll_enable(struct net_bridge_port *p)
  439. {
  440. return 0;
  441. }
  442. static inline void br_netpoll_disable(struct net_bridge_port *p)
  443. {
  444. }
  445. #endif
  446. /* br_fdb.c */
  447. int br_fdb_init(void);
  448. void br_fdb_fini(void);
  449. int br_fdb_hash_init(struct net_bridge *br);
  450. void br_fdb_hash_fini(struct net_bridge *br);
  451. void br_fdb_flush(struct net_bridge *br);
  452. void br_fdb_find_delete_local(struct net_bridge *br,
  453. const struct net_bridge_port *p,
  454. const unsigned char *addr, u16 vid);
  455. void br_fdb_changeaddr(struct net_bridge_port *p, const unsigned char *newaddr);
  456. void br_fdb_change_mac_address(struct net_bridge *br, const u8 *newaddr);
  457. void br_fdb_cleanup(struct work_struct *work);
  458. void br_fdb_delete_by_port(struct net_bridge *br,
  459. const struct net_bridge_port *p, u16 vid, int do_all);
  460. struct net_bridge_fdb_entry *br_fdb_find_rcu(struct net_bridge *br,
  461. const unsigned char *addr,
  462. __u16 vid);
  463. int br_fdb_test_addr(struct net_device *dev, unsigned char *addr);
  464. int br_fdb_fillbuf(struct net_bridge *br, void *buf, unsigned long count,
  465. unsigned long off);
  466. int br_fdb_insert(struct net_bridge *br, struct net_bridge_port *source,
  467. const unsigned char *addr, u16 vid);
  468. void br_fdb_update(struct net_bridge *br, struct net_bridge_port *source,
  469. const unsigned char *addr, u16 vid, bool added_by_user);
  470. int br_fdb_delete(struct ndmsg *ndm, struct nlattr *tb[],
  471. struct net_device *dev, const unsigned char *addr, u16 vid);
  472. int br_fdb_add(struct ndmsg *nlh, struct nlattr *tb[], struct net_device *dev,
  473. const unsigned char *addr, u16 vid, u16 nlh_flags);
  474. int br_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
  475. struct net_device *dev, struct net_device *fdev, int *idx);
  476. int br_fdb_sync_static(struct net_bridge *br, struct net_bridge_port *p);
  477. void br_fdb_unsync_static(struct net_bridge *br, struct net_bridge_port *p);
  478. int br_fdb_external_learn_add(struct net_bridge *br, struct net_bridge_port *p,
  479. const unsigned char *addr, u16 vid,
  480. bool swdev_notify);
  481. int br_fdb_external_learn_del(struct net_bridge *br, struct net_bridge_port *p,
  482. const unsigned char *addr, u16 vid,
  483. bool swdev_notify);
  484. void br_fdb_offloaded_set(struct net_bridge *br, struct net_bridge_port *p,
  485. const unsigned char *addr, u16 vid);
  486. /* br_forward.c */
  487. enum br_pkt_type {
  488. BR_PKT_UNICAST,
  489. BR_PKT_MULTICAST,
  490. BR_PKT_BROADCAST
  491. };
  492. int br_dev_queue_push_xmit(struct net *net, struct sock *sk, struct sk_buff *skb);
  493. void br_forward(const struct net_bridge_port *to, struct sk_buff *skb,
  494. bool local_rcv, bool local_orig);
  495. int br_forward_finish(struct net *net, struct sock *sk, struct sk_buff *skb);
  496. void br_flood(struct net_bridge *br, struct sk_buff *skb,
  497. enum br_pkt_type pkt_type, bool local_rcv, bool local_orig);
  498. /* return true if both source port and dest port are isolated */
  499. static inline bool br_skb_isolated(const struct net_bridge_port *to,
  500. const struct sk_buff *skb)
  501. {
  502. return BR_INPUT_SKB_CB(skb)->src_port_isolated &&
  503. (to->flags & BR_ISOLATED);
  504. }
  505. /* br_if.c */
  506. void br_port_carrier_check(struct net_bridge_port *p, bool *notified);
  507. int br_add_bridge(struct net *net, const char *name);
  508. int br_del_bridge(struct net *net, const char *name);
  509. int br_add_if(struct net_bridge *br, struct net_device *dev,
  510. struct netlink_ext_ack *extack);
  511. int br_del_if(struct net_bridge *br, struct net_device *dev);
  512. void br_mtu_auto_adjust(struct net_bridge *br);
  513. netdev_features_t br_features_recompute(struct net_bridge *br,
  514. netdev_features_t features);
  515. void br_port_flags_change(struct net_bridge_port *port, unsigned long mask);
  516. void br_manage_promisc(struct net_bridge *br);
  517. int nbp_backup_change(struct net_bridge_port *p, struct net_device *backup_dev);
  518. /* br_input.c */
  519. int br_handle_frame_finish(struct net *net, struct sock *sk, struct sk_buff *skb);
  520. rx_handler_result_t br_handle_frame(struct sk_buff **pskb);
  521. static inline bool br_rx_handler_check_rcu(const struct net_device *dev)
  522. {
  523. return rcu_dereference(dev->rx_handler) == br_handle_frame;
  524. }
  525. static inline bool br_rx_handler_check_rtnl(const struct net_device *dev)
  526. {
  527. return rcu_dereference_rtnl(dev->rx_handler) == br_handle_frame;
  528. }
  529. static inline struct net_bridge_port *br_port_get_check_rcu(const struct net_device *dev)
  530. {
  531. return br_rx_handler_check_rcu(dev) ? br_port_get_rcu(dev) : NULL;
  532. }
  533. static inline struct net_bridge_port *
  534. br_port_get_check_rtnl(const struct net_device *dev)
  535. {
  536. return br_rx_handler_check_rtnl(dev) ? br_port_get_rtnl_rcu(dev) : NULL;
  537. }
  538. /* br_ioctl.c */
  539. int br_dev_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);
  540. int br_ioctl_deviceless_stub(struct net *net, unsigned int cmd,
  541. void __user *arg);
  542. /* br_multicast.c */
  543. #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
  544. extern unsigned int br_mdb_rehash_seq;
  545. int br_multicast_rcv(struct net_bridge *br, struct net_bridge_port *port,
  546. struct sk_buff *skb, u16 vid);
  547. struct net_bridge_mdb_entry *br_mdb_get(struct net_bridge *br,
  548. struct sk_buff *skb, u16 vid);
  549. int br_multicast_add_port(struct net_bridge_port *port);
  550. void br_multicast_del_port(struct net_bridge_port *port);
  551. void br_multicast_enable_port(struct net_bridge_port *port);
  552. void br_multicast_disable_port(struct net_bridge_port *port);
  553. void br_multicast_init(struct net_bridge *br);
  554. void br_multicast_open(struct net_bridge *br);
  555. void br_multicast_stop(struct net_bridge *br);
  556. void br_multicast_dev_del(struct net_bridge *br);
  557. void br_multicast_flood(struct net_bridge_mdb_entry *mdst,
  558. struct sk_buff *skb, bool local_rcv, bool local_orig);
  559. int br_multicast_set_router(struct net_bridge *br, unsigned long val);
  560. int br_multicast_set_port_router(struct net_bridge_port *p, unsigned long val);
  561. int br_multicast_toggle(struct net_bridge *br, unsigned long val);
  562. int br_multicast_set_querier(struct net_bridge *br, unsigned long val);
  563. int br_multicast_set_hash_max(struct net_bridge *br, unsigned long val);
  564. int br_multicast_set_igmp_version(struct net_bridge *br, unsigned long val);
  565. #if IS_ENABLED(CONFIG_IPV6)
  566. int br_multicast_set_mld_version(struct net_bridge *br, unsigned long val);
  567. #endif
  568. struct net_bridge_mdb_entry *
  569. br_mdb_ip_get(struct net_bridge_mdb_htable *mdb, struct br_ip *dst);
  570. struct net_bridge_mdb_entry *
  571. br_multicast_new_group(struct net_bridge *br, struct net_bridge_port *port,
  572. struct br_ip *group);
  573. void br_multicast_free_pg(struct rcu_head *head);
  574. struct net_bridge_port_group *
  575. br_multicast_new_port_group(struct net_bridge_port *port, struct br_ip *group,
  576. struct net_bridge_port_group __rcu *next,
  577. unsigned char flags, const unsigned char *src);
  578. void br_mdb_init(void);
  579. void br_mdb_uninit(void);
  580. void br_mdb_notify(struct net_device *dev, struct net_bridge_port *port,
  581. struct br_ip *group, int type, u8 flags);
  582. void br_rtr_notify(struct net_device *dev, struct net_bridge_port *port,
  583. int type);
  584. void br_multicast_count(struct net_bridge *br, const struct net_bridge_port *p,
  585. const struct sk_buff *skb, u8 type, u8 dir);
  586. int br_multicast_init_stats(struct net_bridge *br);
  587. void br_multicast_uninit_stats(struct net_bridge *br);
  588. void br_multicast_get_stats(const struct net_bridge *br,
  589. const struct net_bridge_port *p,
  590. struct br_mcast_stats *dest);
  591. #define mlock_dereference(X, br) \
  592. rcu_dereference_protected(X, lockdep_is_held(&br->multicast_lock))
  593. static inline bool br_multicast_is_router(struct net_bridge *br)
  594. {
  595. return br->multicast_router == 2 ||
  596. (br->multicast_router == 1 &&
  597. timer_pending(&br->multicast_router_timer));
  598. }
  599. static inline bool
  600. __br_multicast_querier_exists(struct net_bridge *br,
  601. struct bridge_mcast_other_query *querier,
  602. const bool is_ipv6)
  603. {
  604. bool own_querier_enabled;
  605. if (br->multicast_querier) {
  606. if (is_ipv6 && !br->has_ipv6_addr)
  607. own_querier_enabled = false;
  608. else
  609. own_querier_enabled = true;
  610. } else {
  611. own_querier_enabled = false;
  612. }
  613. return time_is_before_jiffies(querier->delay_time) &&
  614. (own_querier_enabled || timer_pending(&querier->timer));
  615. }
  616. static inline bool br_multicast_querier_exists(struct net_bridge *br,
  617. struct ethhdr *eth)
  618. {
  619. switch (eth->h_proto) {
  620. case (htons(ETH_P_IP)):
  621. return __br_multicast_querier_exists(br,
  622. &br->ip4_other_query, false);
  623. #if IS_ENABLED(CONFIG_IPV6)
  624. case (htons(ETH_P_IPV6)):
  625. return __br_multicast_querier_exists(br,
  626. &br->ip6_other_query, true);
  627. #endif
  628. default:
  629. return false;
  630. }
  631. }
  632. static inline int br_multicast_igmp_type(const struct sk_buff *skb)
  633. {
  634. return BR_INPUT_SKB_CB(skb)->igmp;
  635. }
  636. #else
  637. static inline int br_multicast_rcv(struct net_bridge *br,
  638. struct net_bridge_port *port,
  639. struct sk_buff *skb,
  640. u16 vid)
  641. {
  642. return 0;
  643. }
  644. static inline struct net_bridge_mdb_entry *br_mdb_get(struct net_bridge *br,
  645. struct sk_buff *skb, u16 vid)
  646. {
  647. return NULL;
  648. }
  649. static inline int br_multicast_add_port(struct net_bridge_port *port)
  650. {
  651. return 0;
  652. }
  653. static inline void br_multicast_del_port(struct net_bridge_port *port)
  654. {
  655. }
  656. static inline void br_multicast_enable_port(struct net_bridge_port *port)
  657. {
  658. }
  659. static inline void br_multicast_disable_port(struct net_bridge_port *port)
  660. {
  661. }
  662. static inline void br_multicast_init(struct net_bridge *br)
  663. {
  664. }
  665. static inline void br_multicast_open(struct net_bridge *br)
  666. {
  667. }
  668. static inline void br_multicast_stop(struct net_bridge *br)
  669. {
  670. }
  671. static inline void br_multicast_dev_del(struct net_bridge *br)
  672. {
  673. }
  674. static inline void br_multicast_flood(struct net_bridge_mdb_entry *mdst,
  675. struct sk_buff *skb,
  676. bool local_rcv, bool local_orig)
  677. {
  678. }
  679. static inline bool br_multicast_is_router(struct net_bridge *br)
  680. {
  681. return false;
  682. }
  683. static inline bool br_multicast_querier_exists(struct net_bridge *br,
  684. struct ethhdr *eth)
  685. {
  686. return false;
  687. }
  688. static inline void br_mdb_init(void)
  689. {
  690. }
  691. static inline void br_mdb_uninit(void)
  692. {
  693. }
  694. static inline void br_multicast_count(struct net_bridge *br,
  695. const struct net_bridge_port *p,
  696. const struct sk_buff *skb,
  697. u8 type, u8 dir)
  698. {
  699. }
  700. static inline int br_multicast_init_stats(struct net_bridge *br)
  701. {
  702. return 0;
  703. }
  704. static inline void br_multicast_uninit_stats(struct net_bridge *br)
  705. {
  706. }
  707. static inline int br_multicast_igmp_type(const struct sk_buff *skb)
  708. {
  709. return 0;
  710. }
  711. #endif
  712. /* br_vlan.c */
  713. #ifdef CONFIG_BRIDGE_VLAN_FILTERING
  714. bool br_allowed_ingress(const struct net_bridge *br,
  715. struct net_bridge_vlan_group *vg, struct sk_buff *skb,
  716. u16 *vid);
  717. bool br_allowed_egress(struct net_bridge_vlan_group *vg,
  718. const struct sk_buff *skb);
  719. bool br_should_learn(struct net_bridge_port *p, struct sk_buff *skb, u16 *vid);
  720. struct sk_buff *br_handle_vlan(struct net_bridge *br,
  721. const struct net_bridge_port *port,
  722. struct net_bridge_vlan_group *vg,
  723. struct sk_buff *skb);
  724. int br_vlan_add(struct net_bridge *br, u16 vid, u16 flags,
  725. bool *changed);
  726. int br_vlan_delete(struct net_bridge *br, u16 vid);
  727. void br_vlan_flush(struct net_bridge *br);
  728. struct net_bridge_vlan *br_vlan_find(struct net_bridge_vlan_group *vg, u16 vid);
  729. void br_recalculate_fwd_mask(struct net_bridge *br);
  730. int __br_vlan_filter_toggle(struct net_bridge *br, unsigned long val);
  731. int br_vlan_filter_toggle(struct net_bridge *br, unsigned long val);
  732. int __br_vlan_set_proto(struct net_bridge *br, __be16 proto);
  733. int br_vlan_set_proto(struct net_bridge *br, unsigned long val);
  734. int br_vlan_set_stats(struct net_bridge *br, unsigned long val);
  735. int br_vlan_init(struct net_bridge *br);
  736. int br_vlan_set_default_pvid(struct net_bridge *br, unsigned long val);
  737. int __br_vlan_set_default_pvid(struct net_bridge *br, u16 pvid);
  738. int nbp_vlan_add(struct net_bridge_port *port, u16 vid, u16 flags,
  739. bool *changed);
  740. int nbp_vlan_delete(struct net_bridge_port *port, u16 vid);
  741. void nbp_vlan_flush(struct net_bridge_port *port);
  742. int nbp_vlan_init(struct net_bridge_port *port);
  743. int nbp_get_num_vlan_infos(struct net_bridge_port *p, u32 filter_mask);
  744. void br_vlan_get_stats(const struct net_bridge_vlan *v,
  745. struct br_vlan_stats *stats);
  746. static inline struct net_bridge_vlan_group *br_vlan_group(
  747. const struct net_bridge *br)
  748. {
  749. return rtnl_dereference(br->vlgrp);
  750. }
  751. static inline struct net_bridge_vlan_group *nbp_vlan_group(
  752. const struct net_bridge_port *p)
  753. {
  754. return rtnl_dereference(p->vlgrp);
  755. }
  756. static inline struct net_bridge_vlan_group *br_vlan_group_rcu(
  757. const struct net_bridge *br)
  758. {
  759. return rcu_dereference(br->vlgrp);
  760. }
  761. static inline struct net_bridge_vlan_group *nbp_vlan_group_rcu(
  762. const struct net_bridge_port *p)
  763. {
  764. return rcu_dereference(p->vlgrp);
  765. }
  766. /* Since bridge now depends on 8021Q module, but the time bridge sees the
  767. * skb, the vlan tag will always be present if the frame was tagged.
  768. */
  769. static inline int br_vlan_get_tag(const struct sk_buff *skb, u16 *vid)
  770. {
  771. int err = 0;
  772. if (skb_vlan_tag_present(skb)) {
  773. *vid = skb_vlan_tag_get(skb) & VLAN_VID_MASK;
  774. } else {
  775. *vid = 0;
  776. err = -EINVAL;
  777. }
  778. return err;
  779. }
  780. static inline u16 br_get_pvid(const struct net_bridge_vlan_group *vg)
  781. {
  782. if (!vg)
  783. return 0;
  784. smp_rmb();
  785. return vg->pvid;
  786. }
  787. #else
  788. static inline bool br_allowed_ingress(const struct net_bridge *br,
  789. struct net_bridge_vlan_group *vg,
  790. struct sk_buff *skb,
  791. u16 *vid)
  792. {
  793. return true;
  794. }
  795. static inline bool br_allowed_egress(struct net_bridge_vlan_group *vg,
  796. const struct sk_buff *skb)
  797. {
  798. return true;
  799. }
  800. static inline bool br_should_learn(struct net_bridge_port *p,
  801. struct sk_buff *skb, u16 *vid)
  802. {
  803. return true;
  804. }
  805. static inline struct sk_buff *br_handle_vlan(struct net_bridge *br,
  806. const struct net_bridge_port *port,
  807. struct net_bridge_vlan_group *vg,
  808. struct sk_buff *skb)
  809. {
  810. return skb;
  811. }
  812. static inline int br_vlan_add(struct net_bridge *br, u16 vid, u16 flags,
  813. bool *changed)
  814. {
  815. *changed = false;
  816. return -EOPNOTSUPP;
  817. }
  818. static inline int br_vlan_delete(struct net_bridge *br, u16 vid)
  819. {
  820. return -EOPNOTSUPP;
  821. }
  822. static inline void br_vlan_flush(struct net_bridge *br)
  823. {
  824. }
  825. static inline void br_recalculate_fwd_mask(struct net_bridge *br)
  826. {
  827. }
  828. static inline int br_vlan_init(struct net_bridge *br)
  829. {
  830. return 0;
  831. }
  832. static inline int nbp_vlan_add(struct net_bridge_port *port, u16 vid, u16 flags,
  833. bool *changed)
  834. {
  835. *changed = false;
  836. return -EOPNOTSUPP;
  837. }
  838. static inline int nbp_vlan_delete(struct net_bridge_port *port, u16 vid)
  839. {
  840. return -EOPNOTSUPP;
  841. }
  842. static inline void nbp_vlan_flush(struct net_bridge_port *port)
  843. {
  844. }
  845. static inline struct net_bridge_vlan *br_vlan_find(struct net_bridge_vlan_group *vg,
  846. u16 vid)
  847. {
  848. return NULL;
  849. }
  850. static inline int nbp_vlan_init(struct net_bridge_port *port)
  851. {
  852. return 0;
  853. }
  854. static inline u16 br_vlan_get_tag(const struct sk_buff *skb, u16 *tag)
  855. {
  856. return 0;
  857. }
  858. static inline u16 br_get_pvid(const struct net_bridge_vlan_group *vg)
  859. {
  860. return 0;
  861. }
  862. static inline int __br_vlan_filter_toggle(struct net_bridge *br,
  863. unsigned long val)
  864. {
  865. return -EOPNOTSUPP;
  866. }
  867. static inline int nbp_get_num_vlan_infos(struct net_bridge_port *p,
  868. u32 filter_mask)
  869. {
  870. return 0;
  871. }
  872. static inline struct net_bridge_vlan_group *br_vlan_group(
  873. const struct net_bridge *br)
  874. {
  875. return NULL;
  876. }
  877. static inline struct net_bridge_vlan_group *nbp_vlan_group(
  878. const struct net_bridge_port *p)
  879. {
  880. return NULL;
  881. }
  882. static inline struct net_bridge_vlan_group *br_vlan_group_rcu(
  883. const struct net_bridge *br)
  884. {
  885. return NULL;
  886. }
  887. static inline struct net_bridge_vlan_group *nbp_vlan_group_rcu(
  888. const struct net_bridge_port *p)
  889. {
  890. return NULL;
  891. }
  892. static inline void br_vlan_get_stats(const struct net_bridge_vlan *v,
  893. struct br_vlan_stats *stats)
  894. {
  895. }
  896. #endif
  897. struct nf_br_ops {
  898. int (*br_dev_xmit_hook)(struct sk_buff *skb);
  899. };
  900. extern const struct nf_br_ops __rcu *nf_br_ops;
  901. /* br_netfilter.c */
  902. #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
  903. int br_nf_core_init(void);
  904. void br_nf_core_fini(void);
  905. void br_netfilter_rtable_init(struct net_bridge *);
  906. #else
  907. static inline int br_nf_core_init(void) { return 0; }
  908. static inline void br_nf_core_fini(void) {}
  909. #define br_netfilter_rtable_init(x)
  910. #endif
  911. /* br_stp.c */
  912. void br_set_state(struct net_bridge_port *p, unsigned int state);
  913. struct net_bridge_port *br_get_port(struct net_bridge *br, u16 port_no);
  914. void br_init_port(struct net_bridge_port *p);
  915. void br_become_designated_port(struct net_bridge_port *p);
  916. void __br_set_forward_delay(struct net_bridge *br, unsigned long t);
  917. int br_set_forward_delay(struct net_bridge *br, unsigned long x);
  918. int br_set_hello_time(struct net_bridge *br, unsigned long x);
  919. int br_set_max_age(struct net_bridge *br, unsigned long x);
  920. int __set_ageing_time(struct net_device *dev, unsigned long t);
  921. int br_set_ageing_time(struct net_bridge *br, clock_t ageing_time);
  922. /* br_stp_if.c */
  923. void br_stp_enable_bridge(struct net_bridge *br);
  924. void br_stp_disable_bridge(struct net_bridge *br);
  925. void br_stp_set_enabled(struct net_bridge *br, unsigned long val);
  926. void br_stp_enable_port(struct net_bridge_port *p);
  927. void br_stp_disable_port(struct net_bridge_port *p);
  928. bool br_stp_recalculate_bridge_id(struct net_bridge *br);
  929. void br_stp_change_bridge_id(struct net_bridge *br, const unsigned char *a);
  930. void br_stp_set_bridge_priority(struct net_bridge *br, u16 newprio);
  931. int br_stp_set_port_priority(struct net_bridge_port *p, unsigned long newprio);
  932. int br_stp_set_path_cost(struct net_bridge_port *p, unsigned long path_cost);
  933. ssize_t br_show_bridge_id(char *buf, const struct bridge_id *id);
  934. /* br_stp_bpdu.c */
  935. struct stp_proto;
  936. void br_stp_rcv(const struct stp_proto *proto, struct sk_buff *skb,
  937. struct net_device *dev);
  938. /* br_stp_timer.c */
  939. void br_stp_timer_init(struct net_bridge *br);
  940. void br_stp_port_timer_init(struct net_bridge_port *p);
  941. unsigned long br_timer_value(const struct timer_list *timer);
  942. /* br.c */
  943. #if IS_ENABLED(CONFIG_ATM_LANE)
  944. extern int (*br_fdb_test_addr_hook)(struct net_device *dev, unsigned char *addr);
  945. #endif
  946. /* br_netlink.c */
  947. extern struct rtnl_link_ops br_link_ops;
  948. int br_netlink_init(void);
  949. void br_netlink_fini(void);
  950. void br_ifinfo_notify(int event, const struct net_bridge *br,
  951. const struct net_bridge_port *port);
  952. int br_setlink(struct net_device *dev, struct nlmsghdr *nlmsg, u16 flags);
  953. int br_dellink(struct net_device *dev, struct nlmsghdr *nlmsg, u16 flags);
  954. int br_getlink(struct sk_buff *skb, u32 pid, u32 seq, struct net_device *dev,
  955. u32 filter_mask, int nlflags);
  956. #ifdef CONFIG_SYSFS
  957. /* br_sysfs_if.c */
  958. extern const struct sysfs_ops brport_sysfs_ops;
  959. int br_sysfs_addif(struct net_bridge_port *p);
  960. int br_sysfs_renameif(struct net_bridge_port *p);
  961. /* br_sysfs_br.c */
  962. int br_sysfs_addbr(struct net_device *dev);
  963. void br_sysfs_delbr(struct net_device *dev);
  964. #else
  965. static inline int br_sysfs_addif(struct net_bridge_port *p) { return 0; }
  966. static inline int br_sysfs_renameif(struct net_bridge_port *p) { return 0; }
  967. static inline int br_sysfs_addbr(struct net_device *dev) { return 0; }
  968. static inline void br_sysfs_delbr(struct net_device *dev) { return; }
  969. #endif /* CONFIG_SYSFS */
  970. /* br_switchdev.c */
  971. #ifdef CONFIG_NET_SWITCHDEV
  972. int nbp_switchdev_mark_set(struct net_bridge_port *p);
  973. void nbp_switchdev_frame_mark(const struct net_bridge_port *p,
  974. struct sk_buff *skb);
  975. bool nbp_switchdev_allowed_egress(const struct net_bridge_port *p,
  976. const struct sk_buff *skb);
  977. int br_switchdev_set_port_flag(struct net_bridge_port *p,
  978. unsigned long flags,
  979. unsigned long mask);
  980. void br_switchdev_fdb_notify(const struct net_bridge_fdb_entry *fdb,
  981. int type);
  982. int br_switchdev_port_vlan_add(struct net_device *dev, u16 vid, u16 flags);
  983. int br_switchdev_port_vlan_del(struct net_device *dev, u16 vid);
  984. static inline void br_switchdev_frame_unmark(struct sk_buff *skb)
  985. {
  986. skb->offload_fwd_mark = 0;
  987. }
  988. #else
  989. static inline int nbp_switchdev_mark_set(struct net_bridge_port *p)
  990. {
  991. return 0;
  992. }
  993. static inline void nbp_switchdev_frame_mark(const struct net_bridge_port *p,
  994. struct sk_buff *skb)
  995. {
  996. }
  997. static inline bool nbp_switchdev_allowed_egress(const struct net_bridge_port *p,
  998. const struct sk_buff *skb)
  999. {
  1000. return true;
  1001. }
  1002. static inline int br_switchdev_set_port_flag(struct net_bridge_port *p,
  1003. unsigned long flags,
  1004. unsigned long mask)
  1005. {
  1006. return 0;
  1007. }
  1008. static inline int br_switchdev_port_vlan_add(struct net_device *dev,
  1009. u16 vid, u16 flags)
  1010. {
  1011. return -EOPNOTSUPP;
  1012. }
  1013. static inline int br_switchdev_port_vlan_del(struct net_device *dev, u16 vid)
  1014. {
  1015. return -EOPNOTSUPP;
  1016. }
  1017. static inline void
  1018. br_switchdev_fdb_notify(const struct net_bridge_fdb_entry *fdb, int type)
  1019. {
  1020. }
  1021. static inline void br_switchdev_frame_unmark(struct sk_buff *skb)
  1022. {
  1023. }
  1024. #endif /* CONFIG_NET_SWITCHDEV */
  1025. /* br_arp_nd_proxy.c */
  1026. void br_recalculate_neigh_suppress_enabled(struct net_bridge *br);
  1027. void br_do_proxy_suppress_arp(struct sk_buff *skb, struct net_bridge *br,
  1028. u16 vid, struct net_bridge_port *p);
  1029. void br_do_suppress_nd(struct sk_buff *skb, struct net_bridge *br,
  1030. u16 vid, struct net_bridge_port *p, struct nd_msg *msg);
  1031. struct nd_msg *br_is_nd_neigh_msg(struct sk_buff *skb, struct nd_msg *m);
  1032. #endif