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