br_netlink.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455
  1. /*
  2. * Bridge netlink control interface
  3. *
  4. * Authors:
  5. * Stephen Hemminger <shemminger@osdl.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. #include <linux/kernel.h>
  13. #include <linux/slab.h>
  14. #include <linux/etherdevice.h>
  15. #include <net/rtnetlink.h>
  16. #include <net/net_namespace.h>
  17. #include <net/sock.h>
  18. #include <uapi/linux/if_bridge.h>
  19. #include "br_private.h"
  20. #include "br_private_stp.h"
  21. static int __get_num_vlan_infos(struct net_bridge_vlan_group *vg,
  22. u32 filter_mask)
  23. {
  24. struct net_bridge_vlan *v;
  25. u16 vid_range_start = 0, vid_range_end = 0, vid_range_flags = 0;
  26. u16 flags, pvid;
  27. int num_vlans = 0;
  28. if (!(filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED))
  29. return 0;
  30. pvid = br_get_pvid(vg);
  31. /* Count number of vlan infos */
  32. list_for_each_entry_rcu(v, &vg->vlan_list, vlist) {
  33. flags = 0;
  34. /* only a context, bridge vlan not activated */
  35. if (!br_vlan_should_use(v))
  36. continue;
  37. if (v->vid == pvid)
  38. flags |= BRIDGE_VLAN_INFO_PVID;
  39. if (v->flags & BRIDGE_VLAN_INFO_UNTAGGED)
  40. flags |= BRIDGE_VLAN_INFO_UNTAGGED;
  41. if (vid_range_start == 0) {
  42. goto initvars;
  43. } else if ((v->vid - vid_range_end) == 1 &&
  44. flags == vid_range_flags) {
  45. vid_range_end = v->vid;
  46. continue;
  47. } else {
  48. if ((vid_range_end - vid_range_start) > 0)
  49. num_vlans += 2;
  50. else
  51. num_vlans += 1;
  52. }
  53. initvars:
  54. vid_range_start = v->vid;
  55. vid_range_end = v->vid;
  56. vid_range_flags = flags;
  57. }
  58. if (vid_range_start != 0) {
  59. if ((vid_range_end - vid_range_start) > 0)
  60. num_vlans += 2;
  61. else
  62. num_vlans += 1;
  63. }
  64. return num_vlans;
  65. }
  66. static int br_get_num_vlan_infos(struct net_bridge_vlan_group *vg,
  67. u32 filter_mask)
  68. {
  69. int num_vlans;
  70. if (!vg)
  71. return 0;
  72. if (filter_mask & RTEXT_FILTER_BRVLAN)
  73. return vg->num_vlans;
  74. rcu_read_lock();
  75. num_vlans = __get_num_vlan_infos(vg, filter_mask);
  76. rcu_read_unlock();
  77. return num_vlans;
  78. }
  79. static size_t br_get_link_af_size_filtered(const struct net_device *dev,
  80. u32 filter_mask)
  81. {
  82. struct net_bridge_vlan_group *vg = NULL;
  83. struct net_bridge_port *p;
  84. struct net_bridge *br;
  85. int num_vlan_infos;
  86. rcu_read_lock();
  87. if (br_port_exists(dev)) {
  88. p = br_port_get_rcu(dev);
  89. vg = nbp_vlan_group_rcu(p);
  90. } else if (dev->priv_flags & IFF_EBRIDGE) {
  91. br = netdev_priv(dev);
  92. vg = br_vlan_group_rcu(br);
  93. }
  94. num_vlan_infos = br_get_num_vlan_infos(vg, filter_mask);
  95. rcu_read_unlock();
  96. /* Each VLAN is returned in bridge_vlan_info along with flags */
  97. return num_vlan_infos * nla_total_size(sizeof(struct bridge_vlan_info));
  98. }
  99. static inline size_t br_port_info_size(void)
  100. {
  101. return nla_total_size(1) /* IFLA_BRPORT_STATE */
  102. + nla_total_size(2) /* IFLA_BRPORT_PRIORITY */
  103. + nla_total_size(4) /* IFLA_BRPORT_COST */
  104. + nla_total_size(1) /* IFLA_BRPORT_MODE */
  105. + nla_total_size(1) /* IFLA_BRPORT_GUARD */
  106. + nla_total_size(1) /* IFLA_BRPORT_PROTECT */
  107. + nla_total_size(1) /* IFLA_BRPORT_FAST_LEAVE */
  108. + nla_total_size(1) /* IFLA_BRPORT_LEARNING */
  109. + nla_total_size(1) /* IFLA_BRPORT_UNICAST_FLOOD */
  110. + nla_total_size(1) /* IFLA_BRPORT_PROXYARP */
  111. + nla_total_size(1) /* IFLA_BRPORT_PROXYARP_WIFI */
  112. + nla_total_size(sizeof(struct ifla_bridge_id)) /* IFLA_BRPORT_ROOT_ID */
  113. + nla_total_size(sizeof(struct ifla_bridge_id)) /* IFLA_BRPORT_BRIDGE_ID */
  114. + nla_total_size(sizeof(u16)) /* IFLA_BRPORT_DESIGNATED_PORT */
  115. + nla_total_size(sizeof(u16)) /* IFLA_BRPORT_DESIGNATED_COST */
  116. + nla_total_size(sizeof(u16)) /* IFLA_BRPORT_ID */
  117. + nla_total_size(sizeof(u16)) /* IFLA_BRPORT_NO */
  118. + nla_total_size(sizeof(u8)) /* IFLA_BRPORT_TOPOLOGY_CHANGE_ACK */
  119. + nla_total_size(sizeof(u8)) /* IFLA_BRPORT_CONFIG_PENDING */
  120. + nla_total_size_64bit(sizeof(u64)) /* IFLA_BRPORT_MESSAGE_AGE_TIMER */
  121. + nla_total_size_64bit(sizeof(u64)) /* IFLA_BRPORT_FORWARD_DELAY_TIMER */
  122. + nla_total_size_64bit(sizeof(u64)) /* IFLA_BRPORT_HOLD_TIMER */
  123. #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
  124. + nla_total_size(sizeof(u8)) /* IFLA_BRPORT_MULTICAST_ROUTER */
  125. #endif
  126. + 0;
  127. }
  128. static inline size_t br_nlmsg_size(struct net_device *dev, u32 filter_mask)
  129. {
  130. return NLMSG_ALIGN(sizeof(struct ifinfomsg))
  131. + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
  132. + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
  133. + nla_total_size(4) /* IFLA_MASTER */
  134. + nla_total_size(4) /* IFLA_MTU */
  135. + nla_total_size(4) /* IFLA_LINK */
  136. + nla_total_size(1) /* IFLA_OPERSTATE */
  137. + nla_total_size(br_port_info_size()) /* IFLA_PROTINFO */
  138. + nla_total_size(br_get_link_af_size_filtered(dev,
  139. filter_mask)); /* IFLA_AF_SPEC */
  140. }
  141. static int br_port_fill_attrs(struct sk_buff *skb,
  142. const struct net_bridge_port *p)
  143. {
  144. u8 mode = !!(p->flags & BR_HAIRPIN_MODE);
  145. u64 timerval;
  146. if (nla_put_u8(skb, IFLA_BRPORT_STATE, p->state) ||
  147. nla_put_u16(skb, IFLA_BRPORT_PRIORITY, p->priority) ||
  148. nla_put_u32(skb, IFLA_BRPORT_COST, p->path_cost) ||
  149. nla_put_u8(skb, IFLA_BRPORT_MODE, mode) ||
  150. nla_put_u8(skb, IFLA_BRPORT_GUARD, !!(p->flags & BR_BPDU_GUARD)) ||
  151. nla_put_u8(skb, IFLA_BRPORT_PROTECT,
  152. !!(p->flags & BR_ROOT_BLOCK)) ||
  153. nla_put_u8(skb, IFLA_BRPORT_FAST_LEAVE,
  154. !!(p->flags & BR_MULTICAST_FAST_LEAVE)) ||
  155. nla_put_u8(skb, IFLA_BRPORT_LEARNING, !!(p->flags & BR_LEARNING)) ||
  156. nla_put_u8(skb, IFLA_BRPORT_UNICAST_FLOOD,
  157. !!(p->flags & BR_FLOOD)) ||
  158. nla_put_u8(skb, IFLA_BRPORT_MCAST_FLOOD,
  159. !!(p->flags & BR_MCAST_FLOOD)) ||
  160. nla_put_u8(skb, IFLA_BRPORT_PROXYARP, !!(p->flags & BR_PROXYARP)) ||
  161. nla_put_u8(skb, IFLA_BRPORT_PROXYARP_WIFI,
  162. !!(p->flags & BR_PROXYARP_WIFI)) ||
  163. nla_put(skb, IFLA_BRPORT_ROOT_ID, sizeof(struct ifla_bridge_id),
  164. &p->designated_root) ||
  165. nla_put(skb, IFLA_BRPORT_BRIDGE_ID, sizeof(struct ifla_bridge_id),
  166. &p->designated_bridge) ||
  167. nla_put_u16(skb, IFLA_BRPORT_DESIGNATED_PORT, p->designated_port) ||
  168. nla_put_u16(skb, IFLA_BRPORT_DESIGNATED_COST, p->designated_cost) ||
  169. nla_put_u16(skb, IFLA_BRPORT_ID, p->port_id) ||
  170. nla_put_u16(skb, IFLA_BRPORT_NO, p->port_no) ||
  171. nla_put_u8(skb, IFLA_BRPORT_TOPOLOGY_CHANGE_ACK,
  172. p->topology_change_ack) ||
  173. nla_put_u8(skb, IFLA_BRPORT_CONFIG_PENDING, p->config_pending))
  174. return -EMSGSIZE;
  175. timerval = br_timer_value(&p->message_age_timer);
  176. if (nla_put_u64_64bit(skb, IFLA_BRPORT_MESSAGE_AGE_TIMER, timerval,
  177. IFLA_BRPORT_PAD))
  178. return -EMSGSIZE;
  179. timerval = br_timer_value(&p->forward_delay_timer);
  180. if (nla_put_u64_64bit(skb, IFLA_BRPORT_FORWARD_DELAY_TIMER, timerval,
  181. IFLA_BRPORT_PAD))
  182. return -EMSGSIZE;
  183. timerval = br_timer_value(&p->hold_timer);
  184. if (nla_put_u64_64bit(skb, IFLA_BRPORT_HOLD_TIMER, timerval,
  185. IFLA_BRPORT_PAD))
  186. return -EMSGSIZE;
  187. #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
  188. if (nla_put_u8(skb, IFLA_BRPORT_MULTICAST_ROUTER,
  189. p->multicast_router))
  190. return -EMSGSIZE;
  191. #endif
  192. return 0;
  193. }
  194. static int br_fill_ifvlaninfo_range(struct sk_buff *skb, u16 vid_start,
  195. u16 vid_end, u16 flags)
  196. {
  197. struct bridge_vlan_info vinfo;
  198. if ((vid_end - vid_start) > 0) {
  199. /* add range to skb */
  200. vinfo.vid = vid_start;
  201. vinfo.flags = flags | BRIDGE_VLAN_INFO_RANGE_BEGIN;
  202. if (nla_put(skb, IFLA_BRIDGE_VLAN_INFO,
  203. sizeof(vinfo), &vinfo))
  204. goto nla_put_failure;
  205. vinfo.vid = vid_end;
  206. vinfo.flags = flags | BRIDGE_VLAN_INFO_RANGE_END;
  207. if (nla_put(skb, IFLA_BRIDGE_VLAN_INFO,
  208. sizeof(vinfo), &vinfo))
  209. goto nla_put_failure;
  210. } else {
  211. vinfo.vid = vid_start;
  212. vinfo.flags = flags;
  213. if (nla_put(skb, IFLA_BRIDGE_VLAN_INFO,
  214. sizeof(vinfo), &vinfo))
  215. goto nla_put_failure;
  216. }
  217. return 0;
  218. nla_put_failure:
  219. return -EMSGSIZE;
  220. }
  221. static int br_fill_ifvlaninfo_compressed(struct sk_buff *skb,
  222. struct net_bridge_vlan_group *vg)
  223. {
  224. struct net_bridge_vlan *v;
  225. u16 vid_range_start = 0, vid_range_end = 0, vid_range_flags = 0;
  226. u16 flags, pvid;
  227. int err = 0;
  228. /* Pack IFLA_BRIDGE_VLAN_INFO's for every vlan
  229. * and mark vlan info with begin and end flags
  230. * if vlaninfo represents a range
  231. */
  232. pvid = br_get_pvid(vg);
  233. list_for_each_entry_rcu(v, &vg->vlan_list, vlist) {
  234. flags = 0;
  235. if (!br_vlan_should_use(v))
  236. continue;
  237. if (v->vid == pvid)
  238. flags |= BRIDGE_VLAN_INFO_PVID;
  239. if (v->flags & BRIDGE_VLAN_INFO_UNTAGGED)
  240. flags |= BRIDGE_VLAN_INFO_UNTAGGED;
  241. if (vid_range_start == 0) {
  242. goto initvars;
  243. } else if ((v->vid - vid_range_end) == 1 &&
  244. flags == vid_range_flags) {
  245. vid_range_end = v->vid;
  246. continue;
  247. } else {
  248. err = br_fill_ifvlaninfo_range(skb, vid_range_start,
  249. vid_range_end,
  250. vid_range_flags);
  251. if (err)
  252. return err;
  253. }
  254. initvars:
  255. vid_range_start = v->vid;
  256. vid_range_end = v->vid;
  257. vid_range_flags = flags;
  258. }
  259. if (vid_range_start != 0) {
  260. /* Call it once more to send any left over vlans */
  261. err = br_fill_ifvlaninfo_range(skb, vid_range_start,
  262. vid_range_end,
  263. vid_range_flags);
  264. if (err)
  265. return err;
  266. }
  267. return 0;
  268. }
  269. static int br_fill_ifvlaninfo(struct sk_buff *skb,
  270. struct net_bridge_vlan_group *vg)
  271. {
  272. struct bridge_vlan_info vinfo;
  273. struct net_bridge_vlan *v;
  274. u16 pvid;
  275. pvid = br_get_pvid(vg);
  276. list_for_each_entry_rcu(v, &vg->vlan_list, vlist) {
  277. if (!br_vlan_should_use(v))
  278. continue;
  279. vinfo.vid = v->vid;
  280. vinfo.flags = 0;
  281. if (v->vid == pvid)
  282. vinfo.flags |= BRIDGE_VLAN_INFO_PVID;
  283. if (v->flags & BRIDGE_VLAN_INFO_UNTAGGED)
  284. vinfo.flags |= BRIDGE_VLAN_INFO_UNTAGGED;
  285. if (nla_put(skb, IFLA_BRIDGE_VLAN_INFO,
  286. sizeof(vinfo), &vinfo))
  287. goto nla_put_failure;
  288. }
  289. return 0;
  290. nla_put_failure:
  291. return -EMSGSIZE;
  292. }
  293. /*
  294. * Create one netlink message for one interface
  295. * Contains port and master info as well as carrier and bridge state.
  296. */
  297. static int br_fill_ifinfo(struct sk_buff *skb,
  298. struct net_bridge_port *port,
  299. u32 pid, u32 seq, int event, unsigned int flags,
  300. u32 filter_mask, const struct net_device *dev)
  301. {
  302. struct net_bridge *br;
  303. struct ifinfomsg *hdr;
  304. struct nlmsghdr *nlh;
  305. u8 operstate = netif_running(dev) ? dev->operstate : IF_OPER_DOWN;
  306. if (port)
  307. br = port->br;
  308. else
  309. br = netdev_priv(dev);
  310. br_debug(br, "br_fill_info event %d port %s master %s\n",
  311. event, dev->name, br->dev->name);
  312. nlh = nlmsg_put(skb, pid, seq, event, sizeof(*hdr), flags);
  313. if (nlh == NULL)
  314. return -EMSGSIZE;
  315. hdr = nlmsg_data(nlh);
  316. hdr->ifi_family = AF_BRIDGE;
  317. hdr->__ifi_pad = 0;
  318. hdr->ifi_type = dev->type;
  319. hdr->ifi_index = dev->ifindex;
  320. hdr->ifi_flags = dev_get_flags(dev);
  321. hdr->ifi_change = 0;
  322. if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
  323. nla_put_u32(skb, IFLA_MASTER, br->dev->ifindex) ||
  324. nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
  325. nla_put_u8(skb, IFLA_OPERSTATE, operstate) ||
  326. (dev->addr_len &&
  327. nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
  328. (dev->ifindex != dev_get_iflink(dev) &&
  329. nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))))
  330. goto nla_put_failure;
  331. if (event == RTM_NEWLINK && port) {
  332. struct nlattr *nest
  333. = nla_nest_start(skb, IFLA_PROTINFO | NLA_F_NESTED);
  334. if (nest == NULL || br_port_fill_attrs(skb, port) < 0)
  335. goto nla_put_failure;
  336. nla_nest_end(skb, nest);
  337. }
  338. /* Check if the VID information is requested */
  339. if ((filter_mask & RTEXT_FILTER_BRVLAN) ||
  340. (filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED)) {
  341. struct net_bridge_vlan_group *vg;
  342. struct nlattr *af;
  343. int err;
  344. /* RCU needed because of the VLAN locking rules (rcu || rtnl) */
  345. rcu_read_lock();
  346. if (port)
  347. vg = nbp_vlan_group_rcu(port);
  348. else
  349. vg = br_vlan_group_rcu(br);
  350. if (!vg || !vg->num_vlans) {
  351. rcu_read_unlock();
  352. goto done;
  353. }
  354. af = nla_nest_start(skb, IFLA_AF_SPEC);
  355. if (!af) {
  356. rcu_read_unlock();
  357. goto nla_put_failure;
  358. }
  359. if (filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED)
  360. err = br_fill_ifvlaninfo_compressed(skb, vg);
  361. else
  362. err = br_fill_ifvlaninfo(skb, vg);
  363. rcu_read_unlock();
  364. if (err)
  365. goto nla_put_failure;
  366. nla_nest_end(skb, af);
  367. }
  368. done:
  369. nlmsg_end(skb, nlh);
  370. return 0;
  371. nla_put_failure:
  372. nlmsg_cancel(skb, nlh);
  373. return -EMSGSIZE;
  374. }
  375. /*
  376. * Notify listeners of a change in port information
  377. */
  378. void br_ifinfo_notify(int event, struct net_bridge_port *port)
  379. {
  380. struct net *net;
  381. struct sk_buff *skb;
  382. int err = -ENOBUFS;
  383. u32 filter = RTEXT_FILTER_BRVLAN_COMPRESSED;
  384. if (!port)
  385. return;
  386. net = dev_net(port->dev);
  387. br_debug(port->br, "port %u(%s) event %d\n",
  388. (unsigned int)port->port_no, port->dev->name, event);
  389. skb = nlmsg_new(br_nlmsg_size(port->dev, filter), GFP_ATOMIC);
  390. if (skb == NULL)
  391. goto errout;
  392. err = br_fill_ifinfo(skb, port, 0, 0, event, 0, filter, port->dev);
  393. if (err < 0) {
  394. /* -EMSGSIZE implies BUG in br_nlmsg_size() */
  395. WARN_ON(err == -EMSGSIZE);
  396. kfree_skb(skb);
  397. goto errout;
  398. }
  399. rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC);
  400. return;
  401. errout:
  402. rtnl_set_sk_err(net, RTNLGRP_LINK, err);
  403. }
  404. /*
  405. * Dump information about all ports, in response to GETLINK
  406. */
  407. int br_getlink(struct sk_buff *skb, u32 pid, u32 seq,
  408. struct net_device *dev, u32 filter_mask, int nlflags)
  409. {
  410. struct net_bridge_port *port = br_port_get_rtnl(dev);
  411. if (!port && !(filter_mask & RTEXT_FILTER_BRVLAN) &&
  412. !(filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED))
  413. return 0;
  414. return br_fill_ifinfo(skb, port, pid, seq, RTM_NEWLINK, nlflags,
  415. filter_mask, dev);
  416. }
  417. static int br_vlan_info(struct net_bridge *br, struct net_bridge_port *p,
  418. int cmd, struct bridge_vlan_info *vinfo)
  419. {
  420. int err = 0;
  421. switch (cmd) {
  422. case RTM_SETLINK:
  423. if (p) {
  424. /* if the MASTER flag is set this will act on the global
  425. * per-VLAN entry as well
  426. */
  427. err = nbp_vlan_add(p, vinfo->vid, vinfo->flags);
  428. if (err)
  429. break;
  430. } else {
  431. vinfo->flags |= BRIDGE_VLAN_INFO_BRENTRY;
  432. err = br_vlan_add(br, vinfo->vid, vinfo->flags);
  433. }
  434. break;
  435. case RTM_DELLINK:
  436. if (p) {
  437. nbp_vlan_delete(p, vinfo->vid);
  438. if (vinfo->flags & BRIDGE_VLAN_INFO_MASTER)
  439. br_vlan_delete(p->br, vinfo->vid);
  440. } else {
  441. br_vlan_delete(br, vinfo->vid);
  442. }
  443. break;
  444. }
  445. return err;
  446. }
  447. static int br_afspec(struct net_bridge *br,
  448. struct net_bridge_port *p,
  449. struct nlattr *af_spec,
  450. int cmd)
  451. {
  452. struct bridge_vlan_info *vinfo_start = NULL;
  453. struct bridge_vlan_info *vinfo = NULL;
  454. struct nlattr *attr;
  455. int err = 0;
  456. int rem;
  457. nla_for_each_nested(attr, af_spec, rem) {
  458. if (nla_type(attr) != IFLA_BRIDGE_VLAN_INFO)
  459. continue;
  460. if (nla_len(attr) != sizeof(struct bridge_vlan_info))
  461. return -EINVAL;
  462. vinfo = nla_data(attr);
  463. if (!vinfo->vid || vinfo->vid >= VLAN_VID_MASK)
  464. return -EINVAL;
  465. if (vinfo->flags & BRIDGE_VLAN_INFO_RANGE_BEGIN) {
  466. if (vinfo_start)
  467. return -EINVAL;
  468. vinfo_start = vinfo;
  469. /* don't allow range of pvids */
  470. if (vinfo_start->flags & BRIDGE_VLAN_INFO_PVID)
  471. return -EINVAL;
  472. continue;
  473. }
  474. if (vinfo_start) {
  475. struct bridge_vlan_info tmp_vinfo;
  476. int v;
  477. if (!(vinfo->flags & BRIDGE_VLAN_INFO_RANGE_END))
  478. return -EINVAL;
  479. if (vinfo->vid <= vinfo_start->vid)
  480. return -EINVAL;
  481. memcpy(&tmp_vinfo, vinfo_start,
  482. sizeof(struct bridge_vlan_info));
  483. for (v = vinfo_start->vid; v <= vinfo->vid; v++) {
  484. tmp_vinfo.vid = v;
  485. err = br_vlan_info(br, p, cmd, &tmp_vinfo);
  486. if (err)
  487. break;
  488. }
  489. vinfo_start = NULL;
  490. } else {
  491. err = br_vlan_info(br, p, cmd, vinfo);
  492. }
  493. if (err)
  494. break;
  495. }
  496. return err;
  497. }
  498. static const struct nla_policy br_port_policy[IFLA_BRPORT_MAX + 1] = {
  499. [IFLA_BRPORT_STATE] = { .type = NLA_U8 },
  500. [IFLA_BRPORT_COST] = { .type = NLA_U32 },
  501. [IFLA_BRPORT_PRIORITY] = { .type = NLA_U16 },
  502. [IFLA_BRPORT_MODE] = { .type = NLA_U8 },
  503. [IFLA_BRPORT_GUARD] = { .type = NLA_U8 },
  504. [IFLA_BRPORT_PROTECT] = { .type = NLA_U8 },
  505. [IFLA_BRPORT_FAST_LEAVE]= { .type = NLA_U8 },
  506. [IFLA_BRPORT_LEARNING] = { .type = NLA_U8 },
  507. [IFLA_BRPORT_UNICAST_FLOOD] = { .type = NLA_U8 },
  508. [IFLA_BRPORT_PROXYARP] = { .type = NLA_U8 },
  509. [IFLA_BRPORT_PROXYARP_WIFI] = { .type = NLA_U8 },
  510. [IFLA_BRPORT_MULTICAST_ROUTER] = { .type = NLA_U8 },
  511. };
  512. /* Change the state of the port and notify spanning tree */
  513. static int br_set_port_state(struct net_bridge_port *p, u8 state)
  514. {
  515. if (state > BR_STATE_BLOCKING)
  516. return -EINVAL;
  517. /* if kernel STP is running, don't allow changes */
  518. if (p->br->stp_enabled == BR_KERNEL_STP)
  519. return -EBUSY;
  520. /* if device is not up, change is not allowed
  521. * if link is not present, only allowable state is disabled
  522. */
  523. if (!netif_running(p->dev) ||
  524. (!netif_oper_up(p->dev) && state != BR_STATE_DISABLED))
  525. return -ENETDOWN;
  526. br_set_state(p, state);
  527. br_port_state_selection(p->br);
  528. return 0;
  529. }
  530. /* Set/clear or port flags based on attribute */
  531. static void br_set_port_flag(struct net_bridge_port *p, struct nlattr *tb[],
  532. int attrtype, unsigned long mask)
  533. {
  534. if (tb[attrtype]) {
  535. u8 flag = nla_get_u8(tb[attrtype]);
  536. if (flag)
  537. p->flags |= mask;
  538. else
  539. p->flags &= ~mask;
  540. }
  541. }
  542. /* Process bridge protocol info on port */
  543. static int br_setport(struct net_bridge_port *p, struct nlattr *tb[])
  544. {
  545. int err;
  546. unsigned long old_flags = p->flags;
  547. br_set_port_flag(p, tb, IFLA_BRPORT_MODE, BR_HAIRPIN_MODE);
  548. br_set_port_flag(p, tb, IFLA_BRPORT_GUARD, BR_BPDU_GUARD);
  549. br_set_port_flag(p, tb, IFLA_BRPORT_FAST_LEAVE, BR_MULTICAST_FAST_LEAVE);
  550. br_set_port_flag(p, tb, IFLA_BRPORT_PROTECT, BR_ROOT_BLOCK);
  551. br_set_port_flag(p, tb, IFLA_BRPORT_LEARNING, BR_LEARNING);
  552. br_set_port_flag(p, tb, IFLA_BRPORT_UNICAST_FLOOD, BR_FLOOD);
  553. br_set_port_flag(p, tb, IFLA_BRPORT_MCAST_FLOOD, BR_MCAST_FLOOD);
  554. br_set_port_flag(p, tb, IFLA_BRPORT_PROXYARP, BR_PROXYARP);
  555. br_set_port_flag(p, tb, IFLA_BRPORT_PROXYARP_WIFI, BR_PROXYARP_WIFI);
  556. if (tb[IFLA_BRPORT_COST]) {
  557. err = br_stp_set_path_cost(p, nla_get_u32(tb[IFLA_BRPORT_COST]));
  558. if (err)
  559. return err;
  560. }
  561. if (tb[IFLA_BRPORT_PRIORITY]) {
  562. err = br_stp_set_port_priority(p, nla_get_u16(tb[IFLA_BRPORT_PRIORITY]));
  563. if (err)
  564. return err;
  565. }
  566. if (tb[IFLA_BRPORT_STATE]) {
  567. err = br_set_port_state(p, nla_get_u8(tb[IFLA_BRPORT_STATE]));
  568. if (err)
  569. return err;
  570. }
  571. if (tb[IFLA_BRPORT_FLUSH])
  572. br_fdb_delete_by_port(p->br, p, 0, 0);
  573. #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
  574. if (tb[IFLA_BRPORT_MULTICAST_ROUTER]) {
  575. u8 mcast_router = nla_get_u8(tb[IFLA_BRPORT_MULTICAST_ROUTER]);
  576. err = br_multicast_set_port_router(p, mcast_router);
  577. if (err)
  578. return err;
  579. }
  580. #endif
  581. br_port_flags_change(p, old_flags ^ p->flags);
  582. return 0;
  583. }
  584. /* Change state and parameters on port. */
  585. int br_setlink(struct net_device *dev, struct nlmsghdr *nlh, u16 flags)
  586. {
  587. struct nlattr *protinfo;
  588. struct nlattr *afspec;
  589. struct net_bridge_port *p;
  590. struct nlattr *tb[IFLA_BRPORT_MAX + 1];
  591. int err = 0;
  592. protinfo = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_PROTINFO);
  593. afspec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
  594. if (!protinfo && !afspec)
  595. return 0;
  596. p = br_port_get_rtnl(dev);
  597. /* We want to accept dev as bridge itself if the AF_SPEC
  598. * is set to see if someone is setting vlan info on the bridge
  599. */
  600. if (!p && !afspec)
  601. return -EINVAL;
  602. if (p && protinfo) {
  603. if (protinfo->nla_type & NLA_F_NESTED) {
  604. err = nla_parse_nested(tb, IFLA_BRPORT_MAX,
  605. protinfo, br_port_policy);
  606. if (err)
  607. return err;
  608. spin_lock_bh(&p->br->lock);
  609. err = br_setport(p, tb);
  610. spin_unlock_bh(&p->br->lock);
  611. } else {
  612. /* Binary compatibility with old RSTP */
  613. if (nla_len(protinfo) < sizeof(u8))
  614. return -EINVAL;
  615. spin_lock_bh(&p->br->lock);
  616. err = br_set_port_state(p, nla_get_u8(protinfo));
  617. spin_unlock_bh(&p->br->lock);
  618. }
  619. if (err)
  620. goto out;
  621. }
  622. if (afspec) {
  623. err = br_afspec((struct net_bridge *)netdev_priv(dev), p,
  624. afspec, RTM_SETLINK);
  625. }
  626. if (err == 0)
  627. br_ifinfo_notify(RTM_NEWLINK, p);
  628. out:
  629. return err;
  630. }
  631. /* Delete port information */
  632. int br_dellink(struct net_device *dev, struct nlmsghdr *nlh, u16 flags)
  633. {
  634. struct nlattr *afspec;
  635. struct net_bridge_port *p;
  636. int err = 0;
  637. afspec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
  638. if (!afspec)
  639. return 0;
  640. p = br_port_get_rtnl(dev);
  641. /* We want to accept dev as bridge itself as well */
  642. if (!p && !(dev->priv_flags & IFF_EBRIDGE))
  643. return -EINVAL;
  644. err = br_afspec((struct net_bridge *)netdev_priv(dev), p,
  645. afspec, RTM_DELLINK);
  646. if (err == 0)
  647. /* Send RTM_NEWLINK because userspace
  648. * expects RTM_NEWLINK for vlan dels
  649. */
  650. br_ifinfo_notify(RTM_NEWLINK, p);
  651. return err;
  652. }
  653. static int br_validate(struct nlattr *tb[], struct nlattr *data[])
  654. {
  655. if (tb[IFLA_ADDRESS]) {
  656. if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
  657. return -EINVAL;
  658. if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
  659. return -EADDRNOTAVAIL;
  660. }
  661. if (!data)
  662. return 0;
  663. #ifdef CONFIG_BRIDGE_VLAN_FILTERING
  664. if (data[IFLA_BR_VLAN_PROTOCOL]) {
  665. switch (nla_get_be16(data[IFLA_BR_VLAN_PROTOCOL])) {
  666. case htons(ETH_P_8021Q):
  667. case htons(ETH_P_8021AD):
  668. break;
  669. default:
  670. return -EPROTONOSUPPORT;
  671. }
  672. }
  673. #endif
  674. return 0;
  675. }
  676. static int br_port_slave_changelink(struct net_device *brdev,
  677. struct net_device *dev,
  678. struct nlattr *tb[],
  679. struct nlattr *data[])
  680. {
  681. struct net_bridge *br = netdev_priv(brdev);
  682. int ret;
  683. if (!data)
  684. return 0;
  685. spin_lock_bh(&br->lock);
  686. ret = br_setport(br_port_get_rtnl(dev), data);
  687. spin_unlock_bh(&br->lock);
  688. return ret;
  689. }
  690. static int br_port_fill_slave_info(struct sk_buff *skb,
  691. const struct net_device *brdev,
  692. const struct net_device *dev)
  693. {
  694. return br_port_fill_attrs(skb, br_port_get_rtnl(dev));
  695. }
  696. static size_t br_port_get_slave_size(const struct net_device *brdev,
  697. const struct net_device *dev)
  698. {
  699. return br_port_info_size();
  700. }
  701. static const struct nla_policy br_policy[IFLA_BR_MAX + 1] = {
  702. [IFLA_BR_FORWARD_DELAY] = { .type = NLA_U32 },
  703. [IFLA_BR_HELLO_TIME] = { .type = NLA_U32 },
  704. [IFLA_BR_MAX_AGE] = { .type = NLA_U32 },
  705. [IFLA_BR_AGEING_TIME] = { .type = NLA_U32 },
  706. [IFLA_BR_STP_STATE] = { .type = NLA_U32 },
  707. [IFLA_BR_PRIORITY] = { .type = NLA_U16 },
  708. [IFLA_BR_VLAN_FILTERING] = { .type = NLA_U8 },
  709. [IFLA_BR_VLAN_PROTOCOL] = { .type = NLA_U16 },
  710. [IFLA_BR_GROUP_FWD_MASK] = { .type = NLA_U16 },
  711. [IFLA_BR_GROUP_ADDR] = { .type = NLA_BINARY,
  712. .len = ETH_ALEN },
  713. [IFLA_BR_MCAST_ROUTER] = { .type = NLA_U8 },
  714. [IFLA_BR_MCAST_SNOOPING] = { .type = NLA_U8 },
  715. [IFLA_BR_MCAST_QUERY_USE_IFADDR] = { .type = NLA_U8 },
  716. [IFLA_BR_MCAST_QUERIER] = { .type = NLA_U8 },
  717. [IFLA_BR_MCAST_HASH_ELASTICITY] = { .type = NLA_U32 },
  718. [IFLA_BR_MCAST_HASH_MAX] = { .type = NLA_U32 },
  719. [IFLA_BR_MCAST_LAST_MEMBER_CNT] = { .type = NLA_U32 },
  720. [IFLA_BR_MCAST_STARTUP_QUERY_CNT] = { .type = NLA_U32 },
  721. [IFLA_BR_MCAST_LAST_MEMBER_INTVL] = { .type = NLA_U64 },
  722. [IFLA_BR_MCAST_MEMBERSHIP_INTVL] = { .type = NLA_U64 },
  723. [IFLA_BR_MCAST_QUERIER_INTVL] = { .type = NLA_U64 },
  724. [IFLA_BR_MCAST_QUERY_INTVL] = { .type = NLA_U64 },
  725. [IFLA_BR_MCAST_QUERY_RESPONSE_INTVL] = { .type = NLA_U64 },
  726. [IFLA_BR_MCAST_STARTUP_QUERY_INTVL] = { .type = NLA_U64 },
  727. [IFLA_BR_NF_CALL_IPTABLES] = { .type = NLA_U8 },
  728. [IFLA_BR_NF_CALL_IP6TABLES] = { .type = NLA_U8 },
  729. [IFLA_BR_NF_CALL_ARPTABLES] = { .type = NLA_U8 },
  730. [IFLA_BR_VLAN_DEFAULT_PVID] = { .type = NLA_U16 },
  731. [IFLA_BR_VLAN_STATS_ENABLED] = { .type = NLA_U8 },
  732. [IFLA_BR_MCAST_STATS_ENABLED] = { .type = NLA_U8 },
  733. [IFLA_BR_MCAST_IGMP_VERSION] = { .type = NLA_U8 },
  734. [IFLA_BR_MCAST_MLD_VERSION] = { .type = NLA_U8 },
  735. };
  736. static int br_changelink(struct net_device *brdev, struct nlattr *tb[],
  737. struct nlattr *data[])
  738. {
  739. struct net_bridge *br = netdev_priv(brdev);
  740. int err;
  741. if (!data)
  742. return 0;
  743. if (data[IFLA_BR_FORWARD_DELAY]) {
  744. err = br_set_forward_delay(br, nla_get_u32(data[IFLA_BR_FORWARD_DELAY]));
  745. if (err)
  746. return err;
  747. }
  748. if (data[IFLA_BR_HELLO_TIME]) {
  749. err = br_set_hello_time(br, nla_get_u32(data[IFLA_BR_HELLO_TIME]));
  750. if (err)
  751. return err;
  752. }
  753. if (data[IFLA_BR_MAX_AGE]) {
  754. err = br_set_max_age(br, nla_get_u32(data[IFLA_BR_MAX_AGE]));
  755. if (err)
  756. return err;
  757. }
  758. if (data[IFLA_BR_AGEING_TIME]) {
  759. err = br_set_ageing_time(br, nla_get_u32(data[IFLA_BR_AGEING_TIME]));
  760. if (err)
  761. return err;
  762. }
  763. if (data[IFLA_BR_STP_STATE]) {
  764. u32 stp_enabled = nla_get_u32(data[IFLA_BR_STP_STATE]);
  765. br_stp_set_enabled(br, stp_enabled);
  766. }
  767. if (data[IFLA_BR_PRIORITY]) {
  768. u32 priority = nla_get_u16(data[IFLA_BR_PRIORITY]);
  769. br_stp_set_bridge_priority(br, priority);
  770. }
  771. if (data[IFLA_BR_VLAN_FILTERING]) {
  772. u8 vlan_filter = nla_get_u8(data[IFLA_BR_VLAN_FILTERING]);
  773. err = __br_vlan_filter_toggle(br, vlan_filter);
  774. if (err)
  775. return err;
  776. }
  777. #ifdef CONFIG_BRIDGE_VLAN_FILTERING
  778. if (data[IFLA_BR_VLAN_PROTOCOL]) {
  779. __be16 vlan_proto = nla_get_be16(data[IFLA_BR_VLAN_PROTOCOL]);
  780. err = __br_vlan_set_proto(br, vlan_proto);
  781. if (err)
  782. return err;
  783. }
  784. if (data[IFLA_BR_VLAN_DEFAULT_PVID]) {
  785. __u16 defpvid = nla_get_u16(data[IFLA_BR_VLAN_DEFAULT_PVID]);
  786. err = __br_vlan_set_default_pvid(br, defpvid);
  787. if (err)
  788. return err;
  789. }
  790. if (data[IFLA_BR_VLAN_STATS_ENABLED]) {
  791. __u8 vlan_stats = nla_get_u8(data[IFLA_BR_VLAN_STATS_ENABLED]);
  792. err = br_vlan_set_stats(br, vlan_stats);
  793. if (err)
  794. return err;
  795. }
  796. #endif
  797. if (data[IFLA_BR_GROUP_FWD_MASK]) {
  798. u16 fwd_mask = nla_get_u16(data[IFLA_BR_GROUP_FWD_MASK]);
  799. if (fwd_mask & BR_GROUPFWD_RESTRICTED)
  800. return -EINVAL;
  801. br->group_fwd_mask = fwd_mask;
  802. }
  803. if (data[IFLA_BR_GROUP_ADDR]) {
  804. u8 new_addr[ETH_ALEN];
  805. if (nla_len(data[IFLA_BR_GROUP_ADDR]) != ETH_ALEN)
  806. return -EINVAL;
  807. memcpy(new_addr, nla_data(data[IFLA_BR_GROUP_ADDR]), ETH_ALEN);
  808. if (!is_link_local_ether_addr(new_addr))
  809. return -EINVAL;
  810. if (new_addr[5] == 1 || /* 802.3x Pause address */
  811. new_addr[5] == 2 || /* 802.3ad Slow protocols */
  812. new_addr[5] == 3) /* 802.1X PAE address */
  813. return -EINVAL;
  814. spin_lock_bh(&br->lock);
  815. memcpy(br->group_addr, new_addr, sizeof(br->group_addr));
  816. spin_unlock_bh(&br->lock);
  817. br->group_addr_set = true;
  818. br_recalculate_fwd_mask(br);
  819. }
  820. if (data[IFLA_BR_FDB_FLUSH])
  821. br_fdb_flush(br);
  822. #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
  823. if (data[IFLA_BR_MCAST_ROUTER]) {
  824. u8 multicast_router = nla_get_u8(data[IFLA_BR_MCAST_ROUTER]);
  825. err = br_multicast_set_router(br, multicast_router);
  826. if (err)
  827. return err;
  828. }
  829. if (data[IFLA_BR_MCAST_SNOOPING]) {
  830. u8 mcast_snooping = nla_get_u8(data[IFLA_BR_MCAST_SNOOPING]);
  831. err = br_multicast_toggle(br, mcast_snooping);
  832. if (err)
  833. return err;
  834. }
  835. if (data[IFLA_BR_MCAST_QUERY_USE_IFADDR]) {
  836. u8 val;
  837. val = nla_get_u8(data[IFLA_BR_MCAST_QUERY_USE_IFADDR]);
  838. br->multicast_query_use_ifaddr = !!val;
  839. }
  840. if (data[IFLA_BR_MCAST_QUERIER]) {
  841. u8 mcast_querier = nla_get_u8(data[IFLA_BR_MCAST_QUERIER]);
  842. err = br_multicast_set_querier(br, mcast_querier);
  843. if (err)
  844. return err;
  845. }
  846. if (data[IFLA_BR_MCAST_HASH_ELASTICITY]) {
  847. u32 val = nla_get_u32(data[IFLA_BR_MCAST_HASH_ELASTICITY]);
  848. br->hash_elasticity = val;
  849. }
  850. if (data[IFLA_BR_MCAST_HASH_MAX]) {
  851. u32 hash_max = nla_get_u32(data[IFLA_BR_MCAST_HASH_MAX]);
  852. err = br_multicast_set_hash_max(br, hash_max);
  853. if (err)
  854. return err;
  855. }
  856. if (data[IFLA_BR_MCAST_LAST_MEMBER_CNT]) {
  857. u32 val = nla_get_u32(data[IFLA_BR_MCAST_LAST_MEMBER_CNT]);
  858. br->multicast_last_member_count = val;
  859. }
  860. if (data[IFLA_BR_MCAST_STARTUP_QUERY_CNT]) {
  861. u32 val = nla_get_u32(data[IFLA_BR_MCAST_STARTUP_QUERY_CNT]);
  862. br->multicast_startup_query_count = val;
  863. }
  864. if (data[IFLA_BR_MCAST_LAST_MEMBER_INTVL]) {
  865. u64 val = nla_get_u64(data[IFLA_BR_MCAST_LAST_MEMBER_INTVL]);
  866. br->multicast_last_member_interval = clock_t_to_jiffies(val);
  867. }
  868. if (data[IFLA_BR_MCAST_MEMBERSHIP_INTVL]) {
  869. u64 val = nla_get_u64(data[IFLA_BR_MCAST_MEMBERSHIP_INTVL]);
  870. br->multicast_membership_interval = clock_t_to_jiffies(val);
  871. }
  872. if (data[IFLA_BR_MCAST_QUERIER_INTVL]) {
  873. u64 val = nla_get_u64(data[IFLA_BR_MCAST_QUERIER_INTVL]);
  874. br->multicast_querier_interval = clock_t_to_jiffies(val);
  875. }
  876. if (data[IFLA_BR_MCAST_QUERY_INTVL]) {
  877. u64 val = nla_get_u64(data[IFLA_BR_MCAST_QUERY_INTVL]);
  878. br->multicast_query_interval = clock_t_to_jiffies(val);
  879. }
  880. if (data[IFLA_BR_MCAST_QUERY_RESPONSE_INTVL]) {
  881. u64 val = nla_get_u64(data[IFLA_BR_MCAST_QUERY_RESPONSE_INTVL]);
  882. br->multicast_query_response_interval = clock_t_to_jiffies(val);
  883. }
  884. if (data[IFLA_BR_MCAST_STARTUP_QUERY_INTVL]) {
  885. u64 val = nla_get_u64(data[IFLA_BR_MCAST_STARTUP_QUERY_INTVL]);
  886. br->multicast_startup_query_interval = clock_t_to_jiffies(val);
  887. }
  888. if (data[IFLA_BR_MCAST_STATS_ENABLED]) {
  889. __u8 mcast_stats;
  890. mcast_stats = nla_get_u8(data[IFLA_BR_MCAST_STATS_ENABLED]);
  891. br->multicast_stats_enabled = !!mcast_stats;
  892. }
  893. if (data[IFLA_BR_MCAST_IGMP_VERSION]) {
  894. __u8 igmp_version;
  895. igmp_version = nla_get_u8(data[IFLA_BR_MCAST_IGMP_VERSION]);
  896. err = br_multicast_set_igmp_version(br, igmp_version);
  897. if (err)
  898. return err;
  899. }
  900. #if IS_ENABLED(CONFIG_IPV6)
  901. if (data[IFLA_BR_MCAST_MLD_VERSION]) {
  902. __u8 mld_version;
  903. mld_version = nla_get_u8(data[IFLA_BR_MCAST_MLD_VERSION]);
  904. err = br_multicast_set_mld_version(br, mld_version);
  905. if (err)
  906. return err;
  907. }
  908. #endif
  909. #endif
  910. #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
  911. if (data[IFLA_BR_NF_CALL_IPTABLES]) {
  912. u8 val = nla_get_u8(data[IFLA_BR_NF_CALL_IPTABLES]);
  913. br->nf_call_iptables = val ? true : false;
  914. }
  915. if (data[IFLA_BR_NF_CALL_IP6TABLES]) {
  916. u8 val = nla_get_u8(data[IFLA_BR_NF_CALL_IP6TABLES]);
  917. br->nf_call_ip6tables = val ? true : false;
  918. }
  919. if (data[IFLA_BR_NF_CALL_ARPTABLES]) {
  920. u8 val = nla_get_u8(data[IFLA_BR_NF_CALL_ARPTABLES]);
  921. br->nf_call_arptables = val ? true : false;
  922. }
  923. #endif
  924. return 0;
  925. }
  926. static int br_dev_newlink(struct net *src_net, struct net_device *dev,
  927. struct nlattr *tb[], struct nlattr *data[])
  928. {
  929. struct net_bridge *br = netdev_priv(dev);
  930. int err;
  931. if (tb[IFLA_ADDRESS]) {
  932. spin_lock_bh(&br->lock);
  933. br_stp_change_bridge_id(br, nla_data(tb[IFLA_ADDRESS]));
  934. spin_unlock_bh(&br->lock);
  935. }
  936. err = br_changelink(dev, tb, data);
  937. if (err)
  938. return err;
  939. return register_netdevice(dev);
  940. }
  941. static size_t br_get_size(const struct net_device *brdev)
  942. {
  943. return nla_total_size(sizeof(u32)) + /* IFLA_BR_FORWARD_DELAY */
  944. nla_total_size(sizeof(u32)) + /* IFLA_BR_HELLO_TIME */
  945. nla_total_size(sizeof(u32)) + /* IFLA_BR_MAX_AGE */
  946. nla_total_size(sizeof(u32)) + /* IFLA_BR_AGEING_TIME */
  947. nla_total_size(sizeof(u32)) + /* IFLA_BR_STP_STATE */
  948. nla_total_size(sizeof(u16)) + /* IFLA_BR_PRIORITY */
  949. nla_total_size(sizeof(u8)) + /* IFLA_BR_VLAN_FILTERING */
  950. #ifdef CONFIG_BRIDGE_VLAN_FILTERING
  951. nla_total_size(sizeof(__be16)) + /* IFLA_BR_VLAN_PROTOCOL */
  952. nla_total_size(sizeof(u16)) + /* IFLA_BR_VLAN_DEFAULT_PVID */
  953. nla_total_size(sizeof(u8)) + /* IFLA_BR_VLAN_STATS_ENABLED */
  954. #endif
  955. nla_total_size(sizeof(u16)) + /* IFLA_BR_GROUP_FWD_MASK */
  956. nla_total_size(sizeof(struct ifla_bridge_id)) + /* IFLA_BR_ROOT_ID */
  957. nla_total_size(sizeof(struct ifla_bridge_id)) + /* IFLA_BR_BRIDGE_ID */
  958. nla_total_size(sizeof(u16)) + /* IFLA_BR_ROOT_PORT */
  959. nla_total_size(sizeof(u32)) + /* IFLA_BR_ROOT_PATH_COST */
  960. nla_total_size(sizeof(u8)) + /* IFLA_BR_TOPOLOGY_CHANGE */
  961. nla_total_size(sizeof(u8)) + /* IFLA_BR_TOPOLOGY_CHANGE_DETECTED */
  962. nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_HELLO_TIMER */
  963. nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_TCN_TIMER */
  964. nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_TOPOLOGY_CHANGE_TIMER */
  965. nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_GC_TIMER */
  966. nla_total_size(ETH_ALEN) + /* IFLA_BR_GROUP_ADDR */
  967. #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
  968. nla_total_size(sizeof(u8)) + /* IFLA_BR_MCAST_ROUTER */
  969. nla_total_size(sizeof(u8)) + /* IFLA_BR_MCAST_SNOOPING */
  970. nla_total_size(sizeof(u8)) + /* IFLA_BR_MCAST_QUERY_USE_IFADDR */
  971. nla_total_size(sizeof(u8)) + /* IFLA_BR_MCAST_QUERIER */
  972. nla_total_size(sizeof(u8)) + /* IFLA_BR_MCAST_STATS_ENABLED */
  973. nla_total_size(sizeof(u32)) + /* IFLA_BR_MCAST_HASH_ELASTICITY */
  974. nla_total_size(sizeof(u32)) + /* IFLA_BR_MCAST_HASH_MAX */
  975. nla_total_size(sizeof(u32)) + /* IFLA_BR_MCAST_LAST_MEMBER_CNT */
  976. nla_total_size(sizeof(u32)) + /* IFLA_BR_MCAST_STARTUP_QUERY_CNT */
  977. nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_MCAST_LAST_MEMBER_INTVL */
  978. nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_MCAST_MEMBERSHIP_INTVL */
  979. nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_MCAST_QUERIER_INTVL */
  980. nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_MCAST_QUERY_INTVL */
  981. nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_MCAST_QUERY_RESPONSE_INTVL */
  982. nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_MCAST_STARTUP_QUERY_INTVL */
  983. nla_total_size(sizeof(u8)) + /* IFLA_BR_MCAST_IGMP_VERSION */
  984. nla_total_size(sizeof(u8)) + /* IFLA_BR_MCAST_MLD_VERSION */
  985. #endif
  986. #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
  987. nla_total_size(sizeof(u8)) + /* IFLA_BR_NF_CALL_IPTABLES */
  988. nla_total_size(sizeof(u8)) + /* IFLA_BR_NF_CALL_IP6TABLES */
  989. nla_total_size(sizeof(u8)) + /* IFLA_BR_NF_CALL_ARPTABLES */
  990. #endif
  991. 0;
  992. }
  993. static int br_fill_info(struct sk_buff *skb, const struct net_device *brdev)
  994. {
  995. struct net_bridge *br = netdev_priv(brdev);
  996. u32 forward_delay = jiffies_to_clock_t(br->forward_delay);
  997. u32 hello_time = jiffies_to_clock_t(br->hello_time);
  998. u32 age_time = jiffies_to_clock_t(br->max_age);
  999. u32 ageing_time = jiffies_to_clock_t(br->ageing_time);
  1000. u32 stp_enabled = br->stp_enabled;
  1001. u16 priority = (br->bridge_id.prio[0] << 8) | br->bridge_id.prio[1];
  1002. u8 vlan_enabled = br_vlan_enabled(br);
  1003. u64 clockval;
  1004. clockval = br_timer_value(&br->hello_timer);
  1005. if (nla_put_u64_64bit(skb, IFLA_BR_HELLO_TIMER, clockval, IFLA_BR_PAD))
  1006. return -EMSGSIZE;
  1007. clockval = br_timer_value(&br->tcn_timer);
  1008. if (nla_put_u64_64bit(skb, IFLA_BR_TCN_TIMER, clockval, IFLA_BR_PAD))
  1009. return -EMSGSIZE;
  1010. clockval = br_timer_value(&br->topology_change_timer);
  1011. if (nla_put_u64_64bit(skb, IFLA_BR_TOPOLOGY_CHANGE_TIMER, clockval,
  1012. IFLA_BR_PAD))
  1013. return -EMSGSIZE;
  1014. clockval = br_timer_value(&br->gc_timer);
  1015. if (nla_put_u64_64bit(skb, IFLA_BR_GC_TIMER, clockval, IFLA_BR_PAD))
  1016. return -EMSGSIZE;
  1017. if (nla_put_u32(skb, IFLA_BR_FORWARD_DELAY, forward_delay) ||
  1018. nla_put_u32(skb, IFLA_BR_HELLO_TIME, hello_time) ||
  1019. nla_put_u32(skb, IFLA_BR_MAX_AGE, age_time) ||
  1020. nla_put_u32(skb, IFLA_BR_AGEING_TIME, ageing_time) ||
  1021. nla_put_u32(skb, IFLA_BR_STP_STATE, stp_enabled) ||
  1022. nla_put_u16(skb, IFLA_BR_PRIORITY, priority) ||
  1023. nla_put_u8(skb, IFLA_BR_VLAN_FILTERING, vlan_enabled) ||
  1024. nla_put_u16(skb, IFLA_BR_GROUP_FWD_MASK, br->group_fwd_mask) ||
  1025. nla_put(skb, IFLA_BR_BRIDGE_ID, sizeof(struct ifla_bridge_id),
  1026. &br->bridge_id) ||
  1027. nla_put(skb, IFLA_BR_ROOT_ID, sizeof(struct ifla_bridge_id),
  1028. &br->designated_root) ||
  1029. nla_put_u16(skb, IFLA_BR_ROOT_PORT, br->root_port) ||
  1030. nla_put_u32(skb, IFLA_BR_ROOT_PATH_COST, br->root_path_cost) ||
  1031. nla_put_u8(skb, IFLA_BR_TOPOLOGY_CHANGE, br->topology_change) ||
  1032. nla_put_u8(skb, IFLA_BR_TOPOLOGY_CHANGE_DETECTED,
  1033. br->topology_change_detected) ||
  1034. nla_put(skb, IFLA_BR_GROUP_ADDR, ETH_ALEN, br->group_addr))
  1035. return -EMSGSIZE;
  1036. #ifdef CONFIG_BRIDGE_VLAN_FILTERING
  1037. if (nla_put_be16(skb, IFLA_BR_VLAN_PROTOCOL, br->vlan_proto) ||
  1038. nla_put_u16(skb, IFLA_BR_VLAN_DEFAULT_PVID, br->default_pvid) ||
  1039. nla_put_u8(skb, IFLA_BR_VLAN_STATS_ENABLED, br->vlan_stats_enabled))
  1040. return -EMSGSIZE;
  1041. #endif
  1042. #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
  1043. if (nla_put_u8(skb, IFLA_BR_MCAST_ROUTER, br->multicast_router) ||
  1044. nla_put_u8(skb, IFLA_BR_MCAST_SNOOPING, !br->multicast_disabled) ||
  1045. nla_put_u8(skb, IFLA_BR_MCAST_QUERY_USE_IFADDR,
  1046. br->multicast_query_use_ifaddr) ||
  1047. nla_put_u8(skb, IFLA_BR_MCAST_QUERIER, br->multicast_querier) ||
  1048. nla_put_u8(skb, IFLA_BR_MCAST_STATS_ENABLED,
  1049. br->multicast_stats_enabled) ||
  1050. nla_put_u32(skb, IFLA_BR_MCAST_HASH_ELASTICITY,
  1051. br->hash_elasticity) ||
  1052. nla_put_u32(skb, IFLA_BR_MCAST_HASH_MAX, br->hash_max) ||
  1053. nla_put_u32(skb, IFLA_BR_MCAST_LAST_MEMBER_CNT,
  1054. br->multicast_last_member_count) ||
  1055. nla_put_u32(skb, IFLA_BR_MCAST_STARTUP_QUERY_CNT,
  1056. br->multicast_startup_query_count) ||
  1057. nla_put_u8(skb, IFLA_BR_MCAST_IGMP_VERSION,
  1058. br->multicast_igmp_version))
  1059. return -EMSGSIZE;
  1060. #if IS_ENABLED(CONFIG_IPV6)
  1061. if (nla_put_u8(skb, IFLA_BR_MCAST_MLD_VERSION,
  1062. br->multicast_mld_version))
  1063. return -EMSGSIZE;
  1064. #endif
  1065. clockval = jiffies_to_clock_t(br->multicast_last_member_interval);
  1066. if (nla_put_u64_64bit(skb, IFLA_BR_MCAST_LAST_MEMBER_INTVL, clockval,
  1067. IFLA_BR_PAD))
  1068. return -EMSGSIZE;
  1069. clockval = jiffies_to_clock_t(br->multicast_membership_interval);
  1070. if (nla_put_u64_64bit(skb, IFLA_BR_MCAST_MEMBERSHIP_INTVL, clockval,
  1071. IFLA_BR_PAD))
  1072. return -EMSGSIZE;
  1073. clockval = jiffies_to_clock_t(br->multicast_querier_interval);
  1074. if (nla_put_u64_64bit(skb, IFLA_BR_MCAST_QUERIER_INTVL, clockval,
  1075. IFLA_BR_PAD))
  1076. return -EMSGSIZE;
  1077. clockval = jiffies_to_clock_t(br->multicast_query_interval);
  1078. if (nla_put_u64_64bit(skb, IFLA_BR_MCAST_QUERY_INTVL, clockval,
  1079. IFLA_BR_PAD))
  1080. return -EMSGSIZE;
  1081. clockval = jiffies_to_clock_t(br->multicast_query_response_interval);
  1082. if (nla_put_u64_64bit(skb, IFLA_BR_MCAST_QUERY_RESPONSE_INTVL, clockval,
  1083. IFLA_BR_PAD))
  1084. return -EMSGSIZE;
  1085. clockval = jiffies_to_clock_t(br->multicast_startup_query_interval);
  1086. if (nla_put_u64_64bit(skb, IFLA_BR_MCAST_STARTUP_QUERY_INTVL, clockval,
  1087. IFLA_BR_PAD))
  1088. return -EMSGSIZE;
  1089. #endif
  1090. #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
  1091. if (nla_put_u8(skb, IFLA_BR_NF_CALL_IPTABLES,
  1092. br->nf_call_iptables ? 1 : 0) ||
  1093. nla_put_u8(skb, IFLA_BR_NF_CALL_IP6TABLES,
  1094. br->nf_call_ip6tables ? 1 : 0) ||
  1095. nla_put_u8(skb, IFLA_BR_NF_CALL_ARPTABLES,
  1096. br->nf_call_arptables ? 1 : 0))
  1097. return -EMSGSIZE;
  1098. #endif
  1099. return 0;
  1100. }
  1101. static size_t br_get_linkxstats_size(const struct net_device *dev, int attr)
  1102. {
  1103. struct net_bridge_port *p = NULL;
  1104. struct net_bridge_vlan_group *vg;
  1105. struct net_bridge_vlan *v;
  1106. struct net_bridge *br;
  1107. int numvls = 0;
  1108. switch (attr) {
  1109. case IFLA_STATS_LINK_XSTATS:
  1110. br = netdev_priv(dev);
  1111. vg = br_vlan_group(br);
  1112. break;
  1113. case IFLA_STATS_LINK_XSTATS_SLAVE:
  1114. p = br_port_get_rtnl(dev);
  1115. if (!p)
  1116. return 0;
  1117. br = p->br;
  1118. vg = nbp_vlan_group(p);
  1119. break;
  1120. default:
  1121. return 0;
  1122. }
  1123. if (vg) {
  1124. /* we need to count all, even placeholder entries */
  1125. list_for_each_entry(v, &vg->vlan_list, vlist)
  1126. numvls++;
  1127. }
  1128. return numvls * nla_total_size(sizeof(struct bridge_vlan_xstats)) +
  1129. nla_total_size(sizeof(struct br_mcast_stats)) +
  1130. nla_total_size(0);
  1131. }
  1132. static int br_fill_linkxstats(struct sk_buff *skb,
  1133. const struct net_device *dev,
  1134. int *prividx, int attr)
  1135. {
  1136. struct nlattr *nla __maybe_unused;
  1137. struct net_bridge_port *p = NULL;
  1138. struct net_bridge_vlan_group *vg;
  1139. struct net_bridge_vlan *v;
  1140. struct net_bridge *br;
  1141. struct nlattr *nest;
  1142. int vl_idx = 0;
  1143. switch (attr) {
  1144. case IFLA_STATS_LINK_XSTATS:
  1145. br = netdev_priv(dev);
  1146. vg = br_vlan_group(br);
  1147. break;
  1148. case IFLA_STATS_LINK_XSTATS_SLAVE:
  1149. p = br_port_get_rtnl(dev);
  1150. if (!p)
  1151. return 0;
  1152. br = p->br;
  1153. vg = nbp_vlan_group(p);
  1154. break;
  1155. default:
  1156. return -EINVAL;
  1157. }
  1158. nest = nla_nest_start(skb, LINK_XSTATS_TYPE_BRIDGE);
  1159. if (!nest)
  1160. return -EMSGSIZE;
  1161. if (vg) {
  1162. u16 pvid;
  1163. pvid = br_get_pvid(vg);
  1164. list_for_each_entry(v, &vg->vlan_list, vlist) {
  1165. struct bridge_vlan_xstats vxi;
  1166. struct br_vlan_stats stats;
  1167. if (++vl_idx < *prividx)
  1168. continue;
  1169. memset(&vxi, 0, sizeof(vxi));
  1170. vxi.vid = v->vid;
  1171. vxi.flags = v->flags;
  1172. if (v->vid == pvid)
  1173. vxi.flags |= BRIDGE_VLAN_INFO_PVID;
  1174. br_vlan_get_stats(v, &stats);
  1175. vxi.rx_bytes = stats.rx_bytes;
  1176. vxi.rx_packets = stats.rx_packets;
  1177. vxi.tx_bytes = stats.tx_bytes;
  1178. vxi.tx_packets = stats.tx_packets;
  1179. if (nla_put(skb, BRIDGE_XSTATS_VLAN, sizeof(vxi), &vxi))
  1180. goto nla_put_failure;
  1181. }
  1182. }
  1183. #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
  1184. if (++vl_idx >= *prividx) {
  1185. nla = nla_reserve_64bit(skb, BRIDGE_XSTATS_MCAST,
  1186. sizeof(struct br_mcast_stats),
  1187. BRIDGE_XSTATS_PAD);
  1188. if (!nla)
  1189. goto nla_put_failure;
  1190. br_multicast_get_stats(br, p, nla_data(nla));
  1191. }
  1192. #endif
  1193. nla_nest_end(skb, nest);
  1194. *prividx = 0;
  1195. return 0;
  1196. nla_put_failure:
  1197. nla_nest_end(skb, nest);
  1198. *prividx = vl_idx;
  1199. return -EMSGSIZE;
  1200. }
  1201. static struct rtnl_af_ops br_af_ops __read_mostly = {
  1202. .family = AF_BRIDGE,
  1203. .get_link_af_size = br_get_link_af_size_filtered,
  1204. };
  1205. struct rtnl_link_ops br_link_ops __read_mostly = {
  1206. .kind = "bridge",
  1207. .priv_size = sizeof(struct net_bridge),
  1208. .setup = br_dev_setup,
  1209. .maxtype = IFLA_BR_MAX,
  1210. .policy = br_policy,
  1211. .validate = br_validate,
  1212. .newlink = br_dev_newlink,
  1213. .changelink = br_changelink,
  1214. .dellink = br_dev_delete,
  1215. .get_size = br_get_size,
  1216. .fill_info = br_fill_info,
  1217. .fill_linkxstats = br_fill_linkxstats,
  1218. .get_linkxstats_size = br_get_linkxstats_size,
  1219. .slave_maxtype = IFLA_BRPORT_MAX,
  1220. .slave_policy = br_port_policy,
  1221. .slave_changelink = br_port_slave_changelink,
  1222. .get_slave_size = br_port_get_slave_size,
  1223. .fill_slave_info = br_port_fill_slave_info,
  1224. };
  1225. int __init br_netlink_init(void)
  1226. {
  1227. int err;
  1228. br_mdb_init();
  1229. rtnl_af_register(&br_af_ops);
  1230. err = rtnl_link_register(&br_link_ops);
  1231. if (err)
  1232. goto out_af;
  1233. return 0;
  1234. out_af:
  1235. rtnl_af_unregister(&br_af_ops);
  1236. br_mdb_uninit();
  1237. return err;
  1238. }
  1239. void br_netlink_fini(void)
  1240. {
  1241. br_mdb_uninit();
  1242. rtnl_af_unregister(&br_af_ops);
  1243. rtnl_link_unregister(&br_link_ops);
  1244. }