hsr_device.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright 2011-2014 Autronica Fire and Security AS
  3. *
  4. * Author(s):
  5. * 2011-2014 Arvid Brodin, arvid.brodin@alten.se
  6. * This file contains device methods for creating, using and destroying
  7. * virtual HSR or PRP devices.
  8. */
  9. #include <linux/netdevice.h>
  10. #include <linux/skbuff.h>
  11. #include <linux/etherdevice.h>
  12. #include <linux/rtnetlink.h>
  13. #include <linux/pkt_sched.h>
  14. #include "hsr_device.h"
  15. #include "hsr_slave.h"
  16. #include "hsr_framereg.h"
  17. #include "hsr_main.h"
  18. #include "hsr_forward.h"
  19. static bool is_admin_up(struct net_device *dev)
  20. {
  21. return dev && (dev->flags & IFF_UP);
  22. }
  23. static bool is_slave_up(struct net_device *dev)
  24. {
  25. return dev && is_admin_up(dev) && netif_oper_up(dev);
  26. }
  27. static void hsr_set_operstate(struct hsr_port *master, bool has_carrier)
  28. {
  29. struct net_device *dev = master->dev;
  30. if (!is_admin_up(dev)) {
  31. netdev_set_operstate(dev, IF_OPER_DOWN);
  32. return;
  33. }
  34. if (has_carrier)
  35. netdev_set_operstate(dev, IF_OPER_UP);
  36. else
  37. netdev_set_operstate(dev, IF_OPER_LOWERLAYERDOWN);
  38. }
  39. static bool hsr_check_carrier(struct hsr_port *master)
  40. {
  41. struct hsr_port *port;
  42. ASSERT_RTNL();
  43. hsr_for_each_port(master->hsr, port) {
  44. if (port->type != HSR_PT_MASTER && is_slave_up(port->dev)) {
  45. netif_carrier_on(master->dev);
  46. return true;
  47. }
  48. }
  49. netif_carrier_off(master->dev);
  50. return false;
  51. }
  52. static void hsr_check_announce(struct net_device *hsr_dev)
  53. {
  54. struct hsr_priv *hsr;
  55. hsr = netdev_priv(hsr_dev);
  56. if (netif_running(hsr_dev) && netif_oper_up(hsr_dev)) {
  57. /* Enable announce timer and start sending supervisory frames */
  58. if (!timer_pending(&hsr->announce_timer)) {
  59. hsr->announce_count = 0;
  60. mod_timer(&hsr->announce_timer, jiffies +
  61. msecs_to_jiffies(HSR_ANNOUNCE_INTERVAL));
  62. }
  63. if (hsr->redbox && !timer_pending(&hsr->announce_proxy_timer))
  64. mod_timer(&hsr->announce_proxy_timer, jiffies +
  65. msecs_to_jiffies(HSR_ANNOUNCE_INTERVAL) / 2);
  66. } else {
  67. /* Deactivate the announce timer */
  68. timer_delete(&hsr->announce_timer);
  69. if (hsr->redbox)
  70. timer_delete(&hsr->announce_proxy_timer);
  71. }
  72. }
  73. void hsr_check_carrier_and_operstate(struct hsr_priv *hsr)
  74. {
  75. struct hsr_port *master;
  76. bool has_carrier;
  77. master = hsr_port_get_hsr(hsr, HSR_PT_MASTER);
  78. /* netif_stacked_transfer_operstate() cannot be used here since
  79. * it doesn't set IF_OPER_LOWERLAYERDOWN (?)
  80. */
  81. has_carrier = hsr_check_carrier(master);
  82. hsr_set_operstate(master, has_carrier);
  83. hsr_check_announce(master->dev);
  84. }
  85. int hsr_get_max_mtu(struct hsr_priv *hsr)
  86. {
  87. unsigned int mtu_max;
  88. struct hsr_port *port;
  89. mtu_max = ETH_DATA_LEN;
  90. hsr_for_each_port(hsr, port)
  91. if (port->type != HSR_PT_MASTER)
  92. mtu_max = min(port->dev->mtu, mtu_max);
  93. if (mtu_max < HSR_HLEN)
  94. return 0;
  95. return mtu_max - HSR_HLEN;
  96. }
  97. static int hsr_dev_change_mtu(struct net_device *dev, int new_mtu)
  98. {
  99. struct hsr_priv *hsr;
  100. hsr = netdev_priv(dev);
  101. if (new_mtu > hsr_get_max_mtu(hsr)) {
  102. netdev_info(dev, "A HSR master's MTU cannot be greater than the smallest MTU of its slaves minus the HSR Tag length (%d octets).\n",
  103. HSR_HLEN);
  104. return -EINVAL;
  105. }
  106. WRITE_ONCE(dev->mtu, new_mtu);
  107. return 0;
  108. }
  109. static int hsr_dev_open(struct net_device *dev)
  110. {
  111. struct hsr_priv *hsr;
  112. struct hsr_port *port;
  113. const char *designation = NULL;
  114. hsr = netdev_priv(dev);
  115. hsr_for_each_port(hsr, port) {
  116. if (port->type == HSR_PT_MASTER)
  117. continue;
  118. switch (port->type) {
  119. case HSR_PT_SLAVE_A:
  120. designation = "Slave A";
  121. break;
  122. case HSR_PT_SLAVE_B:
  123. designation = "Slave B";
  124. break;
  125. case HSR_PT_INTERLINK:
  126. designation = "Interlink";
  127. break;
  128. default:
  129. designation = "Unknown";
  130. }
  131. if (!is_slave_up(port->dev))
  132. netdev_warn(dev, "%s (%s) is not up; please bring it up to get a fully working HSR network\n",
  133. designation, port->dev->name);
  134. }
  135. if (!designation)
  136. netdev_warn(dev, "No slave devices configured\n");
  137. return 0;
  138. }
  139. static int hsr_dev_close(struct net_device *dev)
  140. {
  141. struct hsr_port *port;
  142. struct hsr_priv *hsr;
  143. hsr = netdev_priv(dev);
  144. hsr_for_each_port(hsr, port) {
  145. if (port->type == HSR_PT_MASTER)
  146. continue;
  147. switch (port->type) {
  148. case HSR_PT_SLAVE_A:
  149. case HSR_PT_SLAVE_B:
  150. dev_uc_unsync(port->dev, dev);
  151. dev_mc_unsync(port->dev, dev);
  152. break;
  153. default:
  154. break;
  155. }
  156. }
  157. return 0;
  158. }
  159. static netdev_features_t hsr_features_recompute(struct hsr_priv *hsr,
  160. netdev_features_t features)
  161. {
  162. netdev_features_t mask;
  163. struct hsr_port *port;
  164. mask = features;
  165. /* Mask out all features that, if supported by one device, should be
  166. * enabled for all devices (see NETIF_F_ONE_FOR_ALL).
  167. *
  168. * Anything that's off in mask will not be enabled - so only things
  169. * that were in features originally, and also is in NETIF_F_ONE_FOR_ALL,
  170. * may become enabled.
  171. */
  172. features &= ~NETIF_F_ONE_FOR_ALL;
  173. hsr_for_each_port(hsr, port)
  174. features = netdev_increment_features(features,
  175. port->dev->features,
  176. mask);
  177. return features;
  178. }
  179. static netdev_features_t hsr_fix_features(struct net_device *dev,
  180. netdev_features_t features)
  181. {
  182. struct hsr_priv *hsr = netdev_priv(dev);
  183. return hsr_features_recompute(hsr, features);
  184. }
  185. static netdev_tx_t hsr_dev_xmit(struct sk_buff *skb, struct net_device *dev)
  186. {
  187. struct hsr_priv *hsr = netdev_priv(dev);
  188. struct hsr_port *master;
  189. master = hsr_port_get_hsr(hsr, HSR_PT_MASTER);
  190. if (master) {
  191. skb->dev = master->dev;
  192. skb_reset_mac_header(skb);
  193. skb_reset_mac_len(skb);
  194. spin_lock_bh(&hsr->seqnr_lock);
  195. hsr_forward_skb(skb, master);
  196. spin_unlock_bh(&hsr->seqnr_lock);
  197. } else {
  198. dev_core_stats_tx_dropped_inc(dev);
  199. dev_kfree_skb_any(skb);
  200. }
  201. return NETDEV_TX_OK;
  202. }
  203. static const struct header_ops hsr_header_ops = {
  204. .create = eth_header,
  205. .parse = eth_header_parse,
  206. };
  207. static struct sk_buff *hsr_init_skb(struct hsr_port *master, int extra)
  208. {
  209. struct hsr_priv *hsr = master->hsr;
  210. struct sk_buff *skb;
  211. int hlen, tlen;
  212. int len;
  213. hlen = LL_RESERVED_SPACE(master->dev);
  214. tlen = master->dev->needed_tailroom;
  215. len = sizeof(struct hsr_sup_tag) + sizeof(struct hsr_sup_payload);
  216. /* skb size is same for PRP/HSR frames, only difference
  217. * being, for PRP it is a trailer and for HSR it is a
  218. * header.
  219. * RedBox might use @extra more bytes.
  220. */
  221. skb = dev_alloc_skb(len + extra + hlen + tlen);
  222. if (!skb)
  223. return skb;
  224. skb_reserve(skb, hlen);
  225. skb->dev = master->dev;
  226. skb->priority = TC_PRIO_CONTROL;
  227. skb_reset_network_header(skb);
  228. skb_reset_transport_header(skb);
  229. if (dev_hard_header(skb, skb->dev, ETH_P_PRP,
  230. hsr->sup_multicast_addr,
  231. skb->dev->dev_addr, skb->len) <= 0)
  232. goto out;
  233. skb_reset_mac_header(skb);
  234. skb_reset_mac_len(skb);
  235. return skb;
  236. out:
  237. kfree_skb(skb);
  238. return NULL;
  239. }
  240. static void send_hsr_supervision_frame(struct hsr_port *port,
  241. unsigned long *interval,
  242. const unsigned char *addr)
  243. {
  244. struct hsr_priv *hsr = port->hsr;
  245. __u8 type = HSR_TLV_LIFE_CHECK;
  246. struct hsr_sup_payload *hsr_sp;
  247. struct hsr_sup_tlv *hsr_stlv;
  248. struct hsr_sup_tag *hsr_stag;
  249. struct sk_buff *skb;
  250. int extra = 0;
  251. *interval = msecs_to_jiffies(HSR_LIFE_CHECK_INTERVAL);
  252. if (hsr->announce_count < 3 && hsr->prot_version == 0) {
  253. type = HSR_TLV_ANNOUNCE;
  254. *interval = msecs_to_jiffies(HSR_ANNOUNCE_INTERVAL);
  255. hsr->announce_count++;
  256. }
  257. if (hsr->redbox)
  258. extra = sizeof(struct hsr_sup_tlv) +
  259. sizeof(struct hsr_sup_payload);
  260. skb = hsr_init_skb(port, extra);
  261. if (!skb) {
  262. netdev_warn_once(port->dev, "HSR: Could not send supervision frame\n");
  263. return;
  264. }
  265. hsr_stag = skb_put(skb, sizeof(struct hsr_sup_tag));
  266. set_hsr_stag_path(hsr_stag, (hsr->prot_version ? 0x0 : 0xf));
  267. set_hsr_stag_HSR_ver(hsr_stag, hsr->prot_version);
  268. /* From HSRv1 on we have separate supervision sequence numbers. */
  269. spin_lock_bh(&hsr->seqnr_lock);
  270. if (hsr->prot_version > 0) {
  271. hsr_stag->sequence_nr = htons(hsr->sup_sequence_nr);
  272. hsr->sup_sequence_nr++;
  273. } else {
  274. hsr_stag->sequence_nr = htons(hsr->sequence_nr);
  275. hsr->sequence_nr++;
  276. }
  277. hsr_stag->tlv.HSR_TLV_type = type;
  278. /* TODO: Why 12 in HSRv0? */
  279. hsr_stag->tlv.HSR_TLV_length = hsr->prot_version ?
  280. sizeof(struct hsr_sup_payload) : 12;
  281. /* Payload: MacAddressA / SAN MAC from ProxyNodeTable */
  282. hsr_sp = skb_put(skb, sizeof(struct hsr_sup_payload));
  283. ether_addr_copy(hsr_sp->macaddress_A, addr);
  284. if (hsr->redbox &&
  285. hsr_is_node_in_db(&hsr->proxy_node_db, addr)) {
  286. hsr_stlv = skb_put(skb, sizeof(struct hsr_sup_tlv));
  287. hsr_stlv->HSR_TLV_type = PRP_TLV_REDBOX_MAC;
  288. hsr_stlv->HSR_TLV_length = sizeof(struct hsr_sup_payload);
  289. /* Payload: MacAddressRedBox */
  290. hsr_sp = skb_put(skb, sizeof(struct hsr_sup_payload));
  291. ether_addr_copy(hsr_sp->macaddress_A, hsr->macaddress_redbox);
  292. }
  293. if (skb_put_padto(skb, ETH_ZLEN)) {
  294. spin_unlock_bh(&hsr->seqnr_lock);
  295. return;
  296. }
  297. hsr_forward_skb(skb, port);
  298. spin_unlock_bh(&hsr->seqnr_lock);
  299. return;
  300. }
  301. static void send_prp_supervision_frame(struct hsr_port *master,
  302. unsigned long *interval,
  303. const unsigned char *addr)
  304. {
  305. struct hsr_priv *hsr = master->hsr;
  306. struct hsr_sup_payload *hsr_sp;
  307. struct hsr_sup_tag *hsr_stag;
  308. struct sk_buff *skb;
  309. skb = hsr_init_skb(master, 0);
  310. if (!skb) {
  311. netdev_warn_once(master->dev, "PRP: Could not send supervision frame\n");
  312. return;
  313. }
  314. *interval = msecs_to_jiffies(HSR_LIFE_CHECK_INTERVAL);
  315. hsr_stag = skb_put(skb, sizeof(struct hsr_sup_tag));
  316. set_hsr_stag_path(hsr_stag, (hsr->prot_version ? 0x0 : 0xf));
  317. set_hsr_stag_HSR_ver(hsr_stag, (hsr->prot_version ? 1 : 0));
  318. /* From HSRv1 on we have separate supervision sequence numbers. */
  319. spin_lock_bh(&hsr->seqnr_lock);
  320. hsr_stag->sequence_nr = htons(hsr->sup_sequence_nr);
  321. hsr->sup_sequence_nr++;
  322. hsr_stag->tlv.HSR_TLV_type = PRP_TLV_LIFE_CHECK_DD;
  323. hsr_stag->tlv.HSR_TLV_length = sizeof(struct hsr_sup_payload);
  324. /* Payload: MacAddressA */
  325. hsr_sp = skb_put(skb, sizeof(struct hsr_sup_payload));
  326. ether_addr_copy(hsr_sp->macaddress_A, master->dev->dev_addr);
  327. if (skb_put_padto(skb, ETH_ZLEN)) {
  328. spin_unlock_bh(&hsr->seqnr_lock);
  329. return;
  330. }
  331. hsr_forward_skb(skb, master);
  332. spin_unlock_bh(&hsr->seqnr_lock);
  333. }
  334. /* Announce (supervision frame) timer function
  335. */
  336. static void hsr_announce(struct timer_list *t)
  337. {
  338. struct hsr_priv *hsr;
  339. struct hsr_port *master;
  340. unsigned long interval;
  341. hsr = from_timer(hsr, t, announce_timer);
  342. rcu_read_lock();
  343. master = hsr_port_get_hsr(hsr, HSR_PT_MASTER);
  344. hsr->proto_ops->send_sv_frame(master, &interval, master->dev->dev_addr);
  345. if (is_admin_up(master->dev))
  346. mod_timer(&hsr->announce_timer, jiffies + interval);
  347. rcu_read_unlock();
  348. }
  349. /* Announce (supervision frame) timer function for RedBox
  350. */
  351. static void hsr_proxy_announce(struct timer_list *t)
  352. {
  353. struct hsr_priv *hsr = from_timer(hsr, t, announce_proxy_timer);
  354. struct hsr_port *interlink;
  355. unsigned long interval = 0;
  356. struct hsr_node *node;
  357. rcu_read_lock();
  358. /* RedBOX sends supervisory frames to HSR network with MAC addresses
  359. * of SAN nodes stored in ProxyNodeTable.
  360. */
  361. interlink = hsr_port_get_hsr(hsr, HSR_PT_INTERLINK);
  362. if (!interlink)
  363. goto done;
  364. list_for_each_entry_rcu(node, &hsr->proxy_node_db, mac_list) {
  365. if (hsr_addr_is_redbox(hsr, node->macaddress_A))
  366. continue;
  367. hsr->proto_ops->send_sv_frame(interlink, &interval,
  368. node->macaddress_A);
  369. }
  370. if (is_admin_up(interlink->dev)) {
  371. if (!interval)
  372. interval = msecs_to_jiffies(HSR_ANNOUNCE_INTERVAL);
  373. mod_timer(&hsr->announce_proxy_timer, jiffies + interval);
  374. }
  375. done:
  376. rcu_read_unlock();
  377. }
  378. void hsr_del_ports(struct hsr_priv *hsr)
  379. {
  380. struct hsr_port *port;
  381. port = hsr_port_get_hsr(hsr, HSR_PT_SLAVE_A);
  382. if (port)
  383. hsr_del_port(port);
  384. port = hsr_port_get_hsr(hsr, HSR_PT_SLAVE_B);
  385. if (port)
  386. hsr_del_port(port);
  387. port = hsr_port_get_hsr(hsr, HSR_PT_INTERLINK);
  388. if (port)
  389. hsr_del_port(port);
  390. port = hsr_port_get_hsr(hsr, HSR_PT_MASTER);
  391. if (port)
  392. hsr_del_port(port);
  393. }
  394. static void hsr_set_rx_mode(struct net_device *dev)
  395. {
  396. struct hsr_port *port;
  397. struct hsr_priv *hsr;
  398. hsr = netdev_priv(dev);
  399. hsr_for_each_port(hsr, port) {
  400. if (port->type == HSR_PT_MASTER)
  401. continue;
  402. switch (port->type) {
  403. case HSR_PT_SLAVE_A:
  404. case HSR_PT_SLAVE_B:
  405. dev_mc_sync_multiple(port->dev, dev);
  406. dev_uc_sync_multiple(port->dev, dev);
  407. break;
  408. default:
  409. break;
  410. }
  411. }
  412. }
  413. static void hsr_change_rx_flags(struct net_device *dev, int change)
  414. {
  415. struct hsr_port *port;
  416. struct hsr_priv *hsr;
  417. hsr = netdev_priv(dev);
  418. hsr_for_each_port(hsr, port) {
  419. if (port->type == HSR_PT_MASTER)
  420. continue;
  421. switch (port->type) {
  422. case HSR_PT_SLAVE_A:
  423. case HSR_PT_SLAVE_B:
  424. if (change & IFF_ALLMULTI)
  425. dev_set_allmulti(port->dev,
  426. dev->flags &
  427. IFF_ALLMULTI ? 1 : -1);
  428. break;
  429. default:
  430. break;
  431. }
  432. }
  433. }
  434. static const struct net_device_ops hsr_device_ops = {
  435. .ndo_change_mtu = hsr_dev_change_mtu,
  436. .ndo_open = hsr_dev_open,
  437. .ndo_stop = hsr_dev_close,
  438. .ndo_start_xmit = hsr_dev_xmit,
  439. .ndo_change_rx_flags = hsr_change_rx_flags,
  440. .ndo_fix_features = hsr_fix_features,
  441. .ndo_set_rx_mode = hsr_set_rx_mode,
  442. };
  443. static const struct device_type hsr_type = {
  444. .name = "hsr",
  445. };
  446. static struct hsr_proto_ops hsr_ops = {
  447. .send_sv_frame = send_hsr_supervision_frame,
  448. .create_tagged_frame = hsr_create_tagged_frame,
  449. .get_untagged_frame = hsr_get_untagged_frame,
  450. .drop_frame = hsr_drop_frame,
  451. .fill_frame_info = hsr_fill_frame_info,
  452. .invalid_dan_ingress_frame = hsr_invalid_dan_ingress_frame,
  453. };
  454. static struct hsr_proto_ops prp_ops = {
  455. .send_sv_frame = send_prp_supervision_frame,
  456. .create_tagged_frame = prp_create_tagged_frame,
  457. .get_untagged_frame = prp_get_untagged_frame,
  458. .drop_frame = prp_drop_frame,
  459. .fill_frame_info = prp_fill_frame_info,
  460. .handle_san_frame = prp_handle_san_frame,
  461. .update_san_info = prp_update_san_info,
  462. };
  463. void hsr_dev_setup(struct net_device *dev)
  464. {
  465. eth_hw_addr_random(dev);
  466. ether_setup(dev);
  467. dev->min_mtu = 0;
  468. dev->header_ops = &hsr_header_ops;
  469. dev->netdev_ops = &hsr_device_ops;
  470. SET_NETDEV_DEVTYPE(dev, &hsr_type);
  471. dev->priv_flags |= IFF_NO_QUEUE | IFF_DISABLE_NETPOLL;
  472. /* Prevent recursive tx locking */
  473. dev->lltx = true;
  474. /* Not sure about this. Taken from bridge code. netdevice.h says
  475. * it means "Does not change network namespaces".
  476. */
  477. dev->netns_local = true;
  478. dev->needs_free_netdev = true;
  479. dev->hw_features = NETIF_F_SG | NETIF_F_FRAGLIST | NETIF_F_HIGHDMA |
  480. NETIF_F_GSO_MASK | NETIF_F_HW_CSUM |
  481. NETIF_F_HW_VLAN_CTAG_TX;
  482. dev->features = dev->hw_features;
  483. /* VLAN on top of HSR needs testing and probably some work on
  484. * hsr_header_create() etc.
  485. */
  486. dev->features |= NETIF_F_VLAN_CHALLENGED;
  487. }
  488. /* Return true if dev is a HSR master; return false otherwise.
  489. */
  490. bool is_hsr_master(struct net_device *dev)
  491. {
  492. return (dev->netdev_ops->ndo_start_xmit == hsr_dev_xmit);
  493. }
  494. EXPORT_SYMBOL(is_hsr_master);
  495. /* Default multicast address for HSR Supervision frames */
  496. static const unsigned char def_multicast_addr[ETH_ALEN] __aligned(2) = {
  497. 0x01, 0x15, 0x4e, 0x00, 0x01, 0x00
  498. };
  499. int hsr_dev_finalize(struct net_device *hsr_dev, struct net_device *slave[2],
  500. struct net_device *interlink, unsigned char multicast_spec,
  501. u8 protocol_version, struct netlink_ext_ack *extack)
  502. {
  503. bool unregister = false;
  504. struct hsr_priv *hsr;
  505. int res;
  506. hsr = netdev_priv(hsr_dev);
  507. INIT_LIST_HEAD(&hsr->ports);
  508. INIT_LIST_HEAD(&hsr->node_db);
  509. INIT_LIST_HEAD(&hsr->proxy_node_db);
  510. spin_lock_init(&hsr->list_lock);
  511. eth_hw_addr_set(hsr_dev, slave[0]->dev_addr);
  512. /* initialize protocol specific functions */
  513. if (protocol_version == PRP_V1) {
  514. /* For PRP, lan_id has most significant 3 bits holding
  515. * the net_id of PRP_LAN_ID
  516. */
  517. hsr->net_id = PRP_LAN_ID << 1;
  518. hsr->proto_ops = &prp_ops;
  519. } else {
  520. hsr->proto_ops = &hsr_ops;
  521. }
  522. /* Make sure we recognize frames from ourselves in hsr_rcv() */
  523. res = hsr_create_self_node(hsr, hsr_dev->dev_addr,
  524. slave[1]->dev_addr);
  525. if (res < 0)
  526. return res;
  527. spin_lock_init(&hsr->seqnr_lock);
  528. /* Overflow soon to find bugs easier: */
  529. hsr->sequence_nr = HSR_SEQNR_START;
  530. hsr->sup_sequence_nr = HSR_SUP_SEQNR_START;
  531. timer_setup(&hsr->announce_timer, hsr_announce, 0);
  532. timer_setup(&hsr->prune_timer, hsr_prune_nodes, 0);
  533. timer_setup(&hsr->prune_proxy_timer, hsr_prune_proxy_nodes, 0);
  534. timer_setup(&hsr->announce_proxy_timer, hsr_proxy_announce, 0);
  535. ether_addr_copy(hsr->sup_multicast_addr, def_multicast_addr);
  536. hsr->sup_multicast_addr[ETH_ALEN - 1] = multicast_spec;
  537. hsr->prot_version = protocol_version;
  538. /* Make sure the 1st call to netif_carrier_on() gets through */
  539. netif_carrier_off(hsr_dev);
  540. res = hsr_add_port(hsr, hsr_dev, HSR_PT_MASTER, extack);
  541. if (res)
  542. goto err_add_master;
  543. /* HSR forwarding offload supported in lower device? */
  544. if ((slave[0]->features & NETIF_F_HW_HSR_FWD) &&
  545. (slave[1]->features & NETIF_F_HW_HSR_FWD))
  546. hsr->fwd_offloaded = true;
  547. res = register_netdevice(hsr_dev);
  548. if (res)
  549. goto err_unregister;
  550. unregister = true;
  551. res = hsr_add_port(hsr, slave[0], HSR_PT_SLAVE_A, extack);
  552. if (res)
  553. goto err_unregister;
  554. res = hsr_add_port(hsr, slave[1], HSR_PT_SLAVE_B, extack);
  555. if (res)
  556. goto err_unregister;
  557. if (interlink) {
  558. res = hsr_add_port(hsr, interlink, HSR_PT_INTERLINK, extack);
  559. if (res)
  560. goto err_unregister;
  561. hsr->redbox = true;
  562. ether_addr_copy(hsr->macaddress_redbox, interlink->dev_addr);
  563. mod_timer(&hsr->prune_proxy_timer,
  564. jiffies + msecs_to_jiffies(PRUNE_PROXY_PERIOD));
  565. }
  566. hsr_debugfs_init(hsr, hsr_dev);
  567. mod_timer(&hsr->prune_timer, jiffies + msecs_to_jiffies(PRUNE_PERIOD));
  568. return 0;
  569. err_unregister:
  570. hsr_del_ports(hsr);
  571. err_add_master:
  572. hsr_del_self_node(hsr);
  573. if (unregister)
  574. unregister_netdevice(hsr_dev);
  575. return res;
  576. }