1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * NET3 IP device support routines. 4 * 5 * Derived from the IP parts of dev.c 1.0.19 6 * Authors: Ross Biro 7 * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG> 8 * Mark Evans, <evansmp@uhura.aston.ac.uk> 9 * 10 * Additional Authors: 11 * Alan Cox, <gw4pts@gw4pts.ampr.org> 12 * Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru> 13 * 14 * Changes: 15 * Alexey Kuznetsov: pa_* fields are replaced with ifaddr 16 * lists. 17 * Cyrus Durgin: updated for kmod 18 * Matthias Andree: in devinet_ioctl, compare label and 19 * address (4.4BSD alias style support), 20 * fall back to comparing just the label 21 * if no match found. 22 */ 23 24 25 #include <linux/uaccess.h> 26 #include <linux/bitops.h> 27 #include <linux/capability.h> 28 #include <linux/module.h> 29 #include <linux/types.h> 30 #include <linux/kernel.h> 31 #include <linux/sched/signal.h> 32 #include <linux/string.h> 33 #include <linux/mm.h> 34 #include <linux/socket.h> 35 #include <linux/sockios.h> 36 #include <linux/in.h> 37 #include <linux/errno.h> 38 #include <linux/interrupt.h> 39 #include <linux/if_addr.h> 40 #include <linux/if_ether.h> 41 #include <linux/inet.h> 42 #include <linux/netdevice.h> 43 #include <linux/etherdevice.h> 44 #include <linux/skbuff.h> 45 #include <linux/init.h> 46 #include <linux/notifier.h> 47 #include <linux/inetdevice.h> 48 #include <linux/igmp.h> 49 #include "igmp_internal.h" 50 #include <linux/slab.h> 51 #include <linux/hash.h> 52 #ifdef CONFIG_SYSCTL 53 #include <linux/sysctl.h> 54 #endif 55 #include <linux/kmod.h> 56 #include <linux/netconf.h> 57 58 #include <net/arp.h> 59 #include <net/ip.h> 60 #include <net/route.h> 61 #include <net/ip_fib.h> 62 #include <net/rtnetlink.h> 63 #include <net/net_namespace.h> 64 #include <net/addrconf.h> 65 66 #define IPV6ONLY_FLAGS \ 67 (IFA_F_NODAD | IFA_F_OPTIMISTIC | IFA_F_DADFAILED | \ 68 IFA_F_HOMEADDRESS | IFA_F_TENTATIVE | \ 69 IFA_F_MANAGETEMPADDR | IFA_F_STABLE_PRIVACY) 70 71 static struct ipv4_devconf ipv4_devconf = { 72 .data = { 73 [IPV4_DEVCONF_ACCEPT_REDIRECTS - 1] = 1, 74 [IPV4_DEVCONF_SEND_REDIRECTS - 1] = 1, 75 [IPV4_DEVCONF_SECURE_REDIRECTS - 1] = 1, 76 [IPV4_DEVCONF_SHARED_MEDIA - 1] = 1, 77 [IPV4_DEVCONF_IGMPV2_UNSOLICITED_REPORT_INTERVAL - 1] = 10000 /*ms*/, 78 [IPV4_DEVCONF_IGMPV3_UNSOLICITED_REPORT_INTERVAL - 1] = 1000 /*ms*/, 79 [IPV4_DEVCONF_ARP_EVICT_NOCARRIER - 1] = 1, 80 }, 81 }; 82 83 static struct ipv4_devconf ipv4_devconf_dflt = { 84 .data = { 85 [IPV4_DEVCONF_ACCEPT_REDIRECTS - 1] = 1, 86 [IPV4_DEVCONF_SEND_REDIRECTS - 1] = 1, 87 [IPV4_DEVCONF_SECURE_REDIRECTS - 1] = 1, 88 [IPV4_DEVCONF_SHARED_MEDIA - 1] = 1, 89 [IPV4_DEVCONF_ACCEPT_SOURCE_ROUTE - 1] = 1, 90 [IPV4_DEVCONF_IGMPV2_UNSOLICITED_REPORT_INTERVAL - 1] = 10000 /*ms*/, 91 [IPV4_DEVCONF_IGMPV3_UNSOLICITED_REPORT_INTERVAL - 1] = 1000 /*ms*/, 92 [IPV4_DEVCONF_ARP_EVICT_NOCARRIER - 1] = 1, 93 }, 94 }; 95 96 #define IPV4_DEVCONF_DFLT(net, attr) \ 97 IPV4_DEVCONF((*net->ipv4.devconf_dflt), attr) 98 99 static const struct nla_policy ifa_ipv4_policy[IFA_MAX+1] = { 100 [IFA_LOCAL] = { .type = NLA_U32 }, 101 [IFA_ADDRESS] = { .type = NLA_U32 }, 102 [IFA_BROADCAST] = { .type = NLA_U32 }, 103 [IFA_LABEL] = { .type = NLA_STRING, .len = IFNAMSIZ - 1 }, 104 [IFA_CACHEINFO] = { .len = sizeof(struct ifa_cacheinfo) }, 105 [IFA_FLAGS] = { .type = NLA_U32 }, 106 [IFA_RT_PRIORITY] = { .type = NLA_U32 }, 107 [IFA_TARGET_NETNSID] = { .type = NLA_S32 }, 108 [IFA_PROTO] = { .type = NLA_U8 }, 109 }; 110 111 #define IN4_ADDR_HSIZE_SHIFT 8 112 #define IN4_ADDR_HSIZE (1U << IN4_ADDR_HSIZE_SHIFT) 113 114 static u32 inet_addr_hash(const struct net *net, __be32 addr) 115 { 116 u32 val = __ipv4_addr_hash(addr, net_hash_mix(net)); 117 118 return hash_32(val, IN4_ADDR_HSIZE_SHIFT); 119 } 120 121 static void inet_hash_insert(struct net *net, struct in_ifaddr *ifa) 122 { 123 u32 hash = inet_addr_hash(net, ifa->ifa_local); 124 125 ASSERT_RTNL(); 126 hlist_add_head_rcu(&ifa->addr_lst, &net->ipv4.inet_addr_lst[hash]); 127 } 128 129 static void inet_hash_remove(struct in_ifaddr *ifa) 130 { 131 ASSERT_RTNL(); 132 hlist_del_init_rcu(&ifa->addr_lst); 133 } 134 135 /** 136 * __ip_dev_find - find the first device with a given source address. 137 * @net: the net namespace 138 * @addr: the source address 139 * @devref: if true, take a reference on the found device 140 * 141 * If a caller uses devref=false, it should be protected by RCU, or RTNL 142 */ 143 struct net_device *__ip_dev_find(struct net *net, __be32 addr, bool devref) 144 { 145 struct net_device *result = NULL; 146 struct in_ifaddr *ifa; 147 148 rcu_read_lock(); 149 ifa = inet_lookup_ifaddr_rcu(net, addr); 150 if (!ifa) { 151 struct flowi4 fl4 = { .daddr = addr }; 152 struct fib_result res = { 0 }; 153 struct fib_table *local; 154 155 /* Fallback to FIB local table so that communication 156 * over loopback subnets work. 157 */ 158 local = fib_get_table(net, RT_TABLE_LOCAL); 159 if (local && 160 !fib_table_lookup(local, &fl4, &res, FIB_LOOKUP_NOREF) && 161 res.type == RTN_LOCAL) 162 result = FIB_RES_DEV(res); 163 } else { 164 result = ifa->ifa_dev->dev; 165 } 166 if (result && devref) 167 dev_hold(result); 168 rcu_read_unlock(); 169 return result; 170 } 171 EXPORT_SYMBOL(__ip_dev_find); 172 173 /* called under RCU lock */ 174 struct in_ifaddr *inet_lookup_ifaddr_rcu(struct net *net, __be32 addr) 175 { 176 u32 hash = inet_addr_hash(net, addr); 177 struct in_ifaddr *ifa; 178 179 hlist_for_each_entry_rcu(ifa, &net->ipv4.inet_addr_lst[hash], addr_lst) 180 if (ifa->ifa_local == addr) 181 return ifa; 182 183 return NULL; 184 } 185 186 static void rtmsg_ifa(int event, struct in_ifaddr *, struct nlmsghdr *, u32); 187 188 static BLOCKING_NOTIFIER_HEAD(inetaddr_chain); 189 static BLOCKING_NOTIFIER_HEAD(inetaddr_validator_chain); 190 static void inet_del_ifa(struct in_device *in_dev, 191 struct in_ifaddr __rcu **ifap, 192 int destroy); 193 #ifdef CONFIG_SYSCTL 194 static int devinet_sysctl_register(struct in_device *idev); 195 static void devinet_sysctl_unregister(struct in_device *idev); 196 #else 197 static int devinet_sysctl_register(struct in_device *idev) 198 { 199 return 0; 200 } 201 static void devinet_sysctl_unregister(struct in_device *idev) 202 { 203 } 204 #endif 205 206 /* Locks all the inet devices. */ 207 208 static struct in_ifaddr *inet_alloc_ifa(struct in_device *in_dev) 209 { 210 struct in_ifaddr *ifa; 211 212 ifa = kzalloc_obj(*ifa, GFP_KERNEL_ACCOUNT); 213 if (!ifa) 214 return NULL; 215 216 in_dev_hold(in_dev); 217 ifa->ifa_dev = in_dev; 218 219 INIT_HLIST_NODE(&ifa->addr_lst); 220 221 return ifa; 222 } 223 224 static void inet_rcu_free_ifa(struct rcu_head *head) 225 { 226 struct in_ifaddr *ifa = container_of(head, struct in_ifaddr, rcu_head); 227 228 in_dev_put(ifa->ifa_dev); 229 kfree(ifa); 230 } 231 232 static void inet_free_ifa(struct in_ifaddr *ifa) 233 { 234 /* Our reference to ifa->ifa_dev must be freed ASAP 235 * to release the reference to the netdev the same way. 236 * in_dev_put() -> in_dev_finish_destroy() -> netdev_put() 237 */ 238 call_rcu_hurry(&ifa->rcu_head, inet_rcu_free_ifa); 239 } 240 241 static void in_dev_free_rcu(struct rcu_head *head) 242 { 243 struct in_device *idev = container_of(head, struct in_device, rcu_head); 244 245 kfree(rcu_dereference_protected(idev->mc_hash, 1)); 246 kfree(idev); 247 } 248 249 void in_dev_finish_destroy(struct in_device *idev) 250 { 251 struct net_device *dev = idev->dev; 252 253 WARN_ON(idev->ifa_list); 254 WARN_ON(idev->mc_list); 255 #ifdef NET_REFCNT_DEBUG 256 pr_debug("%s: %p=%s\n", __func__, idev, dev ? dev->name : "NIL"); 257 #endif 258 netdev_put(dev, &idev->dev_tracker); 259 if (!idev->dead) 260 pr_err("Freeing alive in_device %p\n", idev); 261 else 262 call_rcu(&idev->rcu_head, in_dev_free_rcu); 263 } 264 EXPORT_SYMBOL(in_dev_finish_destroy); 265 266 static struct in_device *inetdev_init(struct net_device *dev) 267 { 268 struct net *net = dev_net(dev); 269 struct in_device *in_dev; 270 struct neigh_table *tbl; 271 int err = -ENOMEM; 272 273 ASSERT_RTNL(); 274 275 in_dev = kzalloc_obj(*in_dev); 276 if (!in_dev) 277 goto out; 278 279 tbl = arp_table(net); 280 memcpy(&in_dev->cnf, net->ipv4.devconf_dflt, sizeof(in_dev->cnf)); 281 in_dev->cnf.sysctl = NULL; 282 in_dev->dev = dev; 283 in_dev->arp_parms = neigh_parms_alloc(dev, tbl); 284 if (!in_dev->arp_parms) 285 goto out_kfree; 286 if (IPV4_DEVCONF(in_dev->cnf, FORWARDING)) 287 netif_disable_lro(dev); 288 /* Reference in_dev->dev */ 289 netdev_hold(dev, &in_dev->dev_tracker, GFP_KERNEL); 290 /* Account for reference dev->ip_ptr (below) */ 291 refcount_set(&in_dev->refcnt, 1); 292 293 if (dev != blackhole_netdev) { 294 err = devinet_sysctl_register(in_dev); 295 if (err) { 296 in_dev->dead = 1; 297 neigh_parms_release(tbl, in_dev->arp_parms); 298 in_dev_put(in_dev); 299 in_dev = NULL; 300 goto out; 301 } 302 ip_mc_init_dev(in_dev); 303 if (dev->flags & IFF_UP) 304 ip_mc_up(in_dev); 305 } 306 307 /* we can receive as soon as ip_ptr is set -- do this last */ 308 rcu_assign_pointer(dev->ip_ptr, in_dev); 309 out: 310 return in_dev ?: ERR_PTR(err); 311 out_kfree: 312 kfree(in_dev); 313 in_dev = NULL; 314 goto out; 315 } 316 317 static void inetdev_destroy(struct in_device *in_dev) 318 { 319 struct net_device *dev = in_dev->dev; 320 struct net *net = dev_net(dev); 321 struct in_ifaddr *ifa; 322 323 ASSERT_RTNL(); 324 325 in_dev->dead = 1; 326 327 RCU_INIT_POINTER(dev->ip_ptr, NULL); 328 329 ip_mc_destroy_dev(in_dev); 330 331 while ((ifa = rtnl_dereference(in_dev->ifa_list)) != NULL) { 332 inet_del_ifa(in_dev, &in_dev->ifa_list, 0); 333 inet_free_ifa(ifa); 334 } 335 336 devinet_sysctl_unregister(in_dev); 337 neigh_parms_release(arp_table(net), in_dev->arp_parms); 338 arp_ifdown(dev); 339 340 in_dev_put(in_dev); 341 } 342 343 static int __init inet_blackhole_dev_init(void) 344 { 345 struct in_device *in_dev; 346 347 rtnl_lock(); 348 in_dev = inetdev_init(blackhole_netdev); 349 rtnl_unlock(); 350 351 return PTR_ERR_OR_ZERO(in_dev); 352 } 353 late_initcall(inet_blackhole_dev_init); 354 355 int inet_addr_onlink(struct in_device *in_dev, __be32 a, __be32 b) 356 { 357 const struct in_ifaddr *ifa; 358 359 rcu_read_lock(); 360 in_dev_for_each_ifa_rcu(ifa, in_dev) { 361 if (inet_ifa_match(a, ifa)) { 362 if (!b || inet_ifa_match(b, ifa)) { 363 rcu_read_unlock(); 364 return 1; 365 } 366 } 367 } 368 rcu_read_unlock(); 369 return 0; 370 } 371 372 static void __inet_del_ifa(struct in_device *in_dev, 373 struct in_ifaddr __rcu **ifap, 374 int destroy, struct nlmsghdr *nlh, u32 portid) 375 { 376 struct in_ifaddr *promote = NULL; 377 struct in_ifaddr *ifa, *ifa1; 378 struct in_ifaddr __rcu **last_prim; 379 struct in_ifaddr *prev_prom = NULL; 380 int do_promote = IN_DEV_PROMOTE_SECONDARIES(in_dev); 381 382 ASSERT_RTNL(); 383 384 ifa1 = rtnl_dereference(*ifap); 385 last_prim = ifap; 386 if (in_dev->dead) 387 goto no_promotions; 388 389 /* 1. Deleting primary ifaddr forces deletion all secondaries 390 * unless alias promotion is set 391 **/ 392 393 if (!(ifa1->ifa_flags & IFA_F_SECONDARY)) { 394 struct in_ifaddr __rcu **ifap1 = &ifa1->ifa_next; 395 396 while ((ifa = rtnl_dereference(*ifap1)) != NULL) { 397 if (!(ifa->ifa_flags & IFA_F_SECONDARY) && 398 ifa1->ifa_scope <= ifa->ifa_scope) 399 last_prim = &ifa->ifa_next; 400 401 if (!(ifa->ifa_flags & IFA_F_SECONDARY) || 402 ifa1->ifa_mask != ifa->ifa_mask || 403 !inet_ifa_match(ifa1->ifa_address, ifa)) { 404 ifap1 = &ifa->ifa_next; 405 prev_prom = ifa; 406 continue; 407 } 408 409 if (!do_promote) { 410 inet_hash_remove(ifa); 411 *ifap1 = ifa->ifa_next; 412 413 rtmsg_ifa(RTM_DELADDR, ifa, nlh, portid); 414 blocking_notifier_call_chain(&inetaddr_chain, 415 NETDEV_DOWN, ifa); 416 inet_free_ifa(ifa); 417 } else { 418 promote = ifa; 419 break; 420 } 421 } 422 } 423 424 /* On promotion all secondaries from subnet are changing 425 * the primary IP, we must remove all their routes silently 426 * and later to add them back with new prefsrc. Do this 427 * while all addresses are on the device list. 428 */ 429 for (ifa = promote; ifa; ifa = rtnl_dereference(ifa->ifa_next)) { 430 if (ifa1->ifa_mask == ifa->ifa_mask && 431 inet_ifa_match(ifa1->ifa_address, ifa)) 432 fib_del_ifaddr(ifa, ifa1); 433 } 434 435 no_promotions: 436 /* 2. Unlink it */ 437 438 *ifap = ifa1->ifa_next; 439 inet_hash_remove(ifa1); 440 441 /* 3. Announce address deletion */ 442 443 /* Send message first, then call notifier. 444 At first sight, FIB update triggered by notifier 445 will refer to already deleted ifaddr, that could confuse 446 netlink listeners. It is not true: look, gated sees 447 that route deleted and if it still thinks that ifaddr 448 is valid, it will try to restore deleted routes... Grr. 449 So that, this order is correct. 450 */ 451 rtmsg_ifa(RTM_DELADDR, ifa1, nlh, portid); 452 blocking_notifier_call_chain(&inetaddr_chain, NETDEV_DOWN, ifa1); 453 454 if (promote) { 455 struct in_ifaddr *next_sec; 456 457 next_sec = rtnl_dereference(promote->ifa_next); 458 if (prev_prom) { 459 struct in_ifaddr *last_sec; 460 461 rcu_assign_pointer(prev_prom->ifa_next, next_sec); 462 463 last_sec = rtnl_dereference(*last_prim); 464 rcu_assign_pointer(promote->ifa_next, last_sec); 465 rcu_assign_pointer(*last_prim, promote); 466 } 467 468 promote->ifa_flags &= ~IFA_F_SECONDARY; 469 rtmsg_ifa(RTM_NEWADDR, promote, nlh, portid); 470 blocking_notifier_call_chain(&inetaddr_chain, 471 NETDEV_UP, promote); 472 for (ifa = next_sec; ifa; 473 ifa = rtnl_dereference(ifa->ifa_next)) { 474 if (ifa1->ifa_mask != ifa->ifa_mask || 475 !inet_ifa_match(ifa1->ifa_address, ifa)) 476 continue; 477 fib_add_ifaddr(ifa); 478 } 479 480 } 481 if (destroy) 482 inet_free_ifa(ifa1); 483 } 484 485 static void inet_del_ifa(struct in_device *in_dev, 486 struct in_ifaddr __rcu **ifap, 487 int destroy) 488 { 489 __inet_del_ifa(in_dev, ifap, destroy, NULL, 0); 490 } 491 492 static int __inet_insert_ifa(struct in_ifaddr *ifa, struct nlmsghdr *nlh, 493 u32 portid, struct netlink_ext_ack *extack) 494 { 495 struct in_ifaddr __rcu **last_primary, **ifap; 496 struct in_device *in_dev = ifa->ifa_dev; 497 struct net *net = dev_net(in_dev->dev); 498 struct in_validator_info ivi; 499 struct in_ifaddr *ifa1; 500 int ret; 501 502 ASSERT_RTNL(); 503 504 ifa->ifa_flags &= ~IFA_F_SECONDARY; 505 last_primary = &in_dev->ifa_list; 506 507 /* Don't set IPv6 only flags to IPv4 addresses */ 508 ifa->ifa_flags &= ~IPV6ONLY_FLAGS; 509 510 ifap = &in_dev->ifa_list; 511 ifa1 = rtnl_dereference(*ifap); 512 513 while (ifa1) { 514 if (!(ifa1->ifa_flags & IFA_F_SECONDARY) && 515 ifa->ifa_scope <= ifa1->ifa_scope) 516 last_primary = &ifa1->ifa_next; 517 if (ifa1->ifa_mask == ifa->ifa_mask && 518 inet_ifa_match(ifa1->ifa_address, ifa)) { 519 if (ifa1->ifa_local == ifa->ifa_local) { 520 inet_free_ifa(ifa); 521 return -EEXIST; 522 } 523 if (ifa1->ifa_scope != ifa->ifa_scope) { 524 NL_SET_ERR_MSG(extack, "ipv4: Invalid scope value"); 525 inet_free_ifa(ifa); 526 return -EINVAL; 527 } 528 ifa->ifa_flags |= IFA_F_SECONDARY; 529 } 530 531 ifap = &ifa1->ifa_next; 532 ifa1 = rtnl_dereference(*ifap); 533 } 534 535 /* Allow any devices that wish to register ifaddr validtors to weigh 536 * in now, before changes are committed. The rntl lock is serializing 537 * access here, so the state should not change between a validator call 538 * and a final notify on commit. This isn't invoked on promotion under 539 * the assumption that validators are checking the address itself, and 540 * not the flags. 541 */ 542 ivi.ivi_addr = ifa->ifa_address; 543 ivi.ivi_dev = ifa->ifa_dev; 544 ivi.extack = extack; 545 ret = blocking_notifier_call_chain(&inetaddr_validator_chain, 546 NETDEV_UP, &ivi); 547 ret = notifier_to_errno(ret); 548 if (ret) { 549 inet_free_ifa(ifa); 550 return ret; 551 } 552 553 if (!(ifa->ifa_flags & IFA_F_SECONDARY)) 554 ifap = last_primary; 555 556 rcu_assign_pointer(ifa->ifa_next, *ifap); 557 rcu_assign_pointer(*ifap, ifa); 558 559 inet_hash_insert(dev_net(in_dev->dev), ifa); 560 561 cancel_delayed_work(&net->ipv4.addr_chk_work); 562 queue_delayed_work(system_power_efficient_wq, &net->ipv4.addr_chk_work, 0); 563 564 /* Send message first, then call notifier. 565 Notifier will trigger FIB update, so that 566 listeners of netlink will know about new ifaddr */ 567 rtmsg_ifa(RTM_NEWADDR, ifa, nlh, portid); 568 blocking_notifier_call_chain(&inetaddr_chain, NETDEV_UP, ifa); 569 570 return 0; 571 } 572 573 static int inet_insert_ifa(struct in_ifaddr *ifa) 574 { 575 if (!ifa->ifa_local) { 576 inet_free_ifa(ifa); 577 return 0; 578 } 579 580 return __inet_insert_ifa(ifa, NULL, 0, NULL); 581 } 582 583 static int inet_set_ifa(struct net_device *dev, struct in_ifaddr *ifa) 584 { 585 struct in_device *in_dev = __in_dev_get_rtnl_net(dev); 586 587 ipv4_devconf_setall(in_dev); 588 neigh_parms_data_state_setall(in_dev->arp_parms); 589 590 if (ipv4_is_loopback(ifa->ifa_local)) 591 ifa->ifa_scope = RT_SCOPE_HOST; 592 return inet_insert_ifa(ifa); 593 } 594 595 /* Caller must hold RCU or RTNL : 596 * We dont take a reference on found in_device 597 */ 598 struct in_device *inetdev_by_index(struct net *net, int ifindex) 599 { 600 struct net_device *dev; 601 struct in_device *in_dev = NULL; 602 603 rcu_read_lock(); 604 dev = dev_get_by_index_rcu(net, ifindex); 605 if (dev) 606 in_dev = rcu_dereference_rtnl(dev->ip_ptr); 607 rcu_read_unlock(); 608 return in_dev; 609 } 610 EXPORT_SYMBOL(inetdev_by_index); 611 612 /* Called only from RTNL semaphored context. No locks. */ 613 614 struct in_ifaddr *inet_ifa_byprefix(struct in_device *in_dev, __be32 prefix, 615 __be32 mask) 616 { 617 struct in_ifaddr *ifa; 618 619 ASSERT_RTNL(); 620 621 in_dev_for_each_ifa_rtnl(ifa, in_dev) { 622 if (ifa->ifa_mask == mask && inet_ifa_match(prefix, ifa)) 623 return ifa; 624 } 625 return NULL; 626 } 627 628 static int ip_mc_autojoin_config(struct net *net, bool join, 629 const struct in_ifaddr *ifa) 630 { 631 #if defined(CONFIG_IP_MULTICAST) 632 struct ip_mreqn mreq = { 633 .imr_multiaddr.s_addr = ifa->ifa_address, 634 .imr_ifindex = ifa->ifa_dev->dev->ifindex, 635 }; 636 struct sock *sk = net->ipv4.mc_autojoin_sk; 637 int ret; 638 639 ASSERT_RTNL_NET(net); 640 641 lock_sock(sk); 642 if (join) 643 ret = ip_mc_join_group(sk, &mreq); 644 else 645 ret = ip_mc_leave_group(sk, &mreq); 646 release_sock(sk); 647 648 return ret; 649 #else 650 return -EOPNOTSUPP; 651 #endif 652 } 653 654 static int inet_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh, 655 struct netlink_ext_ack *extack) 656 { 657 struct net *net = sock_net(skb->sk); 658 struct in_ifaddr __rcu **ifap; 659 struct nlattr *tb[IFA_MAX+1]; 660 struct in_device *in_dev; 661 struct ifaddrmsg *ifm; 662 struct in_ifaddr *ifa; 663 int err; 664 665 err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFA_MAX, 666 ifa_ipv4_policy, extack); 667 if (err < 0) 668 goto out; 669 670 ifm = nlmsg_data(nlh); 671 672 rtnl_net_lock(net); 673 674 in_dev = inetdev_by_index(net, ifm->ifa_index); 675 if (!in_dev) { 676 NL_SET_ERR_MSG(extack, "ipv4: Device not found"); 677 err = -ENODEV; 678 goto unlock; 679 } 680 681 for (ifap = &in_dev->ifa_list; 682 (ifa = rtnl_net_dereference(net, *ifap)) != NULL; 683 ifap = &ifa->ifa_next) { 684 if (tb[IFA_LOCAL] && 685 ifa->ifa_local != nla_get_in_addr(tb[IFA_LOCAL])) 686 continue; 687 688 if (tb[IFA_LABEL] && nla_strcmp(tb[IFA_LABEL], ifa->ifa_label)) 689 continue; 690 691 if (tb[IFA_ADDRESS] && 692 (ifm->ifa_prefixlen != ifa->ifa_prefixlen || 693 !inet_ifa_match(nla_get_in_addr(tb[IFA_ADDRESS]), ifa))) 694 continue; 695 696 if (ipv4_is_multicast(ifa->ifa_address)) 697 ip_mc_autojoin_config(net, false, ifa); 698 699 __inet_del_ifa(in_dev, ifap, 1, nlh, NETLINK_CB(skb).portid); 700 goto unlock; 701 } 702 703 NL_SET_ERR_MSG(extack, "ipv4: Address not found"); 704 err = -EADDRNOTAVAIL; 705 unlock: 706 rtnl_net_unlock(net); 707 out: 708 return err; 709 } 710 711 static void check_lifetime(struct work_struct *work) 712 { 713 unsigned long now, next, next_sec, next_sched; 714 struct in_ifaddr *ifa; 715 struct hlist_node *n; 716 struct net *net; 717 int i; 718 719 net = container_of(to_delayed_work(work), struct net, ipv4.addr_chk_work); 720 now = jiffies; 721 next = round_jiffies_up(now + ADDR_CHECK_FREQUENCY); 722 723 for (i = 0; i < IN4_ADDR_HSIZE; i++) { 724 struct hlist_head *head = &net->ipv4.inet_addr_lst[i]; 725 bool change_needed = false; 726 727 rcu_read_lock(); 728 hlist_for_each_entry_rcu(ifa, head, addr_lst) { 729 unsigned long age, tstamp; 730 u32 preferred_lft; 731 u32 valid_lft; 732 u32 flags; 733 734 flags = READ_ONCE(ifa->ifa_flags); 735 if (flags & IFA_F_PERMANENT) 736 continue; 737 738 preferred_lft = READ_ONCE(ifa->ifa_preferred_lft); 739 valid_lft = READ_ONCE(ifa->ifa_valid_lft); 740 tstamp = READ_ONCE(ifa->ifa_tstamp); 741 /* We try to batch several events at once. */ 742 age = (now - tstamp + 743 ADDRCONF_TIMER_FUZZ_MINUS) / HZ; 744 745 if (valid_lft != INFINITY_LIFE_TIME && 746 age >= valid_lft) { 747 change_needed = true; 748 } else if (preferred_lft == 749 INFINITY_LIFE_TIME) { 750 continue; 751 } else if (age >= preferred_lft) { 752 if (time_before(tstamp + valid_lft * HZ, next)) 753 next = tstamp + valid_lft * HZ; 754 755 if (!(flags & IFA_F_DEPRECATED)) 756 change_needed = true; 757 } else if (time_before(tstamp + preferred_lft * HZ, 758 next)) { 759 next = tstamp + preferred_lft * HZ; 760 } 761 } 762 rcu_read_unlock(); 763 if (!change_needed) 764 continue; 765 766 rtnl_net_lock(net); 767 hlist_for_each_entry_safe(ifa, n, head, addr_lst) { 768 unsigned long age; 769 770 if (ifa->ifa_flags & IFA_F_PERMANENT) 771 continue; 772 773 /* We try to batch several events at once. */ 774 age = (now - ifa->ifa_tstamp + 775 ADDRCONF_TIMER_FUZZ_MINUS) / HZ; 776 777 if (ifa->ifa_valid_lft != INFINITY_LIFE_TIME && 778 age >= ifa->ifa_valid_lft) { 779 struct in_ifaddr __rcu **ifap; 780 struct in_ifaddr *tmp; 781 782 ifap = &ifa->ifa_dev->ifa_list; 783 tmp = rtnl_net_dereference(net, *ifap); 784 while (tmp) { 785 if (tmp == ifa) { 786 inet_del_ifa(ifa->ifa_dev, 787 ifap, 1); 788 break; 789 } 790 ifap = &tmp->ifa_next; 791 tmp = rtnl_net_dereference(net, *ifap); 792 } 793 } else if (ifa->ifa_preferred_lft != 794 INFINITY_LIFE_TIME && 795 age >= ifa->ifa_preferred_lft && 796 !(ifa->ifa_flags & IFA_F_DEPRECATED)) { 797 ifa->ifa_flags |= IFA_F_DEPRECATED; 798 rtmsg_ifa(RTM_NEWADDR, ifa, NULL, 0); 799 } 800 } 801 rtnl_net_unlock(net); 802 } 803 804 next_sec = round_jiffies_up(next); 805 next_sched = next; 806 807 /* If rounded timeout is accurate enough, accept it. */ 808 if (time_before(next_sec, next + ADDRCONF_TIMER_FUZZ)) 809 next_sched = next_sec; 810 811 now = jiffies; 812 /* And minimum interval is ADDRCONF_TIMER_FUZZ_MAX. */ 813 if (time_before(next_sched, now + ADDRCONF_TIMER_FUZZ_MAX)) 814 next_sched = now + ADDRCONF_TIMER_FUZZ_MAX; 815 816 queue_delayed_work(system_power_efficient_wq, &net->ipv4.addr_chk_work, 817 next_sched - now); 818 } 819 820 static void set_ifa_lifetime(struct in_ifaddr *ifa, __u32 valid_lft, 821 __u32 prefered_lft) 822 { 823 unsigned long timeout; 824 u32 flags; 825 826 flags = ifa->ifa_flags & ~(IFA_F_PERMANENT | IFA_F_DEPRECATED); 827 828 timeout = addrconf_timeout_fixup(valid_lft, HZ); 829 if (addrconf_finite_timeout(timeout)) 830 WRITE_ONCE(ifa->ifa_valid_lft, timeout); 831 else 832 flags |= IFA_F_PERMANENT; 833 834 timeout = addrconf_timeout_fixup(prefered_lft, HZ); 835 if (addrconf_finite_timeout(timeout)) { 836 if (timeout == 0) 837 flags |= IFA_F_DEPRECATED; 838 WRITE_ONCE(ifa->ifa_preferred_lft, timeout); 839 } 840 WRITE_ONCE(ifa->ifa_flags, flags); 841 WRITE_ONCE(ifa->ifa_tstamp, jiffies); 842 if (!ifa->ifa_cstamp) 843 WRITE_ONCE(ifa->ifa_cstamp, ifa->ifa_tstamp); 844 } 845 846 static int inet_validate_rtm(struct nlmsghdr *nlh, struct nlattr **tb, 847 struct netlink_ext_ack *extack, 848 __u32 *valid_lft, __u32 *prefered_lft) 849 { 850 struct ifaddrmsg *ifm = nlmsg_data(nlh); 851 int err; 852 853 err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFA_MAX, 854 ifa_ipv4_policy, extack); 855 if (err < 0) 856 return err; 857 858 if (ifm->ifa_prefixlen > 32) { 859 NL_SET_ERR_MSG(extack, "ipv4: Invalid prefix length"); 860 return -EINVAL; 861 } 862 863 if (!tb[IFA_LOCAL]) { 864 NL_SET_ERR_MSG(extack, "ipv4: Local address is not supplied"); 865 return -EINVAL; 866 } 867 868 if (tb[IFA_CACHEINFO]) { 869 struct ifa_cacheinfo *ci; 870 871 ci = nla_data(tb[IFA_CACHEINFO]); 872 if (!ci->ifa_valid || ci->ifa_prefered > ci->ifa_valid) { 873 NL_SET_ERR_MSG(extack, "ipv4: address lifetime invalid"); 874 return -EINVAL; 875 } 876 877 *valid_lft = ci->ifa_valid; 878 *prefered_lft = ci->ifa_prefered; 879 } 880 881 return 0; 882 } 883 884 static struct in_ifaddr *inet_rtm_to_ifa(struct net *net, struct nlmsghdr *nlh, 885 struct nlattr **tb, 886 struct netlink_ext_ack *extack) 887 { 888 struct ifaddrmsg *ifm = nlmsg_data(nlh); 889 struct in_device *in_dev; 890 struct net_device *dev; 891 struct in_ifaddr *ifa; 892 int err; 893 894 dev = __dev_get_by_index(net, ifm->ifa_index); 895 err = -ENODEV; 896 if (!dev) { 897 NL_SET_ERR_MSG(extack, "ipv4: Device not found"); 898 goto errout; 899 } 900 901 in_dev = __in_dev_get_rtnl_net(dev); 902 err = -ENOBUFS; 903 if (!in_dev) 904 goto errout; 905 906 ifa = inet_alloc_ifa(in_dev); 907 if (!ifa) 908 /* 909 * A potential indev allocation can be left alive, it stays 910 * assigned to its device and is destroy with it. 911 */ 912 goto errout; 913 914 ipv4_devconf_setall(in_dev); 915 neigh_parms_data_state_setall(in_dev->arp_parms); 916 917 if (!tb[IFA_ADDRESS]) 918 tb[IFA_ADDRESS] = tb[IFA_LOCAL]; 919 920 ifa->ifa_prefixlen = ifm->ifa_prefixlen; 921 ifa->ifa_mask = inet_make_mask(ifm->ifa_prefixlen); 922 ifa->ifa_flags = nla_get_u32_default(tb[IFA_FLAGS], ifm->ifa_flags); 923 ifa->ifa_scope = ifm->ifa_scope; 924 ifa->ifa_local = nla_get_in_addr(tb[IFA_LOCAL]); 925 ifa->ifa_address = nla_get_in_addr(tb[IFA_ADDRESS]); 926 927 if (tb[IFA_BROADCAST]) 928 ifa->ifa_broadcast = nla_get_in_addr(tb[IFA_BROADCAST]); 929 930 if (tb[IFA_LABEL]) 931 nla_strscpy(ifa->ifa_label, tb[IFA_LABEL], IFNAMSIZ); 932 else 933 memcpy(ifa->ifa_label, dev->name, IFNAMSIZ); 934 935 if (tb[IFA_RT_PRIORITY]) 936 ifa->ifa_rt_priority = nla_get_u32(tb[IFA_RT_PRIORITY]); 937 938 if (tb[IFA_PROTO]) 939 ifa->ifa_proto = nla_get_u8(tb[IFA_PROTO]); 940 941 return ifa; 942 943 errout: 944 return ERR_PTR(err); 945 } 946 947 static struct in_ifaddr *find_matching_ifa(struct net *net, struct in_ifaddr *ifa) 948 { 949 struct in_device *in_dev = ifa->ifa_dev; 950 struct in_ifaddr *ifa1; 951 952 in_dev_for_each_ifa_rtnl_net(net, ifa1, in_dev) { 953 if (ifa1->ifa_mask == ifa->ifa_mask && 954 inet_ifa_match(ifa1->ifa_address, ifa) && 955 ifa1->ifa_local == ifa->ifa_local) 956 return ifa1; 957 } 958 959 return NULL; 960 } 961 962 static int inet_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh, 963 struct netlink_ext_ack *extack) 964 { 965 __u32 prefered_lft = INFINITY_LIFE_TIME; 966 __u32 valid_lft = INFINITY_LIFE_TIME; 967 struct net *net = sock_net(skb->sk); 968 struct in_ifaddr *ifa_existing; 969 struct nlattr *tb[IFA_MAX + 1]; 970 struct in_ifaddr *ifa; 971 int ret; 972 973 ret = inet_validate_rtm(nlh, tb, extack, &valid_lft, &prefered_lft); 974 if (ret < 0) 975 return ret; 976 977 if (!nla_get_in_addr(tb[IFA_LOCAL])) 978 return 0; 979 980 rtnl_net_lock(net); 981 982 ifa = inet_rtm_to_ifa(net, nlh, tb, extack); 983 if (IS_ERR(ifa)) { 984 ret = PTR_ERR(ifa); 985 goto unlock; 986 } 987 988 ifa_existing = find_matching_ifa(net, ifa); 989 if (!ifa_existing) { 990 /* It would be best to check for !NLM_F_CREATE here but 991 * userspace already relies on not having to provide this. 992 */ 993 set_ifa_lifetime(ifa, valid_lft, prefered_lft); 994 if (ifa->ifa_flags & IFA_F_MCAUTOJOIN) { 995 ret = ip_mc_autojoin_config(net, true, ifa); 996 if (ret < 0) { 997 NL_SET_ERR_MSG(extack, "ipv4: Multicast auto join failed"); 998 inet_free_ifa(ifa); 999 goto unlock; 1000 } 1001 } 1002 1003 ret = __inet_insert_ifa(ifa, nlh, NETLINK_CB(skb).portid, extack); 1004 } else { 1005 u32 new_metric = ifa->ifa_rt_priority; 1006 u8 new_proto = ifa->ifa_proto; 1007 1008 inet_free_ifa(ifa); 1009 1010 if (nlh->nlmsg_flags & NLM_F_EXCL || 1011 !(nlh->nlmsg_flags & NLM_F_REPLACE)) { 1012 NL_SET_ERR_MSG(extack, "ipv4: Address already assigned"); 1013 ret = -EEXIST; 1014 goto unlock; 1015 } 1016 ifa = ifa_existing; 1017 1018 if (ifa->ifa_rt_priority != new_metric) { 1019 fib_modify_prefix_metric(ifa, new_metric); 1020 ifa->ifa_rt_priority = new_metric; 1021 } 1022 1023 ifa->ifa_proto = new_proto; 1024 1025 set_ifa_lifetime(ifa, valid_lft, prefered_lft); 1026 cancel_delayed_work(&net->ipv4.addr_chk_work); 1027 queue_delayed_work(system_power_efficient_wq, 1028 &net->ipv4.addr_chk_work, 0); 1029 rtmsg_ifa(RTM_NEWADDR, ifa, nlh, NETLINK_CB(skb).portid); 1030 } 1031 1032 unlock: 1033 rtnl_net_unlock(net); 1034 1035 return ret; 1036 } 1037 1038 /* 1039 * Determine a default network mask, based on the IP address. 1040 */ 1041 1042 static int inet_abc_len(__be32 addr) 1043 { 1044 int rc = -1; /* Something else, probably a multicast. */ 1045 1046 if (ipv4_is_zeronet(addr) || ipv4_is_lbcast(addr)) 1047 rc = 0; 1048 else { 1049 __u32 haddr = ntohl(addr); 1050 if (IN_CLASSA(haddr)) 1051 rc = 8; 1052 else if (IN_CLASSB(haddr)) 1053 rc = 16; 1054 else if (IN_CLASSC(haddr)) 1055 rc = 24; 1056 else if (IN_CLASSE(haddr)) 1057 rc = 32; 1058 } 1059 1060 return rc; 1061 } 1062 1063 1064 int devinet_ioctl(struct net *net, unsigned int cmd, struct ifreq *ifr) 1065 { 1066 struct sockaddr_in sin_orig; 1067 struct sockaddr_in *sin = (struct sockaddr_in *)&ifr->ifr_addr; 1068 struct in_ifaddr __rcu **ifap = NULL; 1069 struct in_device *in_dev; 1070 struct in_ifaddr *ifa = NULL; 1071 struct net_device *dev; 1072 char *colon; 1073 int ret = -EFAULT; 1074 int tryaddrmatch = 0; 1075 1076 ifr->ifr_name[IFNAMSIZ - 1] = 0; 1077 1078 /* save original address for comparison */ 1079 memcpy(&sin_orig, sin, sizeof(*sin)); 1080 1081 colon = strchr(ifr->ifr_name, ':'); 1082 if (colon) 1083 *colon = 0; 1084 1085 dev_load(net, ifr->ifr_name); 1086 1087 switch (cmd) { 1088 case SIOCGIFADDR: /* Get interface address */ 1089 case SIOCGIFBRDADDR: /* Get the broadcast address */ 1090 case SIOCGIFDSTADDR: /* Get the destination address */ 1091 case SIOCGIFNETMASK: /* Get the netmask for the interface */ 1092 /* Note that these ioctls will not sleep, 1093 so that we do not impose a lock. 1094 One day we will be forced to put shlock here (I mean SMP) 1095 */ 1096 tryaddrmatch = (sin_orig.sin_family == AF_INET); 1097 memset(sin, 0, sizeof(*sin)); 1098 sin->sin_family = AF_INET; 1099 break; 1100 1101 case SIOCSIFFLAGS: 1102 ret = -EPERM; 1103 if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) 1104 goto out; 1105 break; 1106 case SIOCSIFADDR: /* Set interface address (and family) */ 1107 case SIOCSIFBRDADDR: /* Set the broadcast address */ 1108 case SIOCSIFDSTADDR: /* Set the destination address */ 1109 case SIOCSIFNETMASK: /* Set the netmask for the interface */ 1110 ret = -EPERM; 1111 if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) 1112 goto out; 1113 ret = -EINVAL; 1114 if (sin->sin_family != AF_INET) 1115 goto out; 1116 break; 1117 default: 1118 ret = -EINVAL; 1119 goto out; 1120 } 1121 1122 rtnl_net_lock(net); 1123 1124 ret = -ENODEV; 1125 dev = __dev_get_by_name(net, ifr->ifr_name); 1126 if (!dev) 1127 goto done; 1128 1129 if (colon) 1130 *colon = ':'; 1131 1132 in_dev = __in_dev_get_rtnl_net(dev); 1133 if (in_dev) { 1134 if (tryaddrmatch) { 1135 /* Matthias Andree */ 1136 /* compare label and address (4.4BSD style) */ 1137 /* note: we only do this for a limited set of ioctls 1138 and only if the original address family was AF_INET. 1139 This is checked above. */ 1140 1141 for (ifap = &in_dev->ifa_list; 1142 (ifa = rtnl_net_dereference(net, *ifap)) != NULL; 1143 ifap = &ifa->ifa_next) { 1144 if (!strcmp(ifr->ifr_name, ifa->ifa_label) && 1145 sin_orig.sin_addr.s_addr == 1146 ifa->ifa_local) { 1147 break; /* found */ 1148 } 1149 } 1150 } 1151 /* we didn't get a match, maybe the application is 1152 4.3BSD-style and passed in junk so we fall back to 1153 comparing just the label */ 1154 if (!ifa) { 1155 for (ifap = &in_dev->ifa_list; 1156 (ifa = rtnl_net_dereference(net, *ifap)) != NULL; 1157 ifap = &ifa->ifa_next) 1158 if (!strcmp(ifr->ifr_name, ifa->ifa_label)) 1159 break; 1160 } 1161 } 1162 1163 ret = -EADDRNOTAVAIL; 1164 if (!ifa && cmd != SIOCSIFADDR && cmd != SIOCSIFFLAGS) 1165 goto done; 1166 1167 switch (cmd) { 1168 case SIOCGIFADDR: /* Get interface address */ 1169 ret = 0; 1170 sin->sin_addr.s_addr = ifa->ifa_local; 1171 break; 1172 1173 case SIOCGIFBRDADDR: /* Get the broadcast address */ 1174 ret = 0; 1175 sin->sin_addr.s_addr = ifa->ifa_broadcast; 1176 break; 1177 1178 case SIOCGIFDSTADDR: /* Get the destination address */ 1179 ret = 0; 1180 sin->sin_addr.s_addr = ifa->ifa_address; 1181 break; 1182 1183 case SIOCGIFNETMASK: /* Get the netmask for the interface */ 1184 ret = 0; 1185 sin->sin_addr.s_addr = ifa->ifa_mask; 1186 break; 1187 1188 case SIOCSIFFLAGS: 1189 if (colon) { 1190 ret = -EADDRNOTAVAIL; 1191 if (!ifa) 1192 break; 1193 ret = 0; 1194 if (!(ifr->ifr_flags & IFF_UP)) 1195 inet_del_ifa(in_dev, ifap, 1); 1196 break; 1197 } 1198 1199 /* NETDEV_UP/DOWN/CHANGE could touch a peer dev */ 1200 ASSERT_RTNL(); 1201 ret = dev_change_flags(dev, ifr->ifr_flags, NULL); 1202 break; 1203 1204 case SIOCSIFADDR: /* Set interface address (and family) */ 1205 ret = -EINVAL; 1206 if (inet_abc_len(sin->sin_addr.s_addr) < 0) 1207 break; 1208 1209 if (!ifa) { 1210 ret = -ENOBUFS; 1211 if (!in_dev) 1212 break; 1213 ifa = inet_alloc_ifa(in_dev); 1214 if (!ifa) 1215 break; 1216 1217 if (colon) 1218 memcpy(ifa->ifa_label, ifr->ifr_name, IFNAMSIZ); 1219 else 1220 memcpy(ifa->ifa_label, dev->name, IFNAMSIZ); 1221 } else { 1222 ret = 0; 1223 if (ifa->ifa_local == sin->sin_addr.s_addr) 1224 break; 1225 inet_del_ifa(in_dev, ifap, 0); 1226 ifa->ifa_broadcast = 0; 1227 ifa->ifa_scope = 0; 1228 } 1229 1230 ifa->ifa_address = ifa->ifa_local = sin->sin_addr.s_addr; 1231 1232 if (!(dev->flags & IFF_POINTOPOINT)) { 1233 ifa->ifa_prefixlen = inet_abc_len(ifa->ifa_address); 1234 ifa->ifa_mask = inet_make_mask(ifa->ifa_prefixlen); 1235 if ((dev->flags & IFF_BROADCAST) && 1236 ifa->ifa_prefixlen < 31) 1237 ifa->ifa_broadcast = ifa->ifa_address | 1238 ~ifa->ifa_mask; 1239 } else { 1240 ifa->ifa_prefixlen = 32; 1241 ifa->ifa_mask = inet_make_mask(32); 1242 } 1243 set_ifa_lifetime(ifa, INFINITY_LIFE_TIME, INFINITY_LIFE_TIME); 1244 ret = inet_set_ifa(dev, ifa); 1245 break; 1246 1247 case SIOCSIFBRDADDR: /* Set the broadcast address */ 1248 ret = 0; 1249 if (ifa->ifa_broadcast != sin->sin_addr.s_addr) { 1250 inet_del_ifa(in_dev, ifap, 0); 1251 ifa->ifa_broadcast = sin->sin_addr.s_addr; 1252 inet_insert_ifa(ifa); 1253 } 1254 break; 1255 1256 case SIOCSIFDSTADDR: /* Set the destination address */ 1257 ret = 0; 1258 if (ifa->ifa_address == sin->sin_addr.s_addr) 1259 break; 1260 ret = -EINVAL; 1261 if (inet_abc_len(sin->sin_addr.s_addr) < 0) 1262 break; 1263 ret = 0; 1264 inet_del_ifa(in_dev, ifap, 0); 1265 ifa->ifa_address = sin->sin_addr.s_addr; 1266 inet_insert_ifa(ifa); 1267 break; 1268 1269 case SIOCSIFNETMASK: /* Set the netmask for the interface */ 1270 1271 /* 1272 * The mask we set must be legal. 1273 */ 1274 ret = -EINVAL; 1275 if (bad_mask(sin->sin_addr.s_addr, 0)) 1276 break; 1277 ret = 0; 1278 if (ifa->ifa_mask != sin->sin_addr.s_addr) { 1279 __be32 old_mask = ifa->ifa_mask; 1280 inet_del_ifa(in_dev, ifap, 0); 1281 ifa->ifa_mask = sin->sin_addr.s_addr; 1282 ifa->ifa_prefixlen = inet_mask_len(ifa->ifa_mask); 1283 1284 /* See if current broadcast address matches 1285 * with current netmask, then recalculate 1286 * the broadcast address. Otherwise it's a 1287 * funny address, so don't touch it since 1288 * the user seems to know what (s)he's doing... 1289 */ 1290 if ((dev->flags & IFF_BROADCAST) && 1291 (ifa->ifa_prefixlen < 31) && 1292 (ifa->ifa_broadcast == 1293 (ifa->ifa_local|~old_mask))) { 1294 ifa->ifa_broadcast = (ifa->ifa_local | 1295 ~sin->sin_addr.s_addr); 1296 } 1297 inet_insert_ifa(ifa); 1298 } 1299 break; 1300 } 1301 done: 1302 rtnl_net_unlock(net); 1303 out: 1304 return ret; 1305 } 1306 1307 int inet_gifconf(struct net_device *dev, char __user *buf, int len, int size) 1308 { 1309 struct in_device *in_dev = __in_dev_get_rtnl_net(dev); 1310 const struct in_ifaddr *ifa; 1311 struct ifreq ifr; 1312 int done = 0; 1313 1314 if (WARN_ON(size > sizeof(struct ifreq))) 1315 goto out; 1316 1317 if (!in_dev) 1318 goto out; 1319 1320 in_dev_for_each_ifa_rtnl_net(dev_net(dev), ifa, in_dev) { 1321 if (!buf) { 1322 done += size; 1323 continue; 1324 } 1325 if (len < size) 1326 break; 1327 memset(&ifr, 0, sizeof(struct ifreq)); 1328 strcpy(ifr.ifr_name, ifa->ifa_label); 1329 1330 (*(struct sockaddr_in *)&ifr.ifr_addr).sin_family = AF_INET; 1331 (*(struct sockaddr_in *)&ifr.ifr_addr).sin_addr.s_addr = 1332 ifa->ifa_local; 1333 1334 if (copy_to_user(buf + done, &ifr, size)) { 1335 done = -EFAULT; 1336 break; 1337 } 1338 len -= size; 1339 done += size; 1340 } 1341 out: 1342 return done; 1343 } 1344 1345 static __be32 in_dev_select_addr(const struct in_device *in_dev, 1346 int scope) 1347 { 1348 const struct in_ifaddr *ifa; 1349 1350 in_dev_for_each_ifa_rcu(ifa, in_dev) { 1351 if (READ_ONCE(ifa->ifa_flags) & IFA_F_SECONDARY) 1352 continue; 1353 if (ifa->ifa_scope != RT_SCOPE_LINK && 1354 ifa->ifa_scope <= scope) 1355 return ifa->ifa_local; 1356 } 1357 1358 return 0; 1359 } 1360 1361 __be32 inet_select_addr(const struct net_device *dev, __be32 dst, int scope) 1362 { 1363 const struct in_ifaddr *ifa; 1364 __be32 addr = 0; 1365 unsigned char localnet_scope = RT_SCOPE_HOST; 1366 struct in_device *in_dev; 1367 struct net *net; 1368 int master_idx; 1369 1370 rcu_read_lock(); 1371 net = dev_net_rcu(dev); 1372 in_dev = __in_dev_get_rcu(dev); 1373 if (!in_dev) 1374 goto no_in_dev; 1375 1376 if (unlikely(IN_DEV_ROUTE_LOCALNET(in_dev))) 1377 localnet_scope = RT_SCOPE_LINK; 1378 1379 in_dev_for_each_ifa_rcu(ifa, in_dev) { 1380 if (READ_ONCE(ifa->ifa_flags) & IFA_F_SECONDARY) 1381 continue; 1382 if (min(ifa->ifa_scope, localnet_scope) > scope) 1383 continue; 1384 if (!dst || inet_ifa_match(dst, ifa)) { 1385 addr = ifa->ifa_local; 1386 break; 1387 } 1388 if (!addr) 1389 addr = ifa->ifa_local; 1390 } 1391 1392 if (addr) 1393 goto out_unlock; 1394 no_in_dev: 1395 master_idx = l3mdev_master_ifindex_rcu(dev); 1396 1397 /* For VRFs, the VRF device takes the place of the loopback device, 1398 * with addresses on it being preferred. Note in such cases the 1399 * loopback device will be among the devices that fail the master_idx 1400 * equality check in the loop below. 1401 */ 1402 if (master_idx && 1403 (dev = dev_get_by_index_rcu(net, master_idx)) && 1404 (in_dev = __in_dev_get_rcu(dev))) { 1405 addr = in_dev_select_addr(in_dev, scope); 1406 if (addr) 1407 goto out_unlock; 1408 } 1409 1410 /* Not loopback addresses on loopback should be preferred 1411 in this case. It is important that lo is the first interface 1412 in dev_base list. 1413 */ 1414 for_each_netdev_rcu(net, dev) { 1415 if (l3mdev_master_ifindex_rcu(dev) != master_idx) 1416 continue; 1417 1418 in_dev = __in_dev_get_rcu(dev); 1419 if (!in_dev) 1420 continue; 1421 1422 addr = in_dev_select_addr(in_dev, scope); 1423 if (addr) 1424 goto out_unlock; 1425 } 1426 out_unlock: 1427 rcu_read_unlock(); 1428 return addr; 1429 } 1430 EXPORT_SYMBOL(inet_select_addr); 1431 1432 static __be32 confirm_addr_indev(struct in_device *in_dev, __be32 dst, 1433 __be32 local, int scope) 1434 { 1435 unsigned char localnet_scope = RT_SCOPE_HOST; 1436 const struct in_ifaddr *ifa; 1437 __be32 addr = 0; 1438 int same = 0; 1439 1440 if (unlikely(IN_DEV_ROUTE_LOCALNET(in_dev))) 1441 localnet_scope = RT_SCOPE_LINK; 1442 1443 in_dev_for_each_ifa_rcu(ifa, in_dev) { 1444 unsigned char min_scope = min(ifa->ifa_scope, localnet_scope); 1445 1446 if (!addr && 1447 (local == ifa->ifa_local || !local) && 1448 min_scope <= scope) { 1449 addr = ifa->ifa_local; 1450 if (same) 1451 break; 1452 } 1453 if (!same) { 1454 same = (!local || inet_ifa_match(local, ifa)) && 1455 (!dst || inet_ifa_match(dst, ifa)); 1456 if (same && addr) { 1457 if (local || !dst) 1458 break; 1459 /* Is the selected addr into dst subnet? */ 1460 if (inet_ifa_match(addr, ifa)) 1461 break; 1462 /* No, then can we use new local src? */ 1463 if (min_scope <= scope) { 1464 addr = ifa->ifa_local; 1465 break; 1466 } 1467 /* search for large dst subnet for addr */ 1468 same = 0; 1469 } 1470 } 1471 } 1472 1473 return same ? addr : 0; 1474 } 1475 1476 /* 1477 * Confirm that local IP address exists using wildcards: 1478 * - net: netns to check, cannot be NULL 1479 * - in_dev: only on this interface, NULL=any interface 1480 * - dst: only in the same subnet as dst, 0=any dst 1481 * - local: address, 0=autoselect the local address 1482 * - scope: maximum allowed scope value for the local address 1483 */ 1484 __be32 inet_confirm_addr(struct net *net, struct in_device *in_dev, 1485 __be32 dst, __be32 local, int scope) 1486 { 1487 __be32 addr = 0; 1488 struct net_device *dev; 1489 1490 if (in_dev) 1491 return confirm_addr_indev(in_dev, dst, local, scope); 1492 1493 rcu_read_lock(); 1494 for_each_netdev_rcu(net, dev) { 1495 in_dev = __in_dev_get_rcu(dev); 1496 if (in_dev) { 1497 addr = confirm_addr_indev(in_dev, dst, local, scope); 1498 if (addr) 1499 break; 1500 } 1501 } 1502 rcu_read_unlock(); 1503 1504 return addr; 1505 } 1506 EXPORT_SYMBOL(inet_confirm_addr); 1507 1508 /* 1509 * Device notifier 1510 */ 1511 1512 int register_inetaddr_notifier(struct notifier_block *nb) 1513 { 1514 return blocking_notifier_chain_register(&inetaddr_chain, nb); 1515 } 1516 EXPORT_SYMBOL(register_inetaddr_notifier); 1517 1518 int unregister_inetaddr_notifier(struct notifier_block *nb) 1519 { 1520 return blocking_notifier_chain_unregister(&inetaddr_chain, nb); 1521 } 1522 EXPORT_SYMBOL(unregister_inetaddr_notifier); 1523 1524 int register_inetaddr_validator_notifier(struct notifier_block *nb) 1525 { 1526 return blocking_notifier_chain_register(&inetaddr_validator_chain, nb); 1527 } 1528 EXPORT_SYMBOL(register_inetaddr_validator_notifier); 1529 1530 int unregister_inetaddr_validator_notifier(struct notifier_block *nb) 1531 { 1532 return blocking_notifier_chain_unregister(&inetaddr_validator_chain, 1533 nb); 1534 } 1535 EXPORT_SYMBOL(unregister_inetaddr_validator_notifier); 1536 1537 /* Rename ifa_labels for a device name change. Make some effort to preserve 1538 * existing alias numbering and to create unique labels if possible. 1539 */ 1540 static void inetdev_changename(struct net_device *dev, struct in_device *in_dev) 1541 { 1542 struct in_ifaddr *ifa; 1543 int named = 0; 1544 1545 in_dev_for_each_ifa_rtnl(ifa, in_dev) { 1546 char old[IFNAMSIZ], *dot; 1547 1548 memcpy(old, ifa->ifa_label, IFNAMSIZ); 1549 memcpy(ifa->ifa_label, dev->name, IFNAMSIZ); 1550 if (named++ == 0) 1551 goto skip; 1552 dot = strchr(old, ':'); 1553 if (!dot) { 1554 sprintf(old, ":%d", named); 1555 dot = old; 1556 } 1557 if (strlen(dot) + strlen(dev->name) < IFNAMSIZ) 1558 strcat(ifa->ifa_label, dot); 1559 else 1560 strcpy(ifa->ifa_label + (IFNAMSIZ - strlen(dot) - 1), dot); 1561 skip: 1562 rtmsg_ifa(RTM_NEWADDR, ifa, NULL, 0); 1563 } 1564 } 1565 1566 static void inetdev_send_gratuitous_arp(struct net_device *dev, 1567 struct in_device *in_dev) 1568 1569 { 1570 const struct in_ifaddr *ifa; 1571 1572 in_dev_for_each_ifa_rtnl(ifa, in_dev) { 1573 arp_send(ARPOP_REQUEST, ETH_P_ARP, 1574 ifa->ifa_local, dev, 1575 ifa->ifa_local, NULL, 1576 dev->dev_addr, NULL); 1577 } 1578 } 1579 1580 /* Called only under RTNL semaphore */ 1581 1582 static int inetdev_event(struct notifier_block *this, unsigned long event, 1583 void *ptr) 1584 { 1585 struct net_device *dev = netdev_notifier_info_to_dev(ptr); 1586 struct in_device *in_dev = __in_dev_get_rtnl(dev); 1587 1588 ASSERT_RTNL(); 1589 1590 if (!in_dev) { 1591 if (event == NETDEV_REGISTER) { 1592 in_dev = inetdev_init(dev); 1593 if (IS_ERR(in_dev)) 1594 return notifier_from_errno(PTR_ERR(in_dev)); 1595 if (dev->flags & IFF_LOOPBACK) { 1596 IN_DEV_CONF_SET(in_dev, NOXFRM, 1); 1597 IN_DEV_CONF_SET(in_dev, NOPOLICY, 1); 1598 } 1599 } else if (event == NETDEV_CHANGEMTU) { 1600 /* Re-enabling IP */ 1601 if (inetdev_valid_mtu(dev->mtu)) 1602 in_dev = inetdev_init(dev); 1603 } 1604 goto out; 1605 } 1606 1607 switch (event) { 1608 case NETDEV_REGISTER: 1609 pr_debug("%s: bug\n", __func__); 1610 RCU_INIT_POINTER(dev->ip_ptr, NULL); 1611 break; 1612 case NETDEV_UP: 1613 if (!inetdev_valid_mtu(dev->mtu)) 1614 break; 1615 if (dev->flags & IFF_LOOPBACK) { 1616 struct in_ifaddr *ifa = inet_alloc_ifa(in_dev); 1617 1618 if (ifa) { 1619 ifa->ifa_local = 1620 ifa->ifa_address = htonl(INADDR_LOOPBACK); 1621 ifa->ifa_prefixlen = 8; 1622 ifa->ifa_mask = inet_make_mask(8); 1623 ifa->ifa_scope = RT_SCOPE_HOST; 1624 memcpy(ifa->ifa_label, dev->name, IFNAMSIZ); 1625 set_ifa_lifetime(ifa, INFINITY_LIFE_TIME, 1626 INFINITY_LIFE_TIME); 1627 ipv4_devconf_setall(in_dev); 1628 neigh_parms_data_state_setall(in_dev->arp_parms); 1629 inet_insert_ifa(ifa); 1630 } 1631 } 1632 ip_mc_up(in_dev); 1633 fallthrough; 1634 case NETDEV_CHANGEADDR: 1635 if (!IN_DEV_ARP_NOTIFY(in_dev)) 1636 break; 1637 fallthrough; 1638 case NETDEV_NOTIFY_PEERS: 1639 /* Send gratuitous ARP to notify of link change */ 1640 inetdev_send_gratuitous_arp(dev, in_dev); 1641 break; 1642 case NETDEV_DOWN: 1643 ip_mc_down(in_dev); 1644 break; 1645 case NETDEV_PRE_TYPE_CHANGE: 1646 ip_mc_unmap(in_dev); 1647 break; 1648 case NETDEV_POST_TYPE_CHANGE: 1649 ip_mc_remap(in_dev); 1650 break; 1651 case NETDEV_CHANGEMTU: 1652 if (inetdev_valid_mtu(dev->mtu)) 1653 break; 1654 /* disable IP when MTU is not enough */ 1655 fallthrough; 1656 case NETDEV_UNREGISTER: 1657 inetdev_destroy(in_dev); 1658 break; 1659 case NETDEV_CHANGENAME: 1660 /* Do not notify about label change, this event is 1661 * not interesting to applications using netlink. 1662 */ 1663 inetdev_changename(dev, in_dev); 1664 1665 devinet_sysctl_unregister(in_dev); 1666 devinet_sysctl_register(in_dev); 1667 break; 1668 } 1669 out: 1670 return NOTIFY_DONE; 1671 } 1672 1673 static struct notifier_block ip_netdev_notifier = { 1674 .notifier_call = inetdev_event, 1675 }; 1676 1677 static size_t inet_nlmsg_size(void) 1678 { 1679 return NLMSG_ALIGN(sizeof(struct ifaddrmsg)) 1680 + nla_total_size(4) /* IFA_ADDRESS */ 1681 + nla_total_size(4) /* IFA_LOCAL */ 1682 + nla_total_size(4) /* IFA_BROADCAST */ 1683 + nla_total_size(IFNAMSIZ) /* IFA_LABEL */ 1684 + nla_total_size(4) /* IFA_FLAGS */ 1685 + nla_total_size(1) /* IFA_PROTO */ 1686 + nla_total_size(4) /* IFA_RT_PRIORITY */ 1687 + nla_total_size(sizeof(struct ifa_cacheinfo)); /* IFA_CACHEINFO */ 1688 } 1689 1690 static inline u32 cstamp_delta(unsigned long cstamp) 1691 { 1692 return (cstamp - INITIAL_JIFFIES) * 100UL / HZ; 1693 } 1694 1695 static int put_cacheinfo(struct sk_buff *skb, unsigned long cstamp, 1696 unsigned long tstamp, u32 preferred, u32 valid) 1697 { 1698 struct ifa_cacheinfo ci; 1699 1700 ci.cstamp = cstamp_delta(cstamp); 1701 ci.tstamp = cstamp_delta(tstamp); 1702 ci.ifa_prefered = preferred; 1703 ci.ifa_valid = valid; 1704 1705 return nla_put(skb, IFA_CACHEINFO, sizeof(ci), &ci); 1706 } 1707 1708 static int inet_fill_ifaddr(struct sk_buff *skb, const struct in_ifaddr *ifa, 1709 struct inet_fill_args *args) 1710 { 1711 struct ifaddrmsg *ifm; 1712 struct nlmsghdr *nlh; 1713 unsigned long tstamp; 1714 u32 preferred, valid; 1715 u32 flags; 1716 1717 nlh = nlmsg_put(skb, args->portid, args->seq, args->event, sizeof(*ifm), 1718 args->flags); 1719 if (!nlh) 1720 return -EMSGSIZE; 1721 1722 ifm = nlmsg_data(nlh); 1723 ifm->ifa_family = AF_INET; 1724 ifm->ifa_prefixlen = ifa->ifa_prefixlen; 1725 1726 flags = READ_ONCE(ifa->ifa_flags); 1727 /* Warning : ifm->ifa_flags is an __u8, it holds only 8 bits. 1728 * The 32bit value is given in IFA_FLAGS attribute. 1729 */ 1730 ifm->ifa_flags = (__u8)flags; 1731 1732 ifm->ifa_scope = ifa->ifa_scope; 1733 ifm->ifa_index = ifa->ifa_dev->dev->ifindex; 1734 1735 if (args->netnsid >= 0 && 1736 nla_put_s32(skb, IFA_TARGET_NETNSID, args->netnsid)) 1737 goto nla_put_failure; 1738 1739 tstamp = READ_ONCE(ifa->ifa_tstamp); 1740 if (!(flags & IFA_F_PERMANENT)) { 1741 preferred = READ_ONCE(ifa->ifa_preferred_lft); 1742 valid = READ_ONCE(ifa->ifa_valid_lft); 1743 if (preferred != INFINITY_LIFE_TIME) { 1744 long tval = (jiffies - tstamp) / HZ; 1745 1746 if (preferred > tval) 1747 preferred -= tval; 1748 else 1749 preferred = 0; 1750 if (valid != INFINITY_LIFE_TIME) { 1751 if (valid > tval) 1752 valid -= tval; 1753 else 1754 valid = 0; 1755 } 1756 } 1757 } else { 1758 preferred = INFINITY_LIFE_TIME; 1759 valid = INFINITY_LIFE_TIME; 1760 } 1761 if ((ifa->ifa_address && 1762 nla_put_in_addr(skb, IFA_ADDRESS, ifa->ifa_address)) || 1763 (ifa->ifa_local && 1764 nla_put_in_addr(skb, IFA_LOCAL, ifa->ifa_local)) || 1765 (ifa->ifa_broadcast && 1766 nla_put_in_addr(skb, IFA_BROADCAST, ifa->ifa_broadcast)) || 1767 (ifa->ifa_label[0] && 1768 nla_put_string(skb, IFA_LABEL, ifa->ifa_label)) || 1769 (ifa->ifa_proto && 1770 nla_put_u8(skb, IFA_PROTO, ifa->ifa_proto)) || 1771 nla_put_u32(skb, IFA_FLAGS, flags) || 1772 (ifa->ifa_rt_priority && 1773 nla_put_u32(skb, IFA_RT_PRIORITY, ifa->ifa_rt_priority)) || 1774 put_cacheinfo(skb, READ_ONCE(ifa->ifa_cstamp), tstamp, 1775 preferred, valid)) 1776 goto nla_put_failure; 1777 1778 nlmsg_end(skb, nlh); 1779 return 0; 1780 1781 nla_put_failure: 1782 nlmsg_cancel(skb, nlh); 1783 return -EMSGSIZE; 1784 } 1785 1786 static int inet_valid_dump_ifaddr_req(const struct nlmsghdr *nlh, 1787 struct inet_fill_args *fillargs, 1788 struct net **tgt_net, struct sock *sk, 1789 struct netlink_callback *cb) 1790 { 1791 struct netlink_ext_ack *extack = cb->extack; 1792 struct nlattr *tb[IFA_MAX+1]; 1793 struct ifaddrmsg *ifm; 1794 int err, i; 1795 1796 ifm = nlmsg_payload(nlh, sizeof(*ifm)); 1797 if (!ifm) { 1798 NL_SET_ERR_MSG(extack, "ipv4: Invalid header for address dump request"); 1799 return -EINVAL; 1800 } 1801 1802 if (ifm->ifa_prefixlen || ifm->ifa_flags || ifm->ifa_scope) { 1803 NL_SET_ERR_MSG(extack, "ipv4: Invalid values in header for address dump request"); 1804 return -EINVAL; 1805 } 1806 1807 fillargs->ifindex = ifm->ifa_index; 1808 if (fillargs->ifindex) { 1809 cb->answer_flags |= NLM_F_DUMP_FILTERED; 1810 fillargs->flags |= NLM_F_DUMP_FILTERED; 1811 } 1812 1813 err = nlmsg_parse_deprecated_strict(nlh, sizeof(*ifm), tb, IFA_MAX, 1814 ifa_ipv4_policy, extack); 1815 if (err < 0) 1816 return err; 1817 1818 for (i = 0; i <= IFA_MAX; ++i) { 1819 if (!tb[i]) 1820 continue; 1821 1822 if (i == IFA_TARGET_NETNSID) { 1823 struct net *net; 1824 1825 fillargs->netnsid = nla_get_s32(tb[i]); 1826 1827 net = rtnl_get_net_ns_capable(sk, fillargs->netnsid); 1828 if (IS_ERR(net)) { 1829 fillargs->netnsid = -1; 1830 NL_SET_ERR_MSG(extack, "ipv4: Invalid target network namespace id"); 1831 return PTR_ERR(net); 1832 } 1833 *tgt_net = net; 1834 } else { 1835 NL_SET_ERR_MSG(extack, "ipv4: Unsupported attribute in dump request"); 1836 return -EINVAL; 1837 } 1838 } 1839 1840 return 0; 1841 } 1842 1843 static int in_dev_dump_ifmcaddr(struct in_device *in_dev, struct sk_buff *skb, 1844 struct netlink_callback *cb, int *s_ip_idx, 1845 struct inet_fill_args *fillargs) 1846 { 1847 struct ip_mc_list *im; 1848 int ip_idx = 0; 1849 int err; 1850 1851 for (im = rcu_dereference(in_dev->mc_list); 1852 im; 1853 im = rcu_dereference(im->next_rcu)) { 1854 if (ip_idx < *s_ip_idx) { 1855 ip_idx++; 1856 continue; 1857 } 1858 err = inet_fill_ifmcaddr(skb, in_dev->dev, im, fillargs); 1859 if (err < 0) 1860 goto done; 1861 1862 nl_dump_check_consistent(cb, nlmsg_hdr(skb)); 1863 ip_idx++; 1864 } 1865 err = 0; 1866 ip_idx = 0; 1867 done: 1868 *s_ip_idx = ip_idx; 1869 return err; 1870 } 1871 1872 static int in_dev_dump_ifaddr(struct in_device *in_dev, struct sk_buff *skb, 1873 struct netlink_callback *cb, int *s_ip_idx, 1874 struct inet_fill_args *fillargs) 1875 { 1876 struct in_ifaddr *ifa; 1877 int ip_idx = 0; 1878 int err; 1879 1880 in_dev_for_each_ifa_rcu(ifa, in_dev) { 1881 if (ip_idx < *s_ip_idx) { 1882 ip_idx++; 1883 continue; 1884 } 1885 err = inet_fill_ifaddr(skb, ifa, fillargs); 1886 if (err < 0) 1887 goto done; 1888 1889 nl_dump_check_consistent(cb, nlmsg_hdr(skb)); 1890 ip_idx++; 1891 } 1892 err = 0; 1893 ip_idx = 0; 1894 done: 1895 *s_ip_idx = ip_idx; 1896 1897 return err; 1898 } 1899 1900 static int in_dev_dump_addr(struct in_device *in_dev, struct sk_buff *skb, 1901 struct netlink_callback *cb, int *s_ip_idx, 1902 struct inet_fill_args *fillargs) 1903 { 1904 switch (fillargs->event) { 1905 case RTM_NEWADDR: 1906 return in_dev_dump_ifaddr(in_dev, skb, cb, s_ip_idx, fillargs); 1907 case RTM_GETMULTICAST: 1908 return in_dev_dump_ifmcaddr(in_dev, skb, cb, s_ip_idx, 1909 fillargs); 1910 default: 1911 return -EINVAL; 1912 } 1913 } 1914 1915 /* Combine dev_addr_genid and dev_base_seq to detect changes. 1916 */ 1917 static u32 inet_base_seq(const struct net *net) 1918 { 1919 u32 res = atomic_read(&net->ipv4.dev_addr_genid) + 1920 READ_ONCE(net->dev_base_seq); 1921 1922 /* Must not return 0 (see nl_dump_check_consistent()). 1923 * Chose a value far away from 0. 1924 */ 1925 if (!res) 1926 res = 0x80000000; 1927 return res; 1928 } 1929 1930 static int inet_dump_addr(struct sk_buff *skb, struct netlink_callback *cb, 1931 int event) 1932 { 1933 const struct nlmsghdr *nlh = cb->nlh; 1934 struct inet_fill_args fillargs = { 1935 .portid = NETLINK_CB(cb->skb).portid, 1936 .seq = nlh->nlmsg_seq, 1937 .event = event, 1938 .flags = NLM_F_MULTI, 1939 .netnsid = -1, 1940 }; 1941 struct net *net = sock_net(skb->sk); 1942 struct net *tgt_net = net; 1943 struct { 1944 unsigned long ifindex; 1945 int ip_idx; 1946 } *ctx = (void *)cb->ctx; 1947 struct in_device *in_dev; 1948 struct net_device *dev; 1949 int err = 0; 1950 1951 rcu_read_lock(); 1952 if (cb->strict_check) { 1953 err = inet_valid_dump_ifaddr_req(nlh, &fillargs, &tgt_net, 1954 skb->sk, cb); 1955 if (err < 0) 1956 goto done; 1957 1958 if (fillargs.ifindex) { 1959 dev = dev_get_by_index_rcu(tgt_net, fillargs.ifindex); 1960 if (!dev) { 1961 err = -ENODEV; 1962 goto done; 1963 } 1964 in_dev = __in_dev_get_rcu(dev); 1965 if (!in_dev) 1966 goto done; 1967 err = in_dev_dump_addr(in_dev, skb, cb, &ctx->ip_idx, 1968 &fillargs); 1969 goto done; 1970 } 1971 } 1972 1973 cb->seq = inet_base_seq(tgt_net); 1974 1975 for_each_netdev_dump(tgt_net, dev, ctx->ifindex) { 1976 in_dev = __in_dev_get_rcu(dev); 1977 if (!in_dev) 1978 continue; 1979 err = in_dev_dump_addr(in_dev, skb, cb, &ctx->ip_idx, 1980 &fillargs); 1981 if (err < 0) 1982 goto done; 1983 } 1984 done: 1985 if (fillargs.netnsid >= 0) 1986 put_net(tgt_net); 1987 rcu_read_unlock(); 1988 return err; 1989 } 1990 1991 static int inet_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb) 1992 { 1993 return inet_dump_addr(skb, cb, RTM_NEWADDR); 1994 } 1995 1996 static int inet_dump_ifmcaddr(struct sk_buff *skb, struct netlink_callback *cb) 1997 { 1998 return inet_dump_addr(skb, cb, RTM_GETMULTICAST); 1999 } 2000 2001 static void rtmsg_ifa(int event, struct in_ifaddr *ifa, struct nlmsghdr *nlh, 2002 u32 portid) 2003 { 2004 struct inet_fill_args fillargs = { 2005 .portid = portid, 2006 .seq = nlh ? nlh->nlmsg_seq : 0, 2007 .event = event, 2008 .flags = 0, 2009 .netnsid = -1, 2010 }; 2011 struct sk_buff *skb; 2012 int err = -ENOBUFS; 2013 struct net *net; 2014 2015 net = dev_net(ifa->ifa_dev->dev); 2016 skb = nlmsg_new(inet_nlmsg_size(), GFP_KERNEL); 2017 if (!skb) 2018 goto errout; 2019 2020 err = inet_fill_ifaddr(skb, ifa, &fillargs); 2021 if (err < 0) { 2022 /* -EMSGSIZE implies BUG in inet_nlmsg_size() */ 2023 WARN_ON(err == -EMSGSIZE); 2024 kfree_skb(skb); 2025 goto errout; 2026 } 2027 rtnl_notify(skb, net, portid, RTNLGRP_IPV4_IFADDR, nlh, GFP_KERNEL); 2028 return; 2029 errout: 2030 rtnl_set_sk_err(net, RTNLGRP_IPV4_IFADDR, err); 2031 } 2032 2033 static size_t inet_get_link_af_size(const struct net_device *dev, 2034 u32 ext_filter_mask) 2035 { 2036 struct in_device *in_dev = rcu_dereference_rtnl(dev->ip_ptr); 2037 2038 if (!in_dev) 2039 return 0; 2040 2041 return nla_total_size(IPV4_DEVCONF_MAX * 4); /* IFLA_INET_CONF */ 2042 } 2043 2044 static int inet_fill_link_af(struct sk_buff *skb, const struct net_device *dev, 2045 u32 ext_filter_mask) 2046 { 2047 struct in_device *in_dev = rcu_dereference_rtnl(dev->ip_ptr); 2048 struct nlattr *nla; 2049 int i; 2050 2051 if (!in_dev) 2052 return -ENODATA; 2053 2054 nla = nla_reserve(skb, IFLA_INET_CONF, IPV4_DEVCONF_MAX * 4); 2055 if (!nla) 2056 return -EMSGSIZE; 2057 2058 for (i = 0; i < IPV4_DEVCONF_MAX; i++) 2059 ((u32 *) nla_data(nla))[i] = READ_ONCE(in_dev->cnf.data[i]); 2060 2061 return 0; 2062 } 2063 2064 static const struct nla_policy inet_af_policy[IFLA_INET_MAX+1] = { 2065 [IFLA_INET_CONF] = { .type = NLA_NESTED }, 2066 }; 2067 2068 static const struct nla_policy inet_devconf_policy[IPV4_DEVCONF_MAX + 1] = { 2069 [IPV4_DEVCONF_FORWARDING] = NLA_POLICY_RANGE(NLA_U32, 0, 1), 2070 [IPV4_DEVCONF_MC_FORWARDING] = { .type = NLA_REJECT }, 2071 [IPV4_DEVCONF_PROXY_ARP] = NLA_POLICY_RANGE(NLA_U32, 0, 1), 2072 [IPV4_DEVCONF_ACCEPT_REDIRECTS] = NLA_POLICY_RANGE(NLA_U32, 0, 1), 2073 [IPV4_DEVCONF_SECURE_REDIRECTS] = NLA_POLICY_RANGE(NLA_U32, 0, 1), 2074 [IPV4_DEVCONF_SEND_REDIRECTS] = NLA_POLICY_RANGE(NLA_U32, 0, 1), 2075 [IPV4_DEVCONF_SHARED_MEDIA] = NLA_POLICY_RANGE(NLA_U32, 0, 1), 2076 [IPV4_DEVCONF_RP_FILTER] = NLA_POLICY_RANGE(NLA_U32, 0, 2), 2077 [IPV4_DEVCONF_ACCEPT_SOURCE_ROUTE] = NLA_POLICY_RANGE(NLA_U32, 0, 1), 2078 [IPV4_DEVCONF_BOOTP_RELAY] = NLA_POLICY_RANGE(NLA_U32, 0, 1), 2079 [IPV4_DEVCONF_LOG_MARTIANS] = NLA_POLICY_RANGE(NLA_U32, 0, 1), 2080 [IPV4_DEVCONF_TAG] = { .type = NLA_U32 }, 2081 [IPV4_DEVCONF_ARPFILTER] = NLA_POLICY_RANGE(NLA_U32, 0, 1), 2082 [IPV4_DEVCONF_MEDIUM_ID] = NLA_POLICY_MIN(NLA_S32, -1), 2083 [IPV4_DEVCONF_NOXFRM] = NLA_POLICY_RANGE(NLA_U32, 0, 1), 2084 [IPV4_DEVCONF_NOPOLICY] = NLA_POLICY_RANGE(NLA_U32, 0, 1), 2085 [IPV4_DEVCONF_FORCE_IGMP_VERSION] = NLA_POLICY_RANGE(NLA_U32, 0, 3), 2086 [IPV4_DEVCONF_ARP_ANNOUNCE] = NLA_POLICY_RANGE(NLA_U32, 0, 2), 2087 [IPV4_DEVCONF_ARP_IGNORE] = NLA_POLICY_RANGE(NLA_U32, 0, 8), 2088 [IPV4_DEVCONF_PROMOTE_SECONDARIES] = NLA_POLICY_RANGE(NLA_U32, 0, 1), 2089 [IPV4_DEVCONF_ARP_ACCEPT] = NLA_POLICY_RANGE(NLA_U32, 0, 2), 2090 [IPV4_DEVCONF_ARP_NOTIFY] = NLA_POLICY_RANGE(NLA_U32, 0, 1), 2091 [IPV4_DEVCONF_ACCEPT_LOCAL] = NLA_POLICY_RANGE(NLA_U32, 0, 1), 2092 [IPV4_DEVCONF_SRC_VMARK] = NLA_POLICY_RANGE(NLA_U32, 0, 1), 2093 [IPV4_DEVCONF_PROXY_ARP_PVLAN] = NLA_POLICY_RANGE(NLA_U32, 0, 1), 2094 [IPV4_DEVCONF_ROUTE_LOCALNET] = NLA_POLICY_RANGE(NLA_U32, 0, 1), 2095 [IPV4_DEVCONF_BC_FORWARDING] = NLA_POLICY_RANGE(NLA_U32, 0, 1), 2096 [IPV4_DEVCONF_IGMPV2_UNSOLICITED_REPORT_INTERVAL] = { .type = NLA_U32 }, 2097 [IPV4_DEVCONF_IGMPV3_UNSOLICITED_REPORT_INTERVAL] = { .type = NLA_U32 }, 2098 [IPV4_DEVCONF_IGNORE_ROUTES_WITH_LINKDOWN] = 2099 NLA_POLICY_RANGE(NLA_U32, 0, 1), 2100 [IPV4_DEVCONF_DROP_UNICAST_IN_L2_MULTICAST] = 2101 NLA_POLICY_RANGE(NLA_U32, 0, 1), 2102 [IPV4_DEVCONF_DROP_GRATUITOUS_ARP] = NLA_POLICY_RANGE(NLA_U32, 0, 1), 2103 [IPV4_DEVCONF_ARP_EVICT_NOCARRIER] = NLA_POLICY_RANGE(NLA_U32, 0, 1), 2104 }; 2105 2106 static int inet_validate_link_af(const struct net_device *dev, 2107 const struct nlattr *nla, 2108 struct netlink_ext_ack *extack) 2109 { 2110 struct nlattr *tb[IFLA_INET_MAX + 1], *nested_tb[IPV4_DEVCONF_MAX + 1]; 2111 int err; 2112 2113 if (dev && !__in_dev_get_rtnl(dev)) 2114 return -EAFNOSUPPORT; 2115 2116 err = nla_parse_nested_deprecated(tb, IFLA_INET_MAX, nla, 2117 inet_af_policy, extack); 2118 if (err < 0) 2119 return err; 2120 2121 if (tb[IFLA_INET_CONF]) { 2122 err = nla_parse(nested_tb, IPV4_DEVCONF_MAX, 2123 nla_data(tb[IFLA_INET_CONF]), 2124 nla_len(tb[IFLA_INET_CONF]), 2125 inet_devconf_policy, extack); 2126 2127 if (err < 0) 2128 return err; 2129 } 2130 2131 return 0; 2132 } 2133 2134 static bool devinet_conf_post_set(struct net *net, struct ipv4_devconf *cnf, 2135 int attr, int new, int old, int ifindex) 2136 { 2137 if (new == old) 2138 return false; 2139 2140 switch (attr) { 2141 case IPV4_DEVCONF_ROUTE_LOCALNET: 2142 case IPV4_DEVCONF_ACCEPT_LOCAL: 2143 if (new == 0) 2144 return true; 2145 break; 2146 case IPV4_DEVCONF_NOXFRM: 2147 case IPV4_DEVCONF_NOPOLICY: 2148 case IPV4_DEVCONF_PROMOTE_SECONDARIES: 2149 case IPV4_DEVCONF_DROP_UNICAST_IN_L2_MULTICAST: 2150 case IPV4_DEVCONF_BC_FORWARDING: 2151 return true; 2152 case IPV4_DEVCONF_RP_FILTER: 2153 inet_netconf_notify_devconf(net, RTM_NEWNETCONF, 2154 NETCONFA_RP_FILTER, 2155 ifindex, cnf); 2156 break; 2157 case IPV4_DEVCONF_PROXY_ARP: 2158 inet_netconf_notify_devconf(net, RTM_NEWNETCONF, 2159 NETCONFA_PROXY_NEIGH, 2160 ifindex, cnf); 2161 break; 2162 case IPV4_DEVCONF_IGNORE_ROUTES_WITH_LINKDOWN: 2163 inet_netconf_notify_devconf(net, RTM_NEWNETCONF, 2164 NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN, 2165 ifindex, cnf); 2166 break; 2167 case IPV4_DEVCONF_FORWARDING: 2168 if (new == 1) { 2169 /* it is safe to use container_of() because forwarding case 2170 * is only used by the netlink path 2171 */ 2172 struct in_device *idev = container_of(cnf, struct in_device, cnf); 2173 2174 netif_disable_lro(idev->dev); 2175 } 2176 2177 inet_netconf_notify_devconf(net, RTM_NEWNETCONF, 2178 NETCONFA_FORWARDING, 2179 ifindex, cnf); 2180 return true; 2181 default: 2182 break; 2183 } 2184 2185 return false; 2186 } 2187 2188 static int inet_set_link_af(struct net_device *dev, const struct nlattr *nla, 2189 struct netlink_ext_ack *extack) 2190 { 2191 struct in_device *in_dev = __in_dev_get_rtnl(dev); 2192 struct nlattr *a, *tb[IFLA_INET_MAX+1]; 2193 struct net *net = dev_net(dev); 2194 bool flush_cache = false; 2195 int rem; 2196 2197 if (!in_dev) 2198 return -EAFNOSUPPORT; 2199 2200 if (nla_parse_nested_deprecated(tb, IFLA_INET_MAX, nla, NULL, NULL) < 0) 2201 return -EINVAL; 2202 2203 if (tb[IFLA_INET_CONF]) { 2204 nla_for_each_nested(a, tb[IFLA_INET_CONF], rem) { 2205 int old_value = ipv4_devconf_get(in_dev, nla_type(a)); 2206 int new_value = nla_get_u32(a); 2207 2208 ipv4_devconf_set(in_dev, nla_type(a), new_value); 2209 if (devinet_conf_post_set(net, &in_dev->cnf, nla_type(a), new_value, 2210 old_value, dev->ifindex)) 2211 flush_cache = true; 2212 } 2213 if (flush_cache) 2214 rt_cache_flush(net); 2215 } 2216 2217 return 0; 2218 } 2219 2220 static int inet_netconf_msgsize_devconf(int type) 2221 { 2222 int size = NLMSG_ALIGN(sizeof(struct netconfmsg)) 2223 + nla_total_size(4); /* NETCONFA_IFINDEX */ 2224 bool all = false; 2225 2226 if (type == NETCONFA_ALL) 2227 all = true; 2228 2229 if (all || type == NETCONFA_FORWARDING) 2230 size += nla_total_size(4); 2231 if (all || type == NETCONFA_RP_FILTER) 2232 size += nla_total_size(4); 2233 if (all || type == NETCONFA_MC_FORWARDING) 2234 size += nla_total_size(4); 2235 if (all || type == NETCONFA_BC_FORWARDING) 2236 size += nla_total_size(4); 2237 if (all || type == NETCONFA_PROXY_NEIGH) 2238 size += nla_total_size(4); 2239 if (all || type == NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN) 2240 size += nla_total_size(4); 2241 2242 return size; 2243 } 2244 2245 static int inet_netconf_fill_devconf(struct sk_buff *skb, int ifindex, 2246 const struct ipv4_devconf *devconf, 2247 u32 portid, u32 seq, int event, 2248 unsigned int flags, int type) 2249 { 2250 struct nlmsghdr *nlh; 2251 struct netconfmsg *ncm; 2252 bool all = false; 2253 2254 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct netconfmsg), 2255 flags); 2256 if (!nlh) 2257 return -EMSGSIZE; 2258 2259 if (type == NETCONFA_ALL) 2260 all = true; 2261 2262 ncm = nlmsg_data(nlh); 2263 ncm->ncm_family = AF_INET; 2264 2265 if (nla_put_s32(skb, NETCONFA_IFINDEX, ifindex) < 0) 2266 goto nla_put_failure; 2267 2268 if (!devconf) 2269 goto out; 2270 2271 if ((all || type == NETCONFA_FORWARDING) && 2272 nla_put_s32(skb, NETCONFA_FORWARDING, 2273 IPV4_DEVCONF_RO(*devconf, FORWARDING)) < 0) 2274 goto nla_put_failure; 2275 if ((all || type == NETCONFA_RP_FILTER) && 2276 nla_put_s32(skb, NETCONFA_RP_FILTER, 2277 IPV4_DEVCONF_RO(*devconf, RP_FILTER)) < 0) 2278 goto nla_put_failure; 2279 if ((all || type == NETCONFA_MC_FORWARDING) && 2280 nla_put_s32(skb, NETCONFA_MC_FORWARDING, 2281 IPV4_DEVCONF_RO(*devconf, MC_FORWARDING)) < 0) 2282 goto nla_put_failure; 2283 if ((all || type == NETCONFA_BC_FORWARDING) && 2284 nla_put_s32(skb, NETCONFA_BC_FORWARDING, 2285 IPV4_DEVCONF_RO(*devconf, BC_FORWARDING)) < 0) 2286 goto nla_put_failure; 2287 if ((all || type == NETCONFA_PROXY_NEIGH) && 2288 nla_put_s32(skb, NETCONFA_PROXY_NEIGH, 2289 IPV4_DEVCONF_RO(*devconf, PROXY_ARP)) < 0) 2290 goto nla_put_failure; 2291 if ((all || type == NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN) && 2292 nla_put_s32(skb, NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN, 2293 IPV4_DEVCONF_RO(*devconf, 2294 IGNORE_ROUTES_WITH_LINKDOWN)) < 0) 2295 goto nla_put_failure; 2296 2297 out: 2298 nlmsg_end(skb, nlh); 2299 return 0; 2300 2301 nla_put_failure: 2302 nlmsg_cancel(skb, nlh); 2303 return -EMSGSIZE; 2304 } 2305 2306 void inet_netconf_notify_devconf(struct net *net, int event, int type, 2307 int ifindex, struct ipv4_devconf *devconf) 2308 { 2309 struct sk_buff *skb; 2310 int err = -ENOBUFS; 2311 2312 skb = nlmsg_new(inet_netconf_msgsize_devconf(type), GFP_KERNEL); 2313 if (!skb) 2314 goto errout; 2315 2316 err = inet_netconf_fill_devconf(skb, ifindex, devconf, 0, 0, 2317 event, 0, type); 2318 if (err < 0) { 2319 /* -EMSGSIZE implies BUG in inet_netconf_msgsize_devconf() */ 2320 WARN_ON(err == -EMSGSIZE); 2321 kfree_skb(skb); 2322 goto errout; 2323 } 2324 rtnl_notify(skb, net, 0, RTNLGRP_IPV4_NETCONF, NULL, GFP_KERNEL); 2325 return; 2326 errout: 2327 rtnl_set_sk_err(net, RTNLGRP_IPV4_NETCONF, err); 2328 } 2329 2330 static const struct nla_policy devconf_ipv4_policy[NETCONFA_MAX+1] = { 2331 [NETCONFA_IFINDEX] = { .len = sizeof(int) }, 2332 [NETCONFA_FORWARDING] = { .len = sizeof(int) }, 2333 [NETCONFA_RP_FILTER] = { .len = sizeof(int) }, 2334 [NETCONFA_PROXY_NEIGH] = { .len = sizeof(int) }, 2335 [NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN] = { .len = sizeof(int) }, 2336 }; 2337 2338 static int inet_netconf_valid_get_req(struct sk_buff *skb, 2339 const struct nlmsghdr *nlh, 2340 struct nlattr **tb, 2341 struct netlink_ext_ack *extack) 2342 { 2343 int i, err; 2344 2345 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(struct netconfmsg))) { 2346 NL_SET_ERR_MSG(extack, "ipv4: Invalid header for netconf get request"); 2347 return -EINVAL; 2348 } 2349 2350 if (!netlink_strict_get_check(skb)) 2351 return nlmsg_parse_deprecated(nlh, sizeof(struct netconfmsg), 2352 tb, NETCONFA_MAX, 2353 devconf_ipv4_policy, extack); 2354 2355 err = nlmsg_parse_deprecated_strict(nlh, sizeof(struct netconfmsg), 2356 tb, NETCONFA_MAX, 2357 devconf_ipv4_policy, extack); 2358 if (err) 2359 return err; 2360 2361 for (i = 0; i <= NETCONFA_MAX; i++) { 2362 if (!tb[i]) 2363 continue; 2364 2365 switch (i) { 2366 case NETCONFA_IFINDEX: 2367 break; 2368 default: 2369 NL_SET_ERR_MSG(extack, "ipv4: Unsupported attribute in netconf get request"); 2370 return -EINVAL; 2371 } 2372 } 2373 2374 return 0; 2375 } 2376 2377 static int inet_netconf_get_devconf(struct sk_buff *in_skb, 2378 struct nlmsghdr *nlh, 2379 struct netlink_ext_ack *extack) 2380 { 2381 struct net *net = sock_net(in_skb->sk); 2382 struct nlattr *tb[NETCONFA_MAX + 1]; 2383 const struct ipv4_devconf *devconf; 2384 struct in_device *in_dev = NULL; 2385 struct net_device *dev = NULL; 2386 struct sk_buff *skb; 2387 int ifindex; 2388 int err; 2389 2390 err = inet_netconf_valid_get_req(in_skb, nlh, tb, extack); 2391 if (err) 2392 return err; 2393 2394 if (!tb[NETCONFA_IFINDEX]) 2395 return -EINVAL; 2396 2397 ifindex = nla_get_s32(tb[NETCONFA_IFINDEX]); 2398 switch (ifindex) { 2399 case NETCONFA_IFINDEX_ALL: 2400 devconf = net->ipv4.devconf_all; 2401 break; 2402 case NETCONFA_IFINDEX_DEFAULT: 2403 devconf = net->ipv4.devconf_dflt; 2404 break; 2405 default: 2406 err = -ENODEV; 2407 dev = dev_get_by_index(net, ifindex); 2408 if (dev) 2409 in_dev = in_dev_get(dev); 2410 if (!in_dev) 2411 goto errout; 2412 devconf = &in_dev->cnf; 2413 break; 2414 } 2415 2416 err = -ENOBUFS; 2417 skb = nlmsg_new(inet_netconf_msgsize_devconf(NETCONFA_ALL), GFP_KERNEL); 2418 if (!skb) 2419 goto errout; 2420 2421 err = inet_netconf_fill_devconf(skb, ifindex, devconf, 2422 NETLINK_CB(in_skb).portid, 2423 nlh->nlmsg_seq, RTM_NEWNETCONF, 0, 2424 NETCONFA_ALL); 2425 if (err < 0) { 2426 /* -EMSGSIZE implies BUG in inet_netconf_msgsize_devconf() */ 2427 WARN_ON(err == -EMSGSIZE); 2428 kfree_skb(skb); 2429 goto errout; 2430 } 2431 err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid); 2432 errout: 2433 if (in_dev) 2434 in_dev_put(in_dev); 2435 dev_put(dev); 2436 return err; 2437 } 2438 2439 static int inet_netconf_dump_devconf(struct sk_buff *skb, 2440 struct netlink_callback *cb) 2441 { 2442 const struct nlmsghdr *nlh = cb->nlh; 2443 struct net *net = sock_net(skb->sk); 2444 struct { 2445 unsigned long ifindex; 2446 unsigned int all_default; 2447 } *ctx = (void *)cb->ctx; 2448 const struct in_device *in_dev; 2449 struct net_device *dev; 2450 int err = 0; 2451 2452 if (cb->strict_check) { 2453 struct netlink_ext_ack *extack = cb->extack; 2454 struct netconfmsg *ncm; 2455 2456 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ncm))) { 2457 NL_SET_ERR_MSG(extack, "ipv4: Invalid header for netconf dump request"); 2458 return -EINVAL; 2459 } 2460 2461 if (nlmsg_attrlen(nlh, sizeof(*ncm))) { 2462 NL_SET_ERR_MSG(extack, "ipv4: Invalid data after header in netconf dump request"); 2463 return -EINVAL; 2464 } 2465 } 2466 2467 rcu_read_lock(); 2468 for_each_netdev_dump(net, dev, ctx->ifindex) { 2469 in_dev = __in_dev_get_rcu(dev); 2470 if (!in_dev) 2471 continue; 2472 err = inet_netconf_fill_devconf(skb, dev->ifindex, 2473 &in_dev->cnf, 2474 NETLINK_CB(cb->skb).portid, 2475 nlh->nlmsg_seq, 2476 RTM_NEWNETCONF, NLM_F_MULTI, 2477 NETCONFA_ALL); 2478 if (err < 0) 2479 goto done; 2480 } 2481 if (ctx->all_default == 0) { 2482 err = inet_netconf_fill_devconf(skb, NETCONFA_IFINDEX_ALL, 2483 net->ipv4.devconf_all, 2484 NETLINK_CB(cb->skb).portid, 2485 nlh->nlmsg_seq, 2486 RTM_NEWNETCONF, NLM_F_MULTI, 2487 NETCONFA_ALL); 2488 if (err < 0) 2489 goto done; 2490 ctx->all_default++; 2491 } 2492 if (ctx->all_default == 1) { 2493 err = inet_netconf_fill_devconf(skb, NETCONFA_IFINDEX_DEFAULT, 2494 net->ipv4.devconf_dflt, 2495 NETLINK_CB(cb->skb).portid, 2496 nlh->nlmsg_seq, 2497 RTM_NEWNETCONF, NLM_F_MULTI, 2498 NETCONFA_ALL); 2499 if (err < 0) 2500 goto done; 2501 ctx->all_default++; 2502 } 2503 done: 2504 rcu_read_unlock(); 2505 return err; 2506 } 2507 2508 #ifdef CONFIG_SYSCTL 2509 2510 static void devinet_copy_dflt_conf(struct net *net, int i) 2511 { 2512 struct net_device *dev; 2513 2514 rcu_read_lock(); 2515 for_each_netdev_rcu(net, dev) { 2516 struct in_device *in_dev; 2517 2518 in_dev = __in_dev_get_rcu(dev); 2519 if (in_dev && !test_bit(i, in_dev->cnf.state)) 2520 in_dev->cnf.data[i] = net->ipv4.devconf_dflt->data[i]; 2521 } 2522 rcu_read_unlock(); 2523 } 2524 2525 /* called with RTNL locked */ 2526 static void inet_forward_change(struct net *net) 2527 { 2528 struct net_device *dev; 2529 int on = IPV4_DEVCONF_ALL(net, FORWARDING); 2530 2531 IPV4_DEVCONF_ALL(net, ACCEPT_REDIRECTS) = !on; 2532 IPV4_DEVCONF_DFLT(net, FORWARDING) = on; 2533 inet_netconf_notify_devconf(net, RTM_NEWNETCONF, 2534 NETCONFA_FORWARDING, 2535 NETCONFA_IFINDEX_ALL, 2536 net->ipv4.devconf_all); 2537 inet_netconf_notify_devconf(net, RTM_NEWNETCONF, 2538 NETCONFA_FORWARDING, 2539 NETCONFA_IFINDEX_DEFAULT, 2540 net->ipv4.devconf_dflt); 2541 2542 for_each_netdev(net, dev) { 2543 struct in_device *in_dev; 2544 2545 if (on) 2546 dev_disable_lro(dev); 2547 2548 in_dev = __in_dev_get_rtnl_net(dev); 2549 if (in_dev) { 2550 IN_DEV_CONF_SET(in_dev, FORWARDING, on); 2551 inet_netconf_notify_devconf(net, RTM_NEWNETCONF, 2552 NETCONFA_FORWARDING, 2553 dev->ifindex, &in_dev->cnf); 2554 } 2555 } 2556 } 2557 2558 static int devinet_conf_ifindex(struct net *net, struct ipv4_devconf *cnf) 2559 { 2560 if (cnf == net->ipv4.devconf_dflt) 2561 return NETCONFA_IFINDEX_DEFAULT; 2562 else if (cnf == net->ipv4.devconf_all) 2563 return NETCONFA_IFINDEX_ALL; 2564 else { 2565 struct in_device *idev 2566 = container_of(cnf, struct in_device, cnf); 2567 return idev->dev->ifindex; 2568 } 2569 } 2570 2571 static int devinet_conf_proc(const struct ctl_table *ctl, int write, 2572 void *buffer, size_t *lenp, loff_t *ppos) 2573 { 2574 int old_value = *(int *)ctl->data; 2575 int ret = proc_dointvec(ctl, write, buffer, lenp, ppos); 2576 int new_value = *(int *)ctl->data; 2577 2578 if (write) { 2579 struct ipv4_devconf *cnf = ctl->extra1; 2580 int i = (int *)ctl->data - cnf->data; 2581 struct net *net = ctl->extra2; 2582 int ifindex; 2583 2584 /* These attributes are bypassing the tracking state, 2585 * for the rest track the state and propagate the changes 2586 * to default config 2587 */ 2588 switch (i + 1) { 2589 case IPV4_DEVCONF_NOXFRM: 2590 case IPV4_DEVCONF_NOPOLICY: 2591 case IPV4_DEVCONF_PROMOTE_SECONDARIES: 2592 case IPV4_DEVCONF_DROP_UNICAST_IN_L2_MULTICAST: 2593 break; 2594 default: 2595 set_bit(i, cnf->state); 2596 if (cnf == net->ipv4.devconf_dflt) 2597 devinet_copy_dflt_conf(net, i); 2598 break; 2599 } 2600 2601 ifindex = devinet_conf_ifindex(net, cnf); 2602 if (devinet_conf_post_set(net, cnf, i + 1, new_value, 2603 old_value, ifindex)) 2604 rt_cache_flush(net); 2605 } 2606 2607 return ret; 2608 } 2609 2610 static int devinet_sysctl_forward(const struct ctl_table *ctl, int write, 2611 void *buffer, size_t *lenp, loff_t *ppos) 2612 { 2613 struct net *net = ctl->extra2; 2614 int *valp = ctl->data; 2615 int val = *valp; 2616 int ret; 2617 2618 if (write && !ns_capable(net->user_ns, CAP_NET_ADMIN)) 2619 return -EPERM; 2620 2621 ret = proc_dointvec(ctl, write, buffer, lenp, ppos); 2622 2623 if (write && *valp != val) { 2624 if (valp != &IPV4_DEVCONF_DFLT(net, FORWARDING)) { 2625 if (!rtnl_net_trylock(net)) { 2626 /* Restore the original values before restarting */ 2627 *valp = val; 2628 return restart_syscall(); 2629 } 2630 if (valp == &IPV4_DEVCONF_ALL(net, FORWARDING)) { 2631 inet_forward_change(net); 2632 } else { 2633 struct ipv4_devconf *cnf = ctl->extra1; 2634 struct in_device *idev = 2635 container_of(cnf, struct in_device, cnf); 2636 if (*valp) 2637 dev_disable_lro(idev->dev); 2638 inet_netconf_notify_devconf(net, RTM_NEWNETCONF, 2639 NETCONFA_FORWARDING, 2640 idev->dev->ifindex, 2641 cnf); 2642 } 2643 rtnl_net_unlock(net); 2644 rt_cache_flush(net); 2645 } else 2646 inet_netconf_notify_devconf(net, RTM_NEWNETCONF, 2647 NETCONFA_FORWARDING, 2648 NETCONFA_IFINDEX_DEFAULT, 2649 net->ipv4.devconf_dflt); 2650 } 2651 2652 return ret; 2653 } 2654 2655 #define DEVINET_SYSCTL_ENTRY(attr, name, mval, proc) \ 2656 { \ 2657 .procname = name, \ 2658 .data = ipv4_devconf.data + \ 2659 IPV4_DEVCONF_ ## attr - 1, \ 2660 .maxlen = sizeof(int), \ 2661 .mode = mval, \ 2662 .proc_handler = proc, \ 2663 .extra1 = &ipv4_devconf, \ 2664 } 2665 2666 #define DEVINET_SYSCTL_RW_ENTRY(attr, name) \ 2667 DEVINET_SYSCTL_ENTRY(attr, name, 0644, devinet_conf_proc) 2668 2669 #define DEVINET_SYSCTL_RO_ENTRY(attr, name) \ 2670 DEVINET_SYSCTL_ENTRY(attr, name, 0444, devinet_conf_proc) 2671 2672 #define DEVINET_SYSCTL_COMPLEX_ENTRY(attr, name, proc) \ 2673 DEVINET_SYSCTL_ENTRY(attr, name, 0644, proc) 2674 2675 static struct devinet_sysctl_table { 2676 struct ctl_table_header *sysctl_header; 2677 struct ctl_table devinet_vars[IPV4_DEVCONF_MAX]; 2678 } devinet_sysctl = { 2679 .devinet_vars = { 2680 DEVINET_SYSCTL_COMPLEX_ENTRY(FORWARDING, "forwarding", 2681 devinet_sysctl_forward), 2682 DEVINET_SYSCTL_RO_ENTRY(MC_FORWARDING, "mc_forwarding"), 2683 DEVINET_SYSCTL_RW_ENTRY(BC_FORWARDING, "bc_forwarding"), 2684 2685 DEVINET_SYSCTL_RW_ENTRY(ACCEPT_REDIRECTS, "accept_redirects"), 2686 DEVINET_SYSCTL_RW_ENTRY(SECURE_REDIRECTS, "secure_redirects"), 2687 DEVINET_SYSCTL_RW_ENTRY(SHARED_MEDIA, "shared_media"), 2688 DEVINET_SYSCTL_RW_ENTRY(RP_FILTER, "rp_filter"), 2689 DEVINET_SYSCTL_RW_ENTRY(SEND_REDIRECTS, "send_redirects"), 2690 DEVINET_SYSCTL_RW_ENTRY(ACCEPT_SOURCE_ROUTE, 2691 "accept_source_route"), 2692 DEVINET_SYSCTL_RW_ENTRY(ACCEPT_LOCAL, "accept_local"), 2693 DEVINET_SYSCTL_RW_ENTRY(SRC_VMARK, "src_valid_mark"), 2694 DEVINET_SYSCTL_RW_ENTRY(PROXY_ARP, "proxy_arp"), 2695 DEVINET_SYSCTL_RW_ENTRY(MEDIUM_ID, "medium_id"), 2696 DEVINET_SYSCTL_RW_ENTRY(BOOTP_RELAY, "bootp_relay"), 2697 DEVINET_SYSCTL_RW_ENTRY(LOG_MARTIANS, "log_martians"), 2698 DEVINET_SYSCTL_RW_ENTRY(TAG, "tag"), 2699 DEVINET_SYSCTL_RW_ENTRY(ARPFILTER, "arp_filter"), 2700 DEVINET_SYSCTL_RW_ENTRY(ARP_ANNOUNCE, "arp_announce"), 2701 DEVINET_SYSCTL_RW_ENTRY(ARP_IGNORE, "arp_ignore"), 2702 DEVINET_SYSCTL_RW_ENTRY(ARP_ACCEPT, "arp_accept"), 2703 DEVINET_SYSCTL_RW_ENTRY(ARP_NOTIFY, "arp_notify"), 2704 DEVINET_SYSCTL_RW_ENTRY(ARP_EVICT_NOCARRIER, 2705 "arp_evict_nocarrier"), 2706 DEVINET_SYSCTL_RW_ENTRY(PROXY_ARP_PVLAN, "proxy_arp_pvlan"), 2707 DEVINET_SYSCTL_RW_ENTRY(FORCE_IGMP_VERSION, 2708 "force_igmp_version"), 2709 DEVINET_SYSCTL_RW_ENTRY(IGMPV2_UNSOLICITED_REPORT_INTERVAL, 2710 "igmpv2_unsolicited_report_interval"), 2711 DEVINET_SYSCTL_RW_ENTRY(IGMPV3_UNSOLICITED_REPORT_INTERVAL, 2712 "igmpv3_unsolicited_report_interval"), 2713 DEVINET_SYSCTL_RW_ENTRY(IGNORE_ROUTES_WITH_LINKDOWN, 2714 "ignore_routes_with_linkdown"), 2715 DEVINET_SYSCTL_RW_ENTRY(DROP_GRATUITOUS_ARP, 2716 "drop_gratuitous_arp"), 2717 DEVINET_SYSCTL_RW_ENTRY(NOXFRM, "disable_xfrm"), 2718 DEVINET_SYSCTL_RW_ENTRY(NOPOLICY, "disable_policy"), 2719 DEVINET_SYSCTL_RW_ENTRY(PROMOTE_SECONDARIES, 2720 "promote_secondaries"), 2721 DEVINET_SYSCTL_RW_ENTRY(ROUTE_LOCALNET, 2722 "route_localnet"), 2723 DEVINET_SYSCTL_RW_ENTRY(DROP_UNICAST_IN_L2_MULTICAST, 2724 "drop_unicast_in_l2_multicast"), 2725 }, 2726 }; 2727 2728 static int __devinet_sysctl_register(struct net *net, char *dev_name, 2729 int ifindex, struct ipv4_devconf *p) 2730 { 2731 int i; 2732 struct devinet_sysctl_table *t; 2733 char path[sizeof("net/ipv4/conf/") + IFNAMSIZ]; 2734 2735 t = kmemdup(&devinet_sysctl, sizeof(*t), GFP_KERNEL_ACCOUNT); 2736 if (!t) 2737 goto out; 2738 2739 for (i = 0; i < ARRAY_SIZE(t->devinet_vars); i++) { 2740 t->devinet_vars[i].data += (char *)p - (char *)&ipv4_devconf; 2741 t->devinet_vars[i].extra1 = p; 2742 t->devinet_vars[i].extra2 = net; 2743 } 2744 2745 snprintf(path, sizeof(path), "net/ipv4/conf/%s", dev_name); 2746 2747 t->sysctl_header = register_net_sysctl(net, path, t->devinet_vars); 2748 if (!t->sysctl_header) 2749 goto free; 2750 2751 p->sysctl = t; 2752 2753 inet_netconf_notify_devconf(net, RTM_NEWNETCONF, NETCONFA_ALL, 2754 ifindex, p); 2755 return 0; 2756 2757 free: 2758 kfree(t); 2759 out: 2760 return -ENOMEM; 2761 } 2762 2763 static void __devinet_sysctl_unregister(struct net *net, 2764 struct ipv4_devconf *cnf, int ifindex) 2765 { 2766 struct devinet_sysctl_table *t = cnf->sysctl; 2767 2768 if (t) { 2769 cnf->sysctl = NULL; 2770 unregister_net_sysctl_table(t->sysctl_header); 2771 kfree(t); 2772 } 2773 2774 inet_netconf_notify_devconf(net, RTM_DELNETCONF, 0, ifindex, NULL); 2775 } 2776 2777 static int devinet_sysctl_register(struct in_device *idev) 2778 { 2779 int err; 2780 2781 if (!sysctl_dev_name_is_allowed(idev->dev->name)) 2782 return -EINVAL; 2783 2784 err = neigh_sysctl_register(idev->dev, idev->arp_parms, NULL); 2785 if (err) 2786 return err; 2787 err = __devinet_sysctl_register(dev_net(idev->dev), idev->dev->name, 2788 idev->dev->ifindex, &idev->cnf); 2789 if (err) 2790 neigh_sysctl_unregister(idev->arp_parms); 2791 return err; 2792 } 2793 2794 static void devinet_sysctl_unregister(struct in_device *idev) 2795 { 2796 struct net *net = dev_net(idev->dev); 2797 2798 __devinet_sysctl_unregister(net, &idev->cnf, idev->dev->ifindex); 2799 neigh_sysctl_unregister(idev->arp_parms); 2800 } 2801 2802 static const struct ctl_table ctl_forward_entry[] = { 2803 { 2804 .procname = "ip_forward", 2805 .data = &ipv4_devconf.data[ 2806 IPV4_DEVCONF_FORWARDING - 1], 2807 .maxlen = sizeof(int), 2808 .mode = 0644, 2809 .proc_handler = devinet_sysctl_forward, 2810 .extra1 = &ipv4_devconf, 2811 .extra2 = &init_net, 2812 }, 2813 }; 2814 #endif 2815 2816 static __net_init int devinet_init_net(struct net *net) 2817 { 2818 #ifdef CONFIG_SYSCTL 2819 struct ctl_table_header *forw_hdr; 2820 struct ctl_table *tbl; 2821 #endif 2822 struct ipv4_devconf *all, *dflt; 2823 int err; 2824 int i; 2825 2826 err = -ENOMEM; 2827 net->ipv4.inet_addr_lst = kmalloc_objs(struct hlist_head, 2828 IN4_ADDR_HSIZE); 2829 if (!net->ipv4.inet_addr_lst) 2830 goto err_alloc_hash; 2831 2832 all = kmemdup(&ipv4_devconf, sizeof(ipv4_devconf), GFP_KERNEL); 2833 if (!all) 2834 goto err_alloc_all; 2835 2836 dflt = kmemdup(&ipv4_devconf_dflt, sizeof(ipv4_devconf_dflt), GFP_KERNEL); 2837 if (!dflt) 2838 goto err_alloc_dflt; 2839 2840 #ifdef CONFIG_SYSCTL 2841 tbl = kmemdup(ctl_forward_entry, sizeof(ctl_forward_entry), GFP_KERNEL); 2842 if (!tbl) 2843 goto err_alloc_ctl; 2844 2845 tbl[0].data = &all->data[IPV4_DEVCONF_FORWARDING - 1]; 2846 tbl[0].extra1 = all; 2847 tbl[0].extra2 = net; 2848 #endif 2849 2850 if (!net_eq(net, &init_net)) { 2851 switch (net_inherit_devconf()) { 2852 case 3: 2853 /* copy from the current netns */ 2854 memcpy(all, current->nsproxy->net_ns->ipv4.devconf_all, 2855 sizeof(ipv4_devconf)); 2856 memcpy(dflt, 2857 current->nsproxy->net_ns->ipv4.devconf_dflt, 2858 sizeof(ipv4_devconf_dflt)); 2859 break; 2860 case 0: 2861 case 1: 2862 /* copy from init_net */ 2863 memcpy(all, init_net.ipv4.devconf_all, 2864 sizeof(ipv4_devconf)); 2865 memcpy(dflt, init_net.ipv4.devconf_dflt, 2866 sizeof(ipv4_devconf_dflt)); 2867 break; 2868 case 2: 2869 /* use compiled values */ 2870 break; 2871 } 2872 } 2873 2874 #ifdef CONFIG_SYSCTL 2875 err = __devinet_sysctl_register(net, "all", NETCONFA_IFINDEX_ALL, all); 2876 if (err < 0) 2877 goto err_reg_all; 2878 2879 err = __devinet_sysctl_register(net, "default", 2880 NETCONFA_IFINDEX_DEFAULT, dflt); 2881 if (err < 0) 2882 goto err_reg_dflt; 2883 2884 err = -ENOMEM; 2885 forw_hdr = register_net_sysctl_sz(net, "net/ipv4", tbl, 2886 ARRAY_SIZE(ctl_forward_entry)); 2887 if (!forw_hdr) 2888 goto err_reg_ctl; 2889 net->ipv4.forw_hdr = forw_hdr; 2890 #endif 2891 2892 for (i = 0; i < IN4_ADDR_HSIZE; i++) 2893 INIT_HLIST_HEAD(&net->ipv4.inet_addr_lst[i]); 2894 2895 INIT_DEFERRABLE_WORK(&net->ipv4.addr_chk_work, check_lifetime); 2896 2897 net->ipv4.devconf_all = all; 2898 net->ipv4.devconf_dflt = dflt; 2899 return 0; 2900 2901 #ifdef CONFIG_SYSCTL 2902 err_reg_ctl: 2903 __devinet_sysctl_unregister(net, dflt, NETCONFA_IFINDEX_DEFAULT); 2904 err_reg_dflt: 2905 __devinet_sysctl_unregister(net, all, NETCONFA_IFINDEX_ALL); 2906 err_reg_all: 2907 kfree(tbl); 2908 err_alloc_ctl: 2909 #endif 2910 kfree(dflt); 2911 err_alloc_dflt: 2912 kfree(all); 2913 err_alloc_all: 2914 kfree(net->ipv4.inet_addr_lst); 2915 err_alloc_hash: 2916 return err; 2917 } 2918 2919 static __net_exit void devinet_exit_net(struct net *net) 2920 { 2921 #ifdef CONFIG_SYSCTL 2922 const struct ctl_table *tbl; 2923 #endif 2924 2925 cancel_delayed_work_sync(&net->ipv4.addr_chk_work); 2926 2927 #ifdef CONFIG_SYSCTL 2928 tbl = net->ipv4.forw_hdr->ctl_table_arg; 2929 unregister_net_sysctl_table(net->ipv4.forw_hdr); 2930 __devinet_sysctl_unregister(net, net->ipv4.devconf_dflt, 2931 NETCONFA_IFINDEX_DEFAULT); 2932 __devinet_sysctl_unregister(net, net->ipv4.devconf_all, 2933 NETCONFA_IFINDEX_ALL); 2934 kfree(tbl); 2935 #endif 2936 kfree(net->ipv4.devconf_dflt); 2937 kfree(net->ipv4.devconf_all); 2938 kfree(net->ipv4.inet_addr_lst); 2939 } 2940 2941 static __net_initdata struct pernet_operations devinet_ops = { 2942 .init = devinet_init_net, 2943 .exit = devinet_exit_net, 2944 }; 2945 2946 static struct rtnl_af_ops inet_af_ops __read_mostly = { 2947 .family = AF_INET, 2948 .fill_link_af = inet_fill_link_af, 2949 .get_link_af_size = inet_get_link_af_size, 2950 .validate_link_af = inet_validate_link_af, 2951 .set_link_af = inet_set_link_af, 2952 }; 2953 2954 static const struct rtnl_msg_handler devinet_rtnl_msg_handlers[] __initconst = { 2955 {.protocol = PF_INET, .msgtype = RTM_NEWADDR, .doit = inet_rtm_newaddr, 2956 .flags = RTNL_FLAG_DOIT_PERNET}, 2957 {.protocol = PF_INET, .msgtype = RTM_DELADDR, .doit = inet_rtm_deladdr, 2958 .flags = RTNL_FLAG_DOIT_PERNET}, 2959 {.protocol = PF_INET, .msgtype = RTM_GETADDR, .dumpit = inet_dump_ifaddr, 2960 .flags = RTNL_FLAG_DUMP_UNLOCKED | RTNL_FLAG_DUMP_SPLIT_NLM_DONE}, 2961 {.protocol = PF_INET, .msgtype = RTM_GETNETCONF, 2962 .doit = inet_netconf_get_devconf, .dumpit = inet_netconf_dump_devconf, 2963 .flags = RTNL_FLAG_DOIT_UNLOCKED | RTNL_FLAG_DUMP_UNLOCKED}, 2964 {.owner = THIS_MODULE, .protocol = PF_INET, .msgtype = RTM_GETMULTICAST, 2965 .dumpit = inet_dump_ifmcaddr, .flags = RTNL_FLAG_DUMP_UNLOCKED}, 2966 }; 2967 2968 void __init devinet_init(void) 2969 { 2970 register_pernet_subsys(&devinet_ops); 2971 register_netdevice_notifier(&ip_netdev_notifier); 2972 2973 if (rtnl_af_register(&inet_af_ops)) 2974 panic("Unable to register inet_af_ops\n"); 2975 2976 rtnl_register_many(devinet_rtnl_msg_handlers); 2977 } 2978