Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 1 | /* Copyright 2011-2014 Autronica Fire and Security AS |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 2 | * |
| 3 | * This program is free software; you can redistribute it and/or modify it |
| 4 | * under the terms of the GNU General Public License as published by the Free |
| 5 | * Software Foundation; either version 2 of the License, or (at your option) |
| 6 | * any later version. |
| 7 | * |
| 8 | * Author(s): |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 9 | * 2011-2014 Arvid Brodin, arvid.brodin@alten.se |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 10 | */ |
| 11 | |
| 12 | #include <linux/netdevice.h> |
| 13 | #include <linux/rculist.h> |
| 14 | #include <linux/timer.h> |
| 15 | #include <linux/etherdevice.h> |
| 16 | #include "hsr_main.h" |
| 17 | #include "hsr_device.h" |
| 18 | #include "hsr_netlink.h" |
| 19 | #include "hsr_framereg.h" |
Arvid Brodin | 51f3c60 | 2014-07-04 23:37:27 +0200 | [diff] [blame^] | 20 | #include "hsr_slave.h" |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 21 | |
| 22 | |
| 23 | /* List of all registered virtual HSR devices */ |
| 24 | static LIST_HEAD(hsr_list); |
| 25 | |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 26 | void register_hsr_master(struct hsr_priv *hsr) |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 27 | { |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 28 | list_add_tail_rcu(&hsr->hsr_list, &hsr_list); |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 29 | } |
| 30 | |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 31 | void unregister_hsr_master(struct hsr_priv *hsr) |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 32 | { |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 33 | struct hsr_priv *hsr_it; |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 34 | |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 35 | list_for_each_entry(hsr_it, &hsr_list, hsr_list) |
| 36 | if (hsr_it == hsr) { |
| 37 | list_del_rcu(&hsr_it->hsr_list); |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 38 | return; |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | bool is_hsr_slave(struct net_device *dev) |
| 43 | { |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 44 | struct hsr_priv *hsr_it; |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 45 | |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 46 | list_for_each_entry_rcu(hsr_it, &hsr_list, hsr_list) { |
| 47 | if (dev == hsr_it->slave[0]) |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 48 | return true; |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 49 | if (dev == hsr_it->slave[1]) |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 50 | return true; |
| 51 | } |
| 52 | |
| 53 | return false; |
| 54 | } |
| 55 | |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 56 | /* If dev is a HSR slave device, return the virtual master device. Return NULL |
| 57 | * otherwise. |
| 58 | */ |
Arvid Brodin | 81ba6af | 2014-07-04 23:35:24 +0200 | [diff] [blame] | 59 | struct hsr_priv *get_hsr_master(struct net_device *dev) |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 60 | { |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 61 | struct hsr_priv *hsr; |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 62 | |
| 63 | rcu_read_lock(); |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 64 | list_for_each_entry_rcu(hsr, &hsr_list, hsr_list) |
| 65 | if ((dev == hsr->slave[0]) || |
| 66 | (dev == hsr->slave[1])) { |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 67 | rcu_read_unlock(); |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 68 | return hsr; |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | rcu_read_unlock(); |
| 72 | return NULL; |
| 73 | } |
| 74 | |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 75 | /* If dev is a HSR slave device, return the other slave device. Return NULL |
| 76 | * otherwise. |
| 77 | */ |
Arvid Brodin | 81ba6af | 2014-07-04 23:35:24 +0200 | [diff] [blame] | 78 | struct net_device *get_other_slave(struct hsr_priv *hsr, |
| 79 | struct net_device *dev) |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 80 | { |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 81 | if (dev == hsr->slave[0]) |
| 82 | return hsr->slave[1]; |
| 83 | if (dev == hsr->slave[1]) |
| 84 | return hsr->slave[0]; |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 85 | |
| 86 | return NULL; |
| 87 | } |
| 88 | |
| 89 | |
| 90 | static int hsr_netdev_notify(struct notifier_block *nb, unsigned long event, |
| 91 | void *ptr) |
| 92 | { |
| 93 | struct net_device *slave, *other_slave; |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 94 | struct hsr_priv *hsr; |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 95 | int mtu_max; |
| 96 | int res; |
| 97 | struct net_device *dev; |
| 98 | |
| 99 | dev = netdev_notifier_info_to_dev(ptr); |
| 100 | |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 101 | hsr = get_hsr_master(dev); |
| 102 | if (hsr) { |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 103 | /* dev is a slave device */ |
| 104 | slave = dev; |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 105 | other_slave = get_other_slave(hsr, slave); |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 106 | } else { |
| 107 | if (!is_hsr_master(dev)) |
| 108 | return NOTIFY_DONE; |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 109 | hsr = netdev_priv(dev); |
| 110 | slave = hsr->slave[0]; |
| 111 | other_slave = hsr->slave[1]; |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | switch (event) { |
| 115 | case NETDEV_UP: /* Administrative state DOWN */ |
| 116 | case NETDEV_DOWN: /* Administrative state UP */ |
| 117 | case NETDEV_CHANGE: /* Link (carrier) state changes */ |
Arvid Brodin | e9aae56 | 2014-07-04 23:36:40 +0200 | [diff] [blame] | 118 | hsr_check_carrier_and_operstate(hsr); |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 119 | break; |
| 120 | case NETDEV_CHANGEADDR: |
| 121 | |
| 122 | /* This should not happen since there's no ndo_set_mac_address() |
| 123 | * for HSR devices - i.e. not supported. |
| 124 | */ |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 125 | if (dev == hsr->dev) |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 126 | break; |
| 127 | |
Arvid Brodin | 51f3c60 | 2014-07-04 23:37:27 +0200 | [diff] [blame^] | 128 | if (dev == hsr->slave[0]) { |
| 129 | ether_addr_copy(hsr->dev->dev_addr, dev->dev_addr); |
| 130 | call_netdevice_notifiers(NETDEV_CHANGEADDR, hsr->dev); |
| 131 | } |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 132 | |
| 133 | /* Make sure we recognize frames from ourselves in hsr_rcv() */ |
Arvid Brodin | 51f3c60 | 2014-07-04 23:37:27 +0200 | [diff] [blame^] | 134 | other_slave = hsr->slave[1]; |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 135 | res = hsr_create_self_node(&hsr->self_node_db, |
| 136 | hsr->dev->dev_addr, |
Arvid Brodin | 51f3c60 | 2014-07-04 23:37:27 +0200 | [diff] [blame^] | 137 | other_slave ? |
| 138 | other_slave->dev_addr : |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 139 | hsr->dev->dev_addr); |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 140 | if (res) |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 141 | netdev_warn(hsr->dev, |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 142 | "Could not update HSR node address.\n"); |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 143 | break; |
| 144 | case NETDEV_CHANGEMTU: |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 145 | if (dev == hsr->dev) |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 146 | break; /* Handled in ndo_change_mtu() */ |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 147 | mtu_max = hsr_get_max_mtu(hsr); |
| 148 | if (hsr->dev->mtu > mtu_max) |
| 149 | dev_set_mtu(hsr->dev, mtu_max); |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 150 | break; |
| 151 | case NETDEV_UNREGISTER: |
Arvid Brodin | 51f3c60 | 2014-07-04 23:37:27 +0200 | [diff] [blame^] | 152 | if (dev == hsr->slave[0]) { |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 153 | hsr->slave[0] = NULL; |
Arvid Brodin | 51f3c60 | 2014-07-04 23:37:27 +0200 | [diff] [blame^] | 154 | hsr_del_slave(hsr, 0); |
| 155 | } |
| 156 | if (dev == hsr->slave[1]) { |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 157 | hsr->slave[1] = NULL; |
Arvid Brodin | 51f3c60 | 2014-07-04 23:37:27 +0200 | [diff] [blame^] | 158 | hsr_del_slave(hsr, 1); |
| 159 | } |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 160 | |
| 161 | /* There should really be a way to set a new slave device... */ |
| 162 | |
| 163 | break; |
| 164 | case NETDEV_PRE_TYPE_CHANGE: |
| 165 | /* HSR works only on Ethernet devices. Refuse slave to change |
| 166 | * its type. |
| 167 | */ |
| 168 | return NOTIFY_BAD; |
| 169 | } |
| 170 | |
| 171 | return NOTIFY_DONE; |
| 172 | } |
| 173 | |
| 174 | |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 175 | static struct notifier_block hsr_nb = { |
| 176 | .notifier_call = hsr_netdev_notify, /* Slave event notifications */ |
| 177 | }; |
| 178 | |
| 179 | |
| 180 | static int __init hsr_init(void) |
| 181 | { |
| 182 | int res; |
| 183 | |
Arvid Brodin | 70ebe4a | 2014-07-04 23:34:38 +0200 | [diff] [blame] | 184 | BUILD_BUG_ON(sizeof(struct hsr_tag) != HSR_HLEN); |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 185 | |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 186 | register_netdevice_notifier(&hsr_nb); |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 187 | res = hsr_netlink_init(); |
| 188 | |
| 189 | return res; |
| 190 | } |
| 191 | |
| 192 | static void __exit hsr_exit(void) |
| 193 | { |
| 194 | unregister_netdevice_notifier(&hsr_nb); |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 195 | hsr_netlink_exit(); |
Arvid Brodin | f421436 | 2013-10-30 21:10:47 +0100 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | module_init(hsr_init); |
| 199 | module_exit(hsr_exit); |
| 200 | MODULE_LICENSE("GPL"); |