History

net: ipv6: Make address flushing on ifdown optional

Currently, all ipv6 addresses are flushed when the interface is configured down, including global, static addresses: $ ip -6 addr show dev eth1 3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 state UP qlen 1000 inet6 2100:1::2/120 scope global valid_lft forever preferred_lft forever inet6 fe80::e0:f9ff:fe79:34bd/64 scope link valid_lft forever preferred_lft forever $ ip link set dev eth1 down $ ip -6 addr show dev eth1 << nothing; all addresses have been flushed>> Add a new sysctl to make this behavior optional. The new setting defaults to flush all addresses to maintain backwards compatibility. When the set global addresses with no expire times are not flushed on an admin down. The sysctl is per-interface or system-wide for all interfaces $ sysctl -w net.ipv6.conf.eth1.keep_addr_on_down=1 or $ sysctl -w net.ipv6.conf.all.keep_addr_on_down=1 Will keep addresses on eth1 on an admin down. $ ip -6 addr show dev eth1 3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 state UP qlen 1000 inet6 2100:1::2/120 scope global valid_lft forever preferred_lft forever inet6 fe80::e0:f9ff:fe79:34bd/64 scope link valid_lft forever preferred_lft forever $ ip link set dev eth1 down $ ip -6 addr show dev eth1 3: eth1: <BROADCAST,MULTICAST> mtu 1500 state DOWN qlen 1000 inet6 2100:1::2/120 scope global tentative valid_lft forever preferred_lft forever inet6 fe80::e0:f9ff:fe79:34bd/64 scope link tentative valid_lft forever preferred_lft forever

David Ahern committed 10 years ago

Commit f1705ec1 parent 619b1744 files · +132 −15Browse files

Unified · Split

4 files
  1. Documentation/networking/ip-sysctl.txt +9 −0
  2. include/linux/ipv6.h +1 −0
  3. include/uapi/linux/ipv6.h +1 −0
  4. net/ipv6/addrconf.c +121 −15
Documentation/networking/ip-sysctl.txt+9−0
@@ -1563,6 +1563,15 @@ temp_prefered_lft - INTEGER · 1562 unmodified lines
1563 Preferred lifetime (in seconds) for temporary addresses.1563 Preferred lifetime (in seconds) for temporary addresses.
1564 Default: 86400 (1 day)1564 Default: 86400 (1 day)
1565 1565
1566keep_addr_on_down - INTEGER
1567 Keep all IPv6 addresses on an interface down event. If set static
1568 global addresses with no expiration time are not flushed.
1569 >0 : enabled
1570 0 : system default
1571 <0 : disabled
1572
1573 Default: 0 (addresses are removed)
1574
1566max_desync_factor - INTEGER1575max_desync_factor - INTEGER
1567 Maximum value for DESYNC_FACTOR, which is a random value1576 Maximum value for DESYNC_FACTOR, which is a random value
1568 that ensures that clients don't synchronize with each1577 that ensures that clients don't synchronize with each
include/linux/ipv6.h+1−0
@@ -62,6 +62,7 @@ struct ipv6_devconf { · 61 unmodified lines
62 struct in6_addr secret;62 struct in6_addr secret;
63 } stable_secret;63 } stable_secret;
64 __s32 use_oif_addrs_only;64 __s32 use_oif_addrs_only;
65 __s32 keep_addr_on_down;
65 void *sysctl;66 void *sysctl;
66};67};
67 68
include/uapi/linux/ipv6.h+1−0
@@ -176,6 +176,7 @@ enum { · 175 unmodified lines
176 DEVCONF_IGNORE_ROUTES_WITH_LINKDOWN,176 DEVCONF_IGNORE_ROUTES_WITH_LINKDOWN,
177 DEVCONF_DROP_UNICAST_IN_L2_MULTICAST,177 DEVCONF_DROP_UNICAST_IN_L2_MULTICAST,
178 DEVCONF_DROP_UNSOLICITED_NA,178 DEVCONF_DROP_UNSOLICITED_NA,
179 DEVCONF_KEEP_ADDR_ON_DOWN,
179 DEVCONF_MAX180 DEVCONF_MAX
180};181};
181 182
net/ipv6/addrconf.c+121−15
@@ -216,6 +216,7 @@ static struct ipv6_devconf ipv6_devconf __read_mostly = { · 215 unmodified lines
216 },216 },
217 .use_oif_addrs_only = 0,217 .use_oif_addrs_only = 0,
218 .ignore_routes_with_linkdown = 0,218 .ignore_routes_with_linkdown = 0,
219 .keep_addr_on_down = 0,
219};220};
220 221
221static struct ipv6_devconf ipv6_devconf_dflt __read_mostly = {222static struct ipv6_devconf ipv6_devconf_dflt __read_mostly = {
@@ -260,6 +261,7 @@ static struct ipv6_devconf ipv6_devconf_dflt __read_mostly = { · 38 unmodified lines
260 },261 },
261 .use_oif_addrs_only = 0,262 .use_oif_addrs_only = 0,
262 .ignore_routes_with_linkdown = 0,263 .ignore_routes_with_linkdown = 0,
264 .keep_addr_on_down = 0,
263};265};
264 266
265/* Check if a valid qdisc is available */267/* Check if a valid qdisc is available */
@@ -3168,6 +3170,55 @@ static void addrconf_gre_config(struct net_device *dev) · 2902 unmodified lines
3168}3170}
3169#endif3171#endif
3170 3172
3173static int fixup_permanent_addr(struct inet6_dev *idev,
3174 struct inet6_ifaddr *ifp)
3175{
3176 if (!ifp->rt) {
3177 struct rt6_info *rt;
3178
3179 rt = addrconf_dst_alloc(idev, &ifp->addr, false);
3180 if (unlikely(IS_ERR(rt)))
3181 return PTR_ERR(rt);
3182
3183 ifp->rt = rt;
3184 }
3185
3186 if (!(ifp->flags & IFA_F_NOPREFIXROUTE)) {
3187 addrconf_prefix_route(&ifp->addr, ifp->prefix_len,
3188 idev->dev, 0, 0);
3189 }
3190
3191 addrconf_dad_start(ifp);
3192
3193 return 0;
3194}
3195
3196static void addrconf_permanent_addr(struct net_device *dev)
3197{
3198 struct inet6_ifaddr *ifp, *tmp;
3199 struct inet6_dev *idev;
3200
3201 idev = __in6_dev_get(dev);
3202 if (!idev)
3203 return;
3204
3205 write_lock_bh(&idev->lock);
3206
3207 list_for_each_entry_safe(ifp, tmp, &idev->addr_list, if_list) {
3208 if ((ifp->flags & IFA_F_PERMANENT) &&
3209 fixup_permanent_addr(idev, ifp) < 0) {
3210 write_unlock_bh(&idev->lock);
3211 ipv6_del_addr(ifp);
3212 write_lock_bh(&idev->lock);
3213
3214 net_info_ratelimited("%s: Failed to add prefix route for address %pI6c; dropping\n",
3215 idev->dev->name, &ifp->addr);
3216 }
3217 }
3218
3219 write_unlock_bh(&idev->lock);
3220}
3221
3171static int addrconf_notify(struct notifier_block *this, unsigned long event,3222static int addrconf_notify(struct notifier_block *this, unsigned long event,
3172 void *ptr)3223 void *ptr)
3173{3224{
@@ -3253,6 +3304,9 @@ static int addrconf_notify(struct notifier_block *this, unsigned long event, · 79 unmodified lines
3253 run_pending = 1;3304 run_pending = 1;
3254 }3305 }
3255 3306
3307 /* restore routes for permanent addresses */
3308 addrconf_permanent_addr(dev);
3309
3256 switch (dev->type) {3310 switch (dev->type) {
3257#if IS_ENABLED(CONFIG_IPV6_SIT)3311#if IS_ENABLED(CONFIG_IPV6_SIT)
3258 case ARPHRD_SIT:3312 case ARPHRD_SIT:
@@ -3356,7 +3410,10 @@ static int addrconf_ifdown(struct net_device *dev, int how) · 97 unmodified lines
3356{3410{
3357 struct net *net = dev_net(dev);3411 struct net *net = dev_net(dev);
3358 struct inet6_dev *idev;3412 struct inet6_dev *idev;
3359 struct inet6_ifaddr *ifa;3413 struct inet6_ifaddr *ifa, *tmp;
3414 struct list_head del_list;
3415 int _keep_addr;
3416 bool keep_addr;
3360 int state, i;3417 int state, i;
3361 3418
3362 ASSERT_RTNL();3419 ASSERT_RTNL();
@@ -3383,6 +3440,16 @@ static int addrconf_ifdown(struct net_device *dev, int how) · 20 unmodified lines
3383 3440
3384 }3441 }
3385 3442
3443 /* aggregate the system setting and interface setting */
3444 _keep_addr = net->ipv6.devconf_all->keep_addr_on_down;
3445 if (!_keep_addr)
3446 _keep_addr = idev->cnf.keep_addr_on_down;
3447
3448 /* combine the user config with event to determine if permanent
3449 * addresses are to be removed from address hash table
3450 */
3451 keep_addr = !(how || _keep_addr <= 0);
3452
3386 /* Step 2: clear hash table */3453 /* Step 2: clear hash table */
3387 for (i = 0; i < IN6_ADDR_HSIZE; i++) {3454 for (i = 0; i < IN6_ADDR_HSIZE; i++) {
3388 struct hlist_head *h = &inet6_addr_lst[i];3455 struct hlist_head *h = &inet6_addr_lst[i];
@@ -3391,9 +3458,15 @@ static int addrconf_ifdown(struct net_device *dev, int how) · 2 unmodified lines
3391restart:3458restart:
3392 hlist_for_each_entry_rcu(ifa, h, addr_lst) {3459 hlist_for_each_entry_rcu(ifa, h, addr_lst) {
3393 if (ifa->idev == idev) {3460 if (ifa->idev == idev) {
3394 hlist_del_init_rcu(&ifa->addr_lst);
3395 addrconf_del_dad_work(ifa);3461 addrconf_del_dad_work(ifa);
3396 goto restart;3462 /* combined flag + permanent flag decide if
3463 * address is retained on a down event
3464 */
3465 if (!keep_addr ||
3466 !(ifa->flags & IFA_F_PERMANENT)) {
3467 hlist_del_init_rcu(&ifa->addr_lst);
3468 goto restart;
3469 }
3397 }3470 }
3398 }3471 }
3399 spin_unlock_bh(&addrconf_hash_lock);3472 spin_unlock_bh(&addrconf_hash_lock);
@@ -3427,31 +3500,53 @@ restart: · 27 unmodified lines
3427 write_lock_bh(&idev->lock);3500 write_lock_bh(&idev->lock);
3428 }3501 }
3429 3502
3430 while (!list_empty(&idev->addr_list)) {3503 /* re-combine the user config with event to determine if permanent
3431 ifa = list_first_entry(&idev->addr_list,3504 * addresses are to be removed from the interface list
3432 struct inet6_ifaddr, if_list);3505 */
3433 addrconf_del_dad_work(ifa);3506 keep_addr = (!how && _keep_addr > 0);
3434 3507
3435 list_del(&ifa->if_list);3508 INIT_LIST_HEAD(&del_list);
3509 list_for_each_entry_safe(ifa, tmp, &idev->addr_list, if_list) {
3510 addrconf_del_dad_work(ifa);
3436 3511
3437 write_unlock_bh(&idev->lock);3512 write_unlock_bh(&idev->lock);
3438
3439 spin_lock_bh(&ifa->lock);3513 spin_lock_bh(&ifa->lock);
3440 state = ifa->state;3514
3441 ifa->state = INET6_IFADDR_STATE_DEAD;3515 if (keep_addr && (ifa->flags & IFA_F_PERMANENT)) {
3516 /* set state to skip the notifier below */
3517 state = INET6_IFADDR_STATE_DEAD;
3518 ifa->state = 0;
3519 if (!(ifa->flags & IFA_F_NODAD))
3520 ifa->flags |= IFA_F_TENTATIVE;
3521 } else {
3522 state = ifa->state;
3523 ifa->state = INET6_IFADDR_STATE_DEAD;
3524
3525 list_del(&ifa->if_list);
3526 list_add(&ifa->if_list, &del_list);
3527 }
3528
3442 spin_unlock_bh(&ifa->lock);3529 spin_unlock_bh(&ifa->lock);
3443 3530
3444 if (state != INET6_IFADDR_STATE_DEAD) {3531 if (state != INET6_IFADDR_STATE_DEAD) {
3445 __ipv6_ifa_notify(RTM_DELADDR, ifa);3532 __ipv6_ifa_notify(RTM_DELADDR, ifa);
3446 inet6addr_notifier_call_chain(NETDEV_DOWN, ifa);3533 inet6addr_notifier_call_chain(NETDEV_DOWN, ifa);
3447 }3534 }
3448 in6_ifa_put(ifa);
3449 3535
3450 write_lock_bh(&idev->lock);3536 write_lock_bh(&idev->lock);
3451 }3537 }
3452 3538
3453 write_unlock_bh(&idev->lock);3539 write_unlock_bh(&idev->lock);
3454 3540
3541 /* now clean up addresses to be removed */
3542 while (!list_empty(&del_list)) {
3543 ifa = list_first_entry(&del_list,
3544 struct inet6_ifaddr, if_list);
3545 list_del(&ifa->if_list);
3546
3547 in6_ifa_put(ifa);
3548 }
3549
3455 /* Step 5: Discard anycast and multicast list */3550 /* Step 5: Discard anycast and multicast list */
3456 if (how) {3551 if (how) {
3457 ipv6_ac_destroy_dev(idev);3552 ipv6_ac_destroy_dev(idev);
@@ -4716,6 +4811,7 @@ static inline void ipv6_store_devconf(struct ipv6_devconf *cnf, · 1258 unmodified lines
4716 array[DEVCONF_USE_OIF_ADDRS_ONLY] = cnf->use_oif_addrs_only;4811 array[DEVCONF_USE_OIF_ADDRS_ONLY] = cnf->use_oif_addrs_only;
4717 array[DEVCONF_DROP_UNICAST_IN_L2_MULTICAST] = cnf->drop_unicast_in_l2_multicast;4812 array[DEVCONF_DROP_UNICAST_IN_L2_MULTICAST] = cnf->drop_unicast_in_l2_multicast;
4718 array[DEVCONF_DROP_UNSOLICITED_NA] = cnf->drop_unsolicited_na;4813 array[DEVCONF_DROP_UNSOLICITED_NA] = cnf->drop_unsolicited_na;
4814 array[DEVCONF_KEEP_ADDR_ON_DOWN] = cnf->keep_addr_on_down;
4719}4815}
4720 4816
4721static inline size_t inet6_ifla6_size(void)4817static inline size_t inet6_ifla6_size(void)
@@ -5197,10 +5293,12 @@ static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp) · 475 unmodified lines
5197 if (rt)5293 if (rt)
5198 ip6_del_rt(rt);5294 ip6_del_rt(rt);
5199 }5295 }
5200 dst_hold(&ifp->rt->dst);5296 if (ifp->rt) {
5201 5297 dst_hold(&ifp->rt->dst);
5202 ip6_del_rt(ifp->rt);
5203 5298
5299 ip6_del_rt(ifp->rt);
5300 ifp->rt = NULL;
5301 }
5204 rt_genid_bump_ipv6(net);5302 rt_genid_bump_ipv6(net);
5205 break;5303 break;
5206 }5304 }
@@ -5803,6 +5901,14 @@ static struct addrconf_sysctl_table · 596 unmodified lines
5803 .mode = 0644,5901 .mode = 0644,
5804 .proc_handler = proc_dointvec,5902 .proc_handler = proc_dointvec,
5805 },5903 },
5904 {
5905 .procname = "keep_addr_on_down",
5906 .data = &ipv6_devconf.keep_addr_on_down,
5907 .maxlen = sizeof(int),
5908 .mode = 0644,
5909 .proc_handler = proc_dointvec,
5910
5911 },
5806 {5912 {
5807 /* sentinel */5913 /* sentinel */
5808 }5914 }

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t