1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * INET An implementation of the TCP/IP protocol suite for the LINUX 4 * operating system. INET is implemented using the BSD Socket 5 * interface as the means of communication with the user level. 6 * 7 * The IP to API glue. 8 * 9 * Authors: see ip.c 10 * 11 * Fixes: 12 * Many : Split from ip.c , see ip.c for history. 13 * Martin Mares : TOS setting fixed. 14 * Alan Cox : Fixed a couple of oopses in Martin's 15 * TOS tweaks. 16 * Mike McLagan : Routing by source 17 */ 18 19 #include <linux/module.h> 20 #include <linux/types.h> 21 #include <linux/mm.h> 22 #include <linux/skbuff.h> 23 #include <linux/ip.h> 24 #include <linux/icmp.h> 25 #include <linux/inetdevice.h> 26 #include <linux/netdevice.h> 27 #include <linux/slab.h> 28 #include <net/sock.h> 29 #include <net/ip.h> 30 #include <net/icmp.h> 31 #include <net/tcp_states.h> 32 #include <linux/udp.h> 33 #include <linux/igmp.h> 34 #include <linux/netfilter.h> 35 #include <linux/route.h> 36 #include <linux/mroute.h> 37 #include <net/inet_ecn.h> 38 #include <net/route.h> 39 #include <net/xfrm.h> 40 #include <net/compat.h> 41 #include <net/checksum.h> 42 #if IS_ENABLED(CONFIG_IPV6) 43 #include <net/transp_v6.h> 44 #endif 45 #include <net/ip_fib.h> 46 47 #include <linux/errqueue.h> 48 #include <linux/uaccess.h> 49 50 /* 51 * SOL_IP control messages. 52 */ 53 54 static void ip_cmsg_recv_pktinfo(struct msghdr *msg, struct sk_buff *skb) 55 { 56 struct in_pktinfo info = *PKTINFO_SKB_CB(skb); 57 58 info.ipi_addr.s_addr = ip_hdr(skb)->daddr; 59 60 put_cmsg(msg, SOL_IP, IP_PKTINFO, sizeof(info), &info); 61 } 62 63 static void ip_cmsg_recv_ttl(struct msghdr *msg, struct sk_buff *skb) 64 { 65 int ttl = ip_hdr(skb)->ttl; 66 put_cmsg(msg, SOL_IP, IP_TTL, sizeof(int), &ttl); 67 } 68 69 static void ip_cmsg_recv_tos(struct msghdr *msg, struct sk_buff *skb) 70 { 71 put_cmsg(msg, SOL_IP, IP_TOS, 1, &ip_hdr(skb)->tos); 72 } 73 74 static void ip_cmsg_recv_opts(struct msghdr *msg, struct sk_buff *skb) 75 { 76 if (IPCB(skb)->opt.optlen == 0) 77 return; 78 79 put_cmsg(msg, SOL_IP, IP_RECVOPTS, IPCB(skb)->opt.optlen, 80 ip_hdr(skb) + 1); 81 } 82 83 84 static void ip_cmsg_recv_retopts(struct net *net, struct msghdr *msg, 85 struct sk_buff *skb) 86 { 87 unsigned char optbuf[sizeof(struct ip_options) + 40]; 88 struct ip_options *opt = (struct ip_options *)optbuf; 89 90 if (IPCB(skb)->opt.optlen == 0) 91 return; 92 93 if (ip_options_echo(net, opt, skb)) { 94 msg->msg_flags |= MSG_CTRUNC; 95 return; 96 } 97 ip_options_undo(opt); 98 99 put_cmsg(msg, SOL_IP, IP_RETOPTS, opt->optlen, opt->__data); 100 } 101 102 static void ip_cmsg_recv_fragsize(struct msghdr *msg, struct sk_buff *skb) 103 { 104 int val; 105 106 if (IPCB(skb)->frag_max_size == 0) 107 return; 108 109 val = IPCB(skb)->frag_max_size; 110 put_cmsg(msg, SOL_IP, IP_RECVFRAGSIZE, sizeof(val), &val); 111 } 112 113 static void ip_cmsg_recv_checksum(struct msghdr *msg, struct sk_buff *skb, 114 int tlen, int offset) 115 { 116 __wsum csum = skb->csum; 117 118 if (skb->ip_summed != CHECKSUM_COMPLETE) 119 return; 120 121 if (offset != 0) { 122 int tend_off = skb_transport_offset(skb) + tlen; 123 csum = csum_sub(csum, skb_checksum(skb, tend_off, offset, 0)); 124 } 125 126 put_cmsg(msg, SOL_IP, IP_CHECKSUM, sizeof(__wsum), &csum); 127 } 128 129 static void ip_cmsg_recv_security(struct msghdr *msg, struct sk_buff *skb) 130 { 131 char *secdata; 132 u32 seclen, secid; 133 int err; 134 135 err = security_socket_getpeersec_dgram(NULL, skb, &secid); 136 if (err) 137 return; 138 139 err = security_secid_to_secctx(secid, &secdata, &seclen); 140 if (err) 141 return; 142 143 put_cmsg(msg, SOL_IP, SCM_SECURITY, seclen, secdata); 144 security_release_secctx(secdata, seclen); 145 } 146 147 static void ip_cmsg_recv_dstaddr(struct msghdr *msg, struct sk_buff *skb) 148 { 149 __be16 _ports[2], *ports; 150 struct sockaddr_in sin; 151 152 /* All current transport protocols have the port numbers in the 153 * first four bytes of the transport header and this function is 154 * written with this assumption in mind. 155 */ 156 ports = skb_header_pointer(skb, skb_transport_offset(skb), 157 sizeof(_ports), &_ports); 158 if (!ports) 159 return; 160 161 sin.sin_family = AF_INET; 162 sin.sin_addr.s_addr = ip_hdr(skb)->daddr; 163 sin.sin_port = ports[1]; 164 memset(sin.sin_zero, 0, sizeof(sin.sin_zero)); 165 166 put_cmsg(msg, SOL_IP, IP_ORIGDSTADDR, sizeof(sin), &sin); 167 } 168 169 void ip_cmsg_recv_offset(struct msghdr *msg, struct sock *sk, 170 struct sk_buff *skb, int tlen, int offset) 171 { 172 unsigned long flags = inet_cmsg_flags(inet_sk(sk)); 173 174 if (!flags) 175 return; 176 177 /* Ordered by supposed usage frequency */ 178 if (flags & IP_CMSG_PKTINFO) { 179 ip_cmsg_recv_pktinfo(msg, skb); 180 181 flags &= ~IP_CMSG_PKTINFO; 182 if (!flags) 183 return; 184 } 185 186 if (flags & IP_CMSG_TTL) { 187 ip_cmsg_recv_ttl(msg, skb); 188 189 flags &= ~IP_CMSG_TTL; 190 if (!flags) 191 return; 192 } 193 194 if (flags & IP_CMSG_TOS) { 195 ip_cmsg_recv_tos(msg, skb); 196 197 flags &= ~IP_CMSG_TOS; 198 if (!flags) 199 return; 200 } 201 202 if (flags & IP_CMSG_RECVOPTS) { 203 ip_cmsg_recv_opts(msg, skb); 204 205 flags &= ~IP_CMSG_RECVOPTS; 206 if (!flags) 207 return; 208 } 209 210 if (flags & IP_CMSG_RETOPTS) { 211 ip_cmsg_recv_retopts(sock_net(sk), msg, skb); 212 213 flags &= ~IP_CMSG_RETOPTS; 214 if (!flags) 215 return; 216 } 217 218 if (flags & IP_CMSG_PASSSEC) { 219 ip_cmsg_recv_security(msg, skb); 220 221 flags &= ~IP_CMSG_PASSSEC; 222 if (!flags) 223 return; 224 } 225 226 if (flags & IP_CMSG_ORIGDSTADDR) { 227 ip_cmsg_recv_dstaddr(msg, skb); 228 229 flags &= ~IP_CMSG_ORIGDSTADDR; 230 if (!flags) 231 return; 232 } 233 234 if (flags & IP_CMSG_CHECKSUM) 235 ip_cmsg_recv_checksum(msg, skb, tlen, offset); 236 237 if (flags & IP_CMSG_RECVFRAGSIZE) 238 ip_cmsg_recv_fragsize(msg, skb); 239 } 240 EXPORT_SYMBOL(ip_cmsg_recv_offset); 241 242 int ip_cmsg_send(struct sock *sk, struct msghdr *msg, struct ipcm_cookie *ipc, 243 bool allow_ipv6) 244 { 245 int err, val; 246 struct cmsghdr *cmsg; 247 struct net *net = sock_net(sk); 248 249 for_each_cmsghdr(cmsg, msg) { 250 if (!CMSG_OK(msg, cmsg)) 251 return -EINVAL; 252 #if IS_ENABLED(CONFIG_IPV6) 253 if (allow_ipv6 && 254 cmsg->cmsg_level == SOL_IPV6 && 255 cmsg->cmsg_type == IPV6_PKTINFO) { 256 struct in6_pktinfo *src_info; 257 258 if (cmsg->cmsg_len < CMSG_LEN(sizeof(*src_info))) 259 return -EINVAL; 260 src_info = (struct in6_pktinfo *)CMSG_DATA(cmsg); 261 if (!ipv6_addr_v4mapped(&src_info->ipi6_addr)) 262 return -EINVAL; 263 if (src_info->ipi6_ifindex) 264 ipc->oif = src_info->ipi6_ifindex; 265 ipc->addr = src_info->ipi6_addr.s6_addr32[3]; 266 continue; 267 } 268 #endif 269 if (cmsg->cmsg_level == SOL_SOCKET) { 270 err = __sock_cmsg_send(sk, cmsg, &ipc->sockc); 271 if (err) 272 return err; 273 continue; 274 } 275 276 if (cmsg->cmsg_level != SOL_IP) 277 continue; 278 switch (cmsg->cmsg_type) { 279 case IP_RETOPTS: 280 err = cmsg->cmsg_len - sizeof(struct cmsghdr); 281 282 /* Our caller is responsible for freeing ipc->opt */ 283 err = ip_options_get(net, &ipc->opt, 284 KERNEL_SOCKPTR(CMSG_DATA(cmsg)), 285 err < 40 ? err : 40); 286 if (err) 287 return err; 288 break; 289 case IP_PKTINFO: 290 { 291 struct in_pktinfo *info; 292 if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct in_pktinfo))) 293 return -EINVAL; 294 info = (struct in_pktinfo *)CMSG_DATA(cmsg); 295 if (info->ipi_ifindex) 296 ipc->oif = info->ipi_ifindex; 297 ipc->addr = info->ipi_spec_dst.s_addr; 298 break; 299 } 300 case IP_TTL: 301 if (cmsg->cmsg_len != CMSG_LEN(sizeof(int))) 302 return -EINVAL; 303 val = *(int *)CMSG_DATA(cmsg); 304 if (val < 1 || val > 255) 305 return -EINVAL; 306 ipc->ttl = val; 307 break; 308 case IP_TOS: 309 if (cmsg->cmsg_len == CMSG_LEN(sizeof(int))) 310 val = *(int *)CMSG_DATA(cmsg); 311 else if (cmsg->cmsg_len == CMSG_LEN(sizeof(u8))) 312 val = *(u8 *)CMSG_DATA(cmsg); 313 else 314 return -EINVAL; 315 if (val < 0 || val > 255) 316 return -EINVAL; 317 ipc->tos = val; 318 ipc->priority = rt_tos2priority(ipc->tos); 319 break; 320 case IP_PROTOCOL: 321 if (cmsg->cmsg_len != CMSG_LEN(sizeof(int))) 322 return -EINVAL; 323 val = *(int *)CMSG_DATA(cmsg); 324 if (val < 1 || val > 255) 325 return -EINVAL; 326 ipc->protocol = val; 327 break; 328 default: 329 return -EINVAL; 330 } 331 } 332 return 0; 333 } 334 335 static void ip_ra_destroy_rcu(struct rcu_head *head) 336 { 337 struct ip_ra_chain *ra = container_of(head, struct ip_ra_chain, rcu); 338 339 sock_put(ra->saved_sk); 340 kfree(ra); 341 } 342 343 int ip_ra_control(struct sock *sk, unsigned char on, 344 void (*destructor)(struct sock *)) 345 { 346 struct ip_ra_chain *ra, *new_ra; 347 struct ip_ra_chain __rcu **rap; 348 struct net *net = sock_net(sk); 349 350 if (sk->sk_type != SOCK_RAW || inet_sk(sk)->inet_num == IPPROTO_RAW) 351 return -EINVAL; 352 353 new_ra = on ? kmalloc(sizeof(*new_ra), GFP_KERNEL) : NULL; 354 if (on && !new_ra) 355 return -ENOMEM; 356 357 mutex_lock(&net->ipv4.ra_mutex); 358 for (rap = &net->ipv4.ra_chain; 359 (ra = rcu_dereference_protected(*rap, 360 lockdep_is_held(&net->ipv4.ra_mutex))) != NULL; 361 rap = &ra->next) { 362 if (ra->sk == sk) { 363 if (on) { 364 mutex_unlock(&net->ipv4.ra_mutex); 365 kfree(new_ra); 366 return -EADDRINUSE; 367 } 368 /* dont let ip_call_ra_chain() use sk again */ 369 ra->sk = NULL; 370 RCU_INIT_POINTER(*rap, ra->next); 371 mutex_unlock(&net->ipv4.ra_mutex); 372 373 if (ra->destructor) 374 ra->destructor(sk); 375 /* 376 * Delay sock_put(sk) and kfree(ra) after one rcu grace 377 * period. This guarantee ip_call_ra_chain() dont need 378 * to mess with socket refcounts. 379 */ 380 ra->saved_sk = sk; 381 call_rcu(&ra->rcu, ip_ra_destroy_rcu); 382 return 0; 383 } 384 } 385 if (!new_ra) { 386 mutex_unlock(&net->ipv4.ra_mutex); 387 return -ENOBUFS; 388 } 389 new_ra->sk = sk; 390 new_ra->destructor = destructor; 391 392 RCU_INIT_POINTER(new_ra->next, ra); 393 rcu_assign_pointer(*rap, new_ra); 394 sock_hold(sk); 395 mutex_unlock(&net->ipv4.ra_mutex); 396 397 return 0; 398 } 399 400 static void ipv4_icmp_error_rfc4884(const struct sk_buff *skb, 401 struct sock_ee_data_rfc4884 *out) 402 { 403 switch (icmp_hdr(skb)->type) { 404 case ICMP_DEST_UNREACH: 405 case ICMP_TIME_EXCEEDED: 406 case ICMP_PARAMETERPROB: 407 ip_icmp_error_rfc4884(skb, out, sizeof(struct icmphdr), 408 icmp_hdr(skb)->un.reserved[1] * 4); 409 } 410 } 411 412 void ip_icmp_error(struct sock *sk, struct sk_buff *skb, int err, 413 __be16 port, u32 info, u8 *payload) 414 { 415 struct sock_exterr_skb *serr; 416 417 skb = skb_clone(skb, GFP_ATOMIC); 418 if (!skb) 419 return; 420 421 serr = SKB_EXT_ERR(skb); 422 serr->ee.ee_errno = err; 423 serr->ee.ee_origin = SO_EE_ORIGIN_ICMP; 424 serr->ee.ee_type = icmp_hdr(skb)->type; 425 serr->ee.ee_code = icmp_hdr(skb)->code; 426 serr->ee.ee_pad = 0; 427 serr->ee.ee_info = info; 428 serr->ee.ee_data = 0; 429 serr->addr_offset = (u8 *)&(((struct iphdr *)(icmp_hdr(skb) + 1))->daddr) - 430 skb_network_header(skb); 431 serr->port = port; 432 433 if (skb_pull(skb, payload - skb->data)) { 434 if (inet_test_bit(RECVERR_RFC4884, sk)) 435 ipv4_icmp_error_rfc4884(skb, &serr->ee.ee_rfc4884); 436 437 skb_reset_transport_header(skb); 438 if (sock_queue_err_skb(sk, skb) == 0) 439 return; 440 } 441 kfree_skb(skb); 442 } 443 EXPORT_SYMBOL_GPL(ip_icmp_error); 444 445 void ip_local_error(struct sock *sk, int err, __be32 daddr, __be16 port, u32 info) 446 { 447 struct sock_exterr_skb *serr; 448 struct iphdr *iph; 449 struct sk_buff *skb; 450 451 if (!inet_test_bit(RECVERR, sk)) 452 return; 453 454 skb = alloc_skb(sizeof(struct iphdr), GFP_ATOMIC); 455 if (!skb) 456 return; 457 458 skb_put(skb, sizeof(struct iphdr)); 459 skb_reset_network_header(skb); 460 iph = ip_hdr(skb); 461 iph->daddr = daddr; 462 463 serr = SKB_EXT_ERR(skb); 464 serr->ee.ee_errno = err; 465 serr->ee.ee_origin = SO_EE_ORIGIN_LOCAL; 466 serr->ee.ee_type = 0; 467 serr->ee.ee_code = 0; 468 serr->ee.ee_pad = 0; 469 serr->ee.ee_info = info; 470 serr->ee.ee_data = 0; 471 serr->addr_offset = (u8 *)&iph->daddr - skb_network_header(skb); 472 serr->port = port; 473 474 __skb_pull(skb, skb_tail_pointer(skb) - skb->data); 475 skb_reset_transport_header(skb); 476 477 if (sock_queue_err_skb(sk, skb)) 478 kfree_skb(skb); 479 } 480 481 /* For some errors we have valid addr_offset even with zero payload and 482 * zero port. Also, addr_offset should be supported if port is set. 483 */ 484 static inline bool ipv4_datagram_support_addr(struct sock_exterr_skb *serr) 485 { 486 return serr->ee.ee_origin == SO_EE_ORIGIN_ICMP || 487 serr->ee.ee_origin == SO_EE_ORIGIN_LOCAL || serr->port; 488 } 489 490 /* IPv4 supports cmsg on all imcp errors and some timestamps 491 * 492 * Timestamp code paths do not initialize the fields expected by cmsg: 493 * the PKTINFO fields in skb->cb[]. Fill those in here. 494 */ 495 static bool ipv4_datagram_support_cmsg(const struct sock *sk, 496 struct sk_buff *skb, 497 int ee_origin) 498 { 499 struct in_pktinfo *info; 500 501 if (ee_origin == SO_EE_ORIGIN_ICMP) 502 return true; 503 504 if (ee_origin == SO_EE_ORIGIN_LOCAL) 505 return false; 506 507 /* Support IP_PKTINFO on tstamp packets if requested, to correlate 508 * timestamp with egress dev. Not possible for packets without iif 509 * or without payload (SOF_TIMESTAMPING_OPT_TSONLY). 510 */ 511 info = PKTINFO_SKB_CB(skb); 512 if (!(READ_ONCE(sk->sk_tsflags) & SOF_TIMESTAMPING_OPT_CMSG) || 513 !info->ipi_ifindex) 514 return false; 515 516 info->ipi_spec_dst.s_addr = ip_hdr(skb)->saddr; 517 return true; 518 } 519 520 /* 521 * Handle MSG_ERRQUEUE 522 */ 523 int ip_recv_error(struct sock *sk, struct msghdr *msg, int len, int *addr_len) 524 { 525 struct sock_exterr_skb *serr; 526 struct sk_buff *skb; 527 DECLARE_SOCKADDR(struct sockaddr_in *, sin, msg->msg_name); 528 struct { 529 struct sock_extended_err ee; 530 struct sockaddr_in offender; 531 } errhdr; 532 int err; 533 int copied; 534 535 err = -EAGAIN; 536 skb = sock_dequeue_err_skb(sk); 537 if (!skb) 538 goto out; 539 540 copied = skb->len; 541 if (copied > len) { 542 msg->msg_flags |= MSG_TRUNC; 543 copied = len; 544 } 545 err = skb_copy_datagram_msg(skb, 0, msg, copied); 546 if (unlikely(err)) { 547 kfree_skb(skb); 548 return err; 549 } 550 sock_recv_timestamp(msg, sk, skb); 551 552 serr = SKB_EXT_ERR(skb); 553 554 if (sin && ipv4_datagram_support_addr(serr)) { 555 sin->sin_family = AF_INET; 556 sin->sin_addr.s_addr = *(__be32 *)(skb_network_header(skb) + 557 serr->addr_offset); 558 sin->sin_port = serr->port; 559 memset(&sin->sin_zero, 0, sizeof(sin->sin_zero)); 560 *addr_len = sizeof(*sin); 561 } 562 563 memcpy(&errhdr.ee, &serr->ee, sizeof(struct sock_extended_err)); 564 sin = &errhdr.offender; 565 memset(sin, 0, sizeof(*sin)); 566 567 if (ipv4_datagram_support_cmsg(sk, skb, serr->ee.ee_origin)) { 568 sin->sin_family = AF_INET; 569 sin->sin_addr.s_addr = ip_hdr(skb)->saddr; 570 if (inet_cmsg_flags(inet_sk(sk))) 571 ip_cmsg_recv(msg, skb); 572 } 573 574 put_cmsg(msg, SOL_IP, IP_RECVERR, sizeof(errhdr), &errhdr); 575 576 /* Now we could try to dump offended packet options */ 577 578 msg->msg_flags |= MSG_ERRQUEUE; 579 err = copied; 580 581 consume_skb(skb); 582 out: 583 return err; 584 } 585 586 void __ip_sock_set_tos(struct sock *sk, int val) 587 { 588 u8 old_tos = inet_sk(sk)->tos; 589 590 if (sk->sk_type == SOCK_STREAM) { 591 val &= ~INET_ECN_MASK; 592 val |= old_tos & INET_ECN_MASK; 593 } 594 if (old_tos != val) { 595 WRITE_ONCE(inet_sk(sk)->tos, val); 596 WRITE_ONCE(sk->sk_priority, rt_tos2priority(val)); 597 sk_dst_reset(sk); 598 } 599 } 600 601 void ip_sock_set_tos(struct sock *sk, int val) 602 { 603 sockopt_lock_sock(sk); 604 __ip_sock_set_tos(sk, val); 605 sockopt_release_sock(sk); 606 } 607 EXPORT_SYMBOL(ip_sock_set_tos); 608 609 void ip_sock_set_freebind(struct sock *sk) 610 { 611 inet_set_bit(FREEBIND, sk); 612 } 613 EXPORT_SYMBOL(ip_sock_set_freebind); 614 615 void ip_sock_set_recverr(struct sock *sk) 616 { 617 inet_set_bit(RECVERR, sk); 618 } 619 EXPORT_SYMBOL(ip_sock_set_recverr); 620 621 int ip_sock_set_mtu_discover(struct sock *sk, int val) 622 { 623 if (val < IP_PMTUDISC_DONT || val > IP_PMTUDISC_OMIT) 624 return -EINVAL; 625 WRITE_ONCE(inet_sk(sk)->pmtudisc, val); 626 return 0; 627 } 628 EXPORT_SYMBOL(ip_sock_set_mtu_discover); 629 630 void ip_sock_set_pktinfo(struct sock *sk) 631 { 632 inet_set_bit(PKTINFO, sk); 633 } 634 EXPORT_SYMBOL(ip_sock_set_pktinfo); 635 636 /* 637 * Socket option code for IP. This is the end of the line after any 638 * TCP,UDP etc options on an IP socket. 639 */ 640 static bool setsockopt_needs_rtnl(int optname) 641 { 642 switch (optname) { 643 case IP_ADD_MEMBERSHIP: 644 case IP_ADD_SOURCE_MEMBERSHIP: 645 case IP_BLOCK_SOURCE: 646 case IP_DROP_MEMBERSHIP: 647 case IP_DROP_SOURCE_MEMBERSHIP: 648 case IP_MSFILTER: 649 case IP_UNBLOCK_SOURCE: 650 case MCAST_BLOCK_SOURCE: 651 case MCAST_MSFILTER: 652 case MCAST_JOIN_GROUP: 653 case MCAST_JOIN_SOURCE_GROUP: 654 case MCAST_LEAVE_GROUP: 655 case MCAST_LEAVE_SOURCE_GROUP: 656 case MCAST_UNBLOCK_SOURCE: 657 return true; 658 } 659 return false; 660 } 661 662 static int set_mcast_msfilter(struct sock *sk, int ifindex, 663 int numsrc, int fmode, 664 struct sockaddr_storage *group, 665 struct sockaddr_storage *list) 666 { 667 struct ip_msfilter *msf; 668 struct sockaddr_in *psin; 669 int err, i; 670 671 msf = kmalloc(IP_MSFILTER_SIZE(numsrc), GFP_KERNEL); 672 if (!msf) 673 return -ENOBUFS; 674 675 psin = (struct sockaddr_in *)group; 676 if (psin->sin_family != AF_INET) 677 goto Eaddrnotavail; 678 msf->imsf_multiaddr = psin->sin_addr.s_addr; 679 msf->imsf_interface = 0; 680 msf->imsf_fmode = fmode; 681 msf->imsf_numsrc = numsrc; 682 for (i = 0; i < numsrc; ++i) { 683 psin = (struct sockaddr_in *)&list[i]; 684 685 if (psin->sin_family != AF_INET) 686 goto Eaddrnotavail; 687 msf->imsf_slist_flex[i] = psin->sin_addr.s_addr; 688 } 689 err = ip_mc_msfilter(sk, msf, ifindex); 690 kfree(msf); 691 return err; 692 693 Eaddrnotavail: 694 kfree(msf); 695 return -EADDRNOTAVAIL; 696 } 697 698 static int copy_group_source_from_sockptr(struct group_source_req *greqs, 699 sockptr_t optval, int optlen) 700 { 701 if (in_compat_syscall()) { 702 struct compat_group_source_req gr32; 703 704 if (optlen != sizeof(gr32)) 705 return -EINVAL; 706 if (copy_from_sockptr(&gr32, optval, sizeof(gr32))) 707 return -EFAULT; 708 greqs->gsr_interface = gr32.gsr_interface; 709 greqs->gsr_group = gr32.gsr_group; 710 greqs->gsr_source = gr32.gsr_source; 711 } else { 712 if (optlen != sizeof(*greqs)) 713 return -EINVAL; 714 if (copy_from_sockptr(greqs, optval, sizeof(*greqs))) 715 return -EFAULT; 716 } 717 718 return 0; 719 } 720 721 static int do_mcast_group_source(struct sock *sk, int optname, 722 sockptr_t optval, int optlen) 723 { 724 struct group_source_req greqs; 725 struct ip_mreq_source mreqs; 726 struct sockaddr_in *psin; 727 int omode, add, err; 728 729 err = copy_group_source_from_sockptr(&greqs, optval, optlen); 730 if (err) 731 return err; 732 733 if (greqs.gsr_group.ss_family != AF_INET || 734 greqs.gsr_source.ss_family != AF_INET) 735 return -EADDRNOTAVAIL; 736 737 psin = (struct sockaddr_in *)&greqs.gsr_group; 738 mreqs.imr_multiaddr = psin->sin_addr.s_addr; 739 psin = (struct sockaddr_in *)&greqs.gsr_source; 740 mreqs.imr_sourceaddr = psin->sin_addr.s_addr; 741 mreqs.imr_interface = 0; /* use index for mc_source */ 742 743 if (optname == MCAST_BLOCK_SOURCE) { 744 omode = MCAST_EXCLUDE; 745 add = 1; 746 } else if (optname == MCAST_UNBLOCK_SOURCE) { 747 omode = MCAST_EXCLUDE; 748 add = 0; 749 } else if (optname == MCAST_JOIN_SOURCE_GROUP) { 750 struct ip_mreqn mreq; 751 752 psin = (struct sockaddr_in *)&greqs.gsr_group; 753 mreq.imr_multiaddr = psin->sin_addr; 754 mreq.imr_address.s_addr = 0; 755 mreq.imr_ifindex = greqs.gsr_interface; 756 err = ip_mc_join_group_ssm(sk, &mreq, MCAST_INCLUDE); 757 if (err && err != -EADDRINUSE) 758 return err; 759 greqs.gsr_interface = mreq.imr_ifindex; 760 omode = MCAST_INCLUDE; 761 add = 1; 762 } else /* MCAST_LEAVE_SOURCE_GROUP */ { 763 omode = MCAST_INCLUDE; 764 add = 0; 765 } 766 return ip_mc_source(add, omode, sk, &mreqs, greqs.gsr_interface); 767 } 768 769 static int ip_set_mcast_msfilter(struct sock *sk, sockptr_t optval, int optlen) 770 { 771 struct group_filter *gsf = NULL; 772 int err; 773 774 if (optlen < GROUP_FILTER_SIZE(0)) 775 return -EINVAL; 776 if (optlen > READ_ONCE(sock_net(sk)->core.sysctl_optmem_max)) 777 return -ENOBUFS; 778 779 gsf = memdup_sockptr(optval, optlen); 780 if (IS_ERR(gsf)) 781 return PTR_ERR(gsf); 782 783 /* numsrc >= (4G-140)/128 overflow in 32 bits */ 784 err = -ENOBUFS; 785 if (gsf->gf_numsrc >= 0x1ffffff || 786 gsf->gf_numsrc > READ_ONCE(sock_net(sk)->ipv4.sysctl_igmp_max_msf)) 787 goto out_free_gsf; 788 789 err = -EINVAL; 790 if (GROUP_FILTER_SIZE(gsf->gf_numsrc) > optlen) 791 goto out_free_gsf; 792 793 err = set_mcast_msfilter(sk, gsf->gf_interface, gsf->gf_numsrc, 794 gsf->gf_fmode, &gsf->gf_group, 795 gsf->gf_slist_flex); 796 out_free_gsf: 797 kfree(gsf); 798 return err; 799 } 800 801 static int compat_ip_set_mcast_msfilter(struct sock *sk, sockptr_t optval, 802 int optlen) 803 { 804 const int size0 = offsetof(struct compat_group_filter, gf_slist_flex); 805 struct compat_group_filter *gf32; 806 unsigned int n; 807 void *p; 808 int err; 809 810 if (optlen < size0) 811 return -EINVAL; 812 if (optlen > READ_ONCE(sock_net(sk)->core.sysctl_optmem_max) - 4) 813 return -ENOBUFS; 814 815 p = kmalloc(optlen + 4, GFP_KERNEL); 816 if (!p) 817 return -ENOMEM; 818 gf32 = p + 4; /* we want ->gf_group and ->gf_slist_flex aligned */ 819 820 err = -EFAULT; 821 if (copy_from_sockptr(gf32, optval, optlen)) 822 goto out_free_gsf; 823 824 /* numsrc >= (4G-140)/128 overflow in 32 bits */ 825 n = gf32->gf_numsrc; 826 err = -ENOBUFS; 827 if (n >= 0x1ffffff) 828 goto out_free_gsf; 829 830 err = -EINVAL; 831 if (offsetof(struct compat_group_filter, gf_slist_flex[n]) > optlen) 832 goto out_free_gsf; 833 834 /* numsrc >= (4G-140)/128 overflow in 32 bits */ 835 err = -ENOBUFS; 836 if (n > READ_ONCE(sock_net(sk)->ipv4.sysctl_igmp_max_msf)) 837 goto out_free_gsf; 838 err = set_mcast_msfilter(sk, gf32->gf_interface, n, gf32->gf_fmode, 839 &gf32->gf_group, gf32->gf_slist_flex); 840 out_free_gsf: 841 kfree(p); 842 return err; 843 } 844 845 static int ip_mcast_join_leave(struct sock *sk, int optname, 846 sockptr_t optval, int optlen) 847 { 848 struct ip_mreqn mreq = { }; 849 struct sockaddr_in *psin; 850 struct group_req greq; 851 852 if (optlen < sizeof(struct group_req)) 853 return -EINVAL; 854 if (copy_from_sockptr(&greq, optval, sizeof(greq))) 855 return -EFAULT; 856 857 psin = (struct sockaddr_in *)&greq.gr_group; 858 if (psin->sin_family != AF_INET) 859 return -EINVAL; 860 mreq.imr_multiaddr = psin->sin_addr; 861 mreq.imr_ifindex = greq.gr_interface; 862 if (optname == MCAST_JOIN_GROUP) 863 return ip_mc_join_group(sk, &mreq); 864 return ip_mc_leave_group(sk, &mreq); 865 } 866 867 static int compat_ip_mcast_join_leave(struct sock *sk, int optname, 868 sockptr_t optval, int optlen) 869 { 870 struct compat_group_req greq; 871 struct ip_mreqn mreq = { }; 872 struct sockaddr_in *psin; 873 874 if (optlen < sizeof(struct compat_group_req)) 875 return -EINVAL; 876 if (copy_from_sockptr(&greq, optval, sizeof(greq))) 877 return -EFAULT; 878 879 psin = (struct sockaddr_in *)&greq.gr_group; 880 if (psin->sin_family != AF_INET) 881 return -EINVAL; 882 mreq.imr_multiaddr = psin->sin_addr; 883 mreq.imr_ifindex = greq.gr_interface; 884 885 if (optname == MCAST_JOIN_GROUP) 886 return ip_mc_join_group(sk, &mreq); 887 return ip_mc_leave_group(sk, &mreq); 888 } 889 890 DEFINE_STATIC_KEY_FALSE(ip4_min_ttl); 891 892 int do_ip_setsockopt(struct sock *sk, int level, int optname, 893 sockptr_t optval, unsigned int optlen) 894 { 895 struct inet_sock *inet = inet_sk(sk); 896 struct net *net = sock_net(sk); 897 int val = 0, err; 898 bool needs_rtnl = setsockopt_needs_rtnl(optname); 899 900 switch (optname) { 901 case IP_PKTINFO: 902 case IP_RECVTTL: 903 case IP_RECVOPTS: 904 case IP_RECVTOS: 905 case IP_RETOPTS: 906 case IP_TOS: 907 case IP_TTL: 908 case IP_HDRINCL: 909 case IP_MTU_DISCOVER: 910 case IP_RECVERR: 911 case IP_ROUTER_ALERT: 912 case IP_FREEBIND: 913 case IP_PASSSEC: 914 case IP_TRANSPARENT: 915 case IP_MINTTL: 916 case IP_NODEFRAG: 917 case IP_BIND_ADDRESS_NO_PORT: 918 case IP_UNICAST_IF: 919 case IP_MULTICAST_TTL: 920 case IP_MULTICAST_ALL: 921 case IP_MULTICAST_LOOP: 922 case IP_RECVORIGDSTADDR: 923 case IP_CHECKSUM: 924 case IP_RECVFRAGSIZE: 925 case IP_RECVERR_RFC4884: 926 case IP_LOCAL_PORT_RANGE: 927 if (optlen >= sizeof(int)) { 928 if (copy_from_sockptr(&val, optval, sizeof(val))) 929 return -EFAULT; 930 } else if (optlen >= sizeof(char)) { 931 unsigned char ucval; 932 933 if (copy_from_sockptr(&ucval, optval, sizeof(ucval))) 934 return -EFAULT; 935 val = (int) ucval; 936 } 937 } 938 939 /* If optlen==0, it is equivalent to val == 0 */ 940 941 if (optname == IP_ROUTER_ALERT) 942 return ip_ra_control(sk, val ? 1 : 0, NULL); 943 if (ip_mroute_opt(optname)) 944 return ip_mroute_setsockopt(sk, optname, optval, optlen); 945 946 /* Handle options that can be set without locking the socket. */ 947 switch (optname) { 948 case IP_PKTINFO: 949 inet_assign_bit(PKTINFO, sk, val); 950 return 0; 951 case IP_RECVTTL: 952 inet_assign_bit(TTL, sk, val); 953 return 0; 954 case IP_RECVTOS: 955 inet_assign_bit(TOS, sk, val); 956 return 0; 957 case IP_RECVOPTS: 958 inet_assign_bit(RECVOPTS, sk, val); 959 return 0; 960 case IP_RETOPTS: 961 inet_assign_bit(RETOPTS, sk, val); 962 return 0; 963 case IP_PASSSEC: 964 inet_assign_bit(PASSSEC, sk, val); 965 return 0; 966 case IP_RECVORIGDSTADDR: 967 inet_assign_bit(ORIGDSTADDR, sk, val); 968 return 0; 969 case IP_RECVFRAGSIZE: 970 if (sk->sk_type != SOCK_RAW && sk->sk_type != SOCK_DGRAM) 971 return -EINVAL; 972 inet_assign_bit(RECVFRAGSIZE, sk, val); 973 return 0; 974 case IP_RECVERR: 975 inet_assign_bit(RECVERR, sk, val); 976 if (!val) 977 skb_errqueue_purge(&sk->sk_error_queue); 978 return 0; 979 case IP_RECVERR_RFC4884: 980 if (val < 0 || val > 1) 981 return -EINVAL; 982 inet_assign_bit(RECVERR_RFC4884, sk, val); 983 return 0; 984 case IP_FREEBIND: 985 if (optlen < 1) 986 return -EINVAL; 987 inet_assign_bit(FREEBIND, sk, val); 988 return 0; 989 case IP_HDRINCL: 990 if (sk->sk_type != SOCK_RAW) 991 return -ENOPROTOOPT; 992 inet_assign_bit(HDRINCL, sk, val); 993 return 0; 994 case IP_MULTICAST_LOOP: 995 if (optlen < 1) 996 return -EINVAL; 997 inet_assign_bit(MC_LOOP, sk, val); 998 return 0; 999 case IP_MULTICAST_ALL: 1000 if (optlen < 1) 1001 return -EINVAL; 1002 if (val != 0 && val != 1) 1003 return -EINVAL; 1004 inet_assign_bit(MC_ALL, sk, val); 1005 return 0; 1006 case IP_TRANSPARENT: 1007 if (!!val && !sockopt_ns_capable(sock_net(sk)->user_ns, CAP_NET_RAW) && 1008 !sockopt_ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN)) 1009 return -EPERM; 1010 if (optlen < 1) 1011 return -EINVAL; 1012 inet_assign_bit(TRANSPARENT, sk, val); 1013 return 0; 1014 case IP_NODEFRAG: 1015 if (sk->sk_type != SOCK_RAW) 1016 return -ENOPROTOOPT; 1017 inet_assign_bit(NODEFRAG, sk, val); 1018 return 0; 1019 case IP_BIND_ADDRESS_NO_PORT: 1020 inet_assign_bit(BIND_ADDRESS_NO_PORT, sk, val); 1021 return 0; 1022 case IP_TTL: 1023 if (optlen < 1) 1024 return -EINVAL; 1025 if (val != -1 && (val < 1 || val > 255)) 1026 return -EINVAL; 1027 WRITE_ONCE(inet->uc_ttl, val); 1028 return 0; 1029 case IP_MINTTL: 1030 if (optlen < 1) 1031 return -EINVAL; 1032 if (val < 0 || val > 255) 1033 return -EINVAL; 1034 1035 if (val) 1036 static_branch_enable(&ip4_min_ttl); 1037 1038 WRITE_ONCE(inet->min_ttl, val); 1039 return 0; 1040 case IP_MULTICAST_TTL: 1041 if (sk->sk_type == SOCK_STREAM) 1042 return -EINVAL; 1043 if (optlen < 1) 1044 return -EINVAL; 1045 if (val == -1) 1046 val = 1; 1047 if (val < 0 || val > 255) 1048 return -EINVAL; 1049 WRITE_ONCE(inet->mc_ttl, val); 1050 return 0; 1051 case IP_MTU_DISCOVER: 1052 return ip_sock_set_mtu_discover(sk, val); 1053 case IP_TOS: /* This sets both TOS and Precedence */ 1054 ip_sock_set_tos(sk, val); 1055 return 0; 1056 case IP_LOCAL_PORT_RANGE: 1057 { 1058 u16 lo = val; 1059 u16 hi = val >> 16; 1060 1061 if (optlen != sizeof(u32)) 1062 return -EINVAL; 1063 if (lo != 0 && hi != 0 && lo > hi) 1064 return -EINVAL; 1065 1066 WRITE_ONCE(inet->local_port_range, val); 1067 return 0; 1068 } 1069 } 1070 1071 err = 0; 1072 if (needs_rtnl) 1073 rtnl_lock(); 1074 sockopt_lock_sock(sk); 1075 1076 switch (optname) { 1077 case IP_OPTIONS: 1078 { 1079 struct ip_options_rcu *old, *opt = NULL; 1080 1081 if (optlen > 40) 1082 goto e_inval; 1083 err = ip_options_get(sock_net(sk), &opt, optval, optlen); 1084 if (err) 1085 break; 1086 old = rcu_dereference_protected(inet->inet_opt, 1087 lockdep_sock_is_held(sk)); 1088 if (inet_test_bit(IS_ICSK, sk)) { 1089 struct inet_connection_sock *icsk = inet_csk(sk); 1090 #if IS_ENABLED(CONFIG_IPV6) 1091 if (sk->sk_family == PF_INET || 1092 (!((1 << sk->sk_state) & 1093 (TCPF_LISTEN | TCPF_CLOSE)) && 1094 inet->inet_daddr != LOOPBACK4_IPV6)) { 1095 #endif 1096 if (old) 1097 icsk->icsk_ext_hdr_len -= old->opt.optlen; 1098 if (opt) 1099 icsk->icsk_ext_hdr_len += opt->opt.optlen; 1100 icsk->icsk_sync_mss(sk, icsk->icsk_pmtu_cookie); 1101 #if IS_ENABLED(CONFIG_IPV6) 1102 } 1103 #endif 1104 } 1105 rcu_assign_pointer(inet->inet_opt, opt); 1106 if (old) 1107 kfree_rcu(old, rcu); 1108 break; 1109 } 1110 case IP_CHECKSUM: 1111 if (val) { 1112 if (!(inet_test_bit(CHECKSUM, sk))) { 1113 inet_inc_convert_csum(sk); 1114 inet_set_bit(CHECKSUM, sk); 1115 } 1116 } else { 1117 if (inet_test_bit(CHECKSUM, sk)) { 1118 inet_dec_convert_csum(sk); 1119 inet_clear_bit(CHECKSUM, sk); 1120 } 1121 } 1122 break; 1123 case IP_UNICAST_IF: 1124 { 1125 struct net_device *dev = NULL; 1126 int ifindex; 1127 int midx; 1128 1129 if (optlen != sizeof(int)) 1130 goto e_inval; 1131 1132 ifindex = (__force int)ntohl((__force __be32)val); 1133 if (ifindex == 0) { 1134 WRITE_ONCE(inet->uc_index, 0); 1135 err = 0; 1136 break; 1137 } 1138 1139 dev = dev_get_by_index(sock_net(sk), ifindex); 1140 err = -EADDRNOTAVAIL; 1141 if (!dev) 1142 break; 1143 1144 midx = l3mdev_master_ifindex(dev); 1145 dev_put(dev); 1146 1147 err = -EINVAL; 1148 if (sk->sk_bound_dev_if && midx != sk->sk_bound_dev_if) 1149 break; 1150 1151 WRITE_ONCE(inet->uc_index, ifindex); 1152 err = 0; 1153 break; 1154 } 1155 case IP_MULTICAST_IF: 1156 { 1157 struct ip_mreqn mreq; 1158 struct net_device *dev = NULL; 1159 int midx; 1160 1161 if (sk->sk_type == SOCK_STREAM) 1162 goto e_inval; 1163 /* 1164 * Check the arguments are allowable 1165 */ 1166 1167 if (optlen < sizeof(struct in_addr)) 1168 goto e_inval; 1169 1170 err = -EFAULT; 1171 if (optlen >= sizeof(struct ip_mreqn)) { 1172 if (copy_from_sockptr(&mreq, optval, sizeof(mreq))) 1173 break; 1174 } else { 1175 memset(&mreq, 0, sizeof(mreq)); 1176 if (optlen >= sizeof(struct ip_mreq)) { 1177 if (copy_from_sockptr(&mreq, optval, 1178 sizeof(struct ip_mreq))) 1179 break; 1180 } else if (optlen >= sizeof(struct in_addr)) { 1181 if (copy_from_sockptr(&mreq.imr_address, optval, 1182 sizeof(struct in_addr))) 1183 break; 1184 } 1185 } 1186 1187 if (!mreq.imr_ifindex) { 1188 if (mreq.imr_address.s_addr == htonl(INADDR_ANY)) { 1189 WRITE_ONCE(inet->mc_index, 0); 1190 WRITE_ONCE(inet->mc_addr, 0); 1191 err = 0; 1192 break; 1193 } 1194 dev = ip_dev_find(sock_net(sk), mreq.imr_address.s_addr); 1195 if (dev) 1196 mreq.imr_ifindex = dev->ifindex; 1197 } else 1198 dev = dev_get_by_index(sock_net(sk), mreq.imr_ifindex); 1199 1200 1201 err = -EADDRNOTAVAIL; 1202 if (!dev) 1203 break; 1204 1205 midx = l3mdev_master_ifindex(dev); 1206 1207 dev_put(dev); 1208 1209 err = -EINVAL; 1210 if (sk->sk_bound_dev_if && 1211 mreq.imr_ifindex != sk->sk_bound_dev_if && 1212 midx != sk->sk_bound_dev_if) 1213 break; 1214 1215 WRITE_ONCE(inet->mc_index, mreq.imr_ifindex); 1216 WRITE_ONCE(inet->mc_addr, mreq.imr_address.s_addr); 1217 err = 0; 1218 break; 1219 } 1220 1221 case IP_ADD_MEMBERSHIP: 1222 case IP_DROP_MEMBERSHIP: 1223 { 1224 struct ip_mreqn mreq; 1225 1226 err = -EPROTO; 1227 if (inet_test_bit(IS_ICSK, sk)) 1228 break; 1229 1230 if (optlen < sizeof(struct ip_mreq)) 1231 goto e_inval; 1232 err = -EFAULT; 1233 if (optlen >= sizeof(struct ip_mreqn)) { 1234 if (copy_from_sockptr(&mreq, optval, sizeof(mreq))) 1235 break; 1236 } else { 1237 memset(&mreq, 0, sizeof(mreq)); 1238 if (copy_from_sockptr(&mreq, optval, 1239 sizeof(struct ip_mreq))) 1240 break; 1241 } 1242 1243 if (optname == IP_ADD_MEMBERSHIP) 1244 err = ip_mc_join_group(sk, &mreq); 1245 else 1246 err = ip_mc_leave_group(sk, &mreq); 1247 break; 1248 } 1249 case IP_MSFILTER: 1250 { 1251 struct ip_msfilter *msf; 1252 1253 if (optlen < IP_MSFILTER_SIZE(0)) 1254 goto e_inval; 1255 if (optlen > READ_ONCE(net->core.sysctl_optmem_max)) { 1256 err = -ENOBUFS; 1257 break; 1258 } 1259 msf = memdup_sockptr(optval, optlen); 1260 if (IS_ERR(msf)) { 1261 err = PTR_ERR(msf); 1262 break; 1263 } 1264 /* numsrc >= (1G-4) overflow in 32 bits */ 1265 if (msf->imsf_numsrc >= 0x3ffffffcU || 1266 msf->imsf_numsrc > READ_ONCE(net->ipv4.sysctl_igmp_max_msf)) { 1267 kfree(msf); 1268 err = -ENOBUFS; 1269 break; 1270 } 1271 if (IP_MSFILTER_SIZE(msf->imsf_numsrc) > optlen) { 1272 kfree(msf); 1273 err = -EINVAL; 1274 break; 1275 } 1276 err = ip_mc_msfilter(sk, msf, 0); 1277 kfree(msf); 1278 break; 1279 } 1280 case IP_BLOCK_SOURCE: 1281 case IP_UNBLOCK_SOURCE: 1282 case IP_ADD_SOURCE_MEMBERSHIP: 1283 case IP_DROP_SOURCE_MEMBERSHIP: 1284 { 1285 struct ip_mreq_source mreqs; 1286 int omode, add; 1287 1288 if (optlen != sizeof(struct ip_mreq_source)) 1289 goto e_inval; 1290 if (copy_from_sockptr(&mreqs, optval, sizeof(mreqs))) { 1291 err = -EFAULT; 1292 break; 1293 } 1294 if (optname == IP_BLOCK_SOURCE) { 1295 omode = MCAST_EXCLUDE; 1296 add = 1; 1297 } else if (optname == IP_UNBLOCK_SOURCE) { 1298 omode = MCAST_EXCLUDE; 1299 add = 0; 1300 } else if (optname == IP_ADD_SOURCE_MEMBERSHIP) { 1301 struct ip_mreqn mreq; 1302 1303 mreq.imr_multiaddr.s_addr = mreqs.imr_multiaddr; 1304 mreq.imr_address.s_addr = mreqs.imr_interface; 1305 mreq.imr_ifindex = 0; 1306 err = ip_mc_join_group_ssm(sk, &mreq, MCAST_INCLUDE); 1307 if (err && err != -EADDRINUSE) 1308 break; 1309 omode = MCAST_INCLUDE; 1310 add = 1; 1311 } else /* IP_DROP_SOURCE_MEMBERSHIP */ { 1312 omode = MCAST_INCLUDE; 1313 add = 0; 1314 } 1315 err = ip_mc_source(add, omode, sk, &mreqs, 0); 1316 break; 1317 } 1318 case MCAST_JOIN_GROUP: 1319 case MCAST_LEAVE_GROUP: 1320 if (in_compat_syscall()) 1321 err = compat_ip_mcast_join_leave(sk, optname, optval, 1322 optlen); 1323 else 1324 err = ip_mcast_join_leave(sk, optname, optval, optlen); 1325 break; 1326 case MCAST_JOIN_SOURCE_GROUP: 1327 case MCAST_LEAVE_SOURCE_GROUP: 1328 case MCAST_BLOCK_SOURCE: 1329 case MCAST_UNBLOCK_SOURCE: 1330 err = do_mcast_group_source(sk, optname, optval, optlen); 1331 break; 1332 case MCAST_MSFILTER: 1333 if (in_compat_syscall()) 1334 err = compat_ip_set_mcast_msfilter(sk, optval, optlen); 1335 else 1336 err = ip_set_mcast_msfilter(sk, optval, optlen); 1337 break; 1338 case IP_IPSEC_POLICY: 1339 case IP_XFRM_POLICY: 1340 err = -EPERM; 1341 if (!sockopt_ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN)) 1342 break; 1343 err = xfrm_user_policy(sk, optname, optval, optlen); 1344 break; 1345 1346 default: 1347 err = -ENOPROTOOPT; 1348 break; 1349 } 1350 sockopt_release_sock(sk); 1351 if (needs_rtnl) 1352 rtnl_unlock(); 1353 return err; 1354 1355 e_inval: 1356 sockopt_release_sock(sk); 1357 if (needs_rtnl) 1358 rtnl_unlock(); 1359 return -EINVAL; 1360 } 1361 1362 /** 1363 * ipv4_pktinfo_prepare - transfer some info from rtable to skb 1364 * @sk: socket 1365 * @skb: buffer 1366 * 1367 * To support IP_CMSG_PKTINFO option, we store rt_iif and specific 1368 * destination in skb->cb[] before dst drop. 1369 * This way, receiver doesn't make cache line misses to read rtable. 1370 */ 1371 void ipv4_pktinfo_prepare(const struct sock *sk, struct sk_buff *skb) 1372 { 1373 struct in_pktinfo *pktinfo = PKTINFO_SKB_CB(skb); 1374 bool prepare = inet_test_bit(PKTINFO, sk) || 1375 ipv6_sk_rxinfo(sk); 1376 1377 if (prepare && skb_rtable(skb)) { 1378 /* skb->cb is overloaded: prior to this point it is IP{6}CB 1379 * which has interface index (iif) as the first member of the 1380 * underlying inet{6}_skb_parm struct. This code then overlays 1381 * PKTINFO_SKB_CB and in_pktinfo also has iif as the first 1382 * element so the iif is picked up from the prior IPCB. If iif 1383 * is the loopback interface, then return the sending interface 1384 * (e.g., process binds socket to eth0 for Tx which is 1385 * redirected to loopback in the rtable/dst). 1386 */ 1387 struct rtable *rt = skb_rtable(skb); 1388 bool l3slave = ipv4_l3mdev_skb(IPCB(skb)->flags); 1389 1390 if (pktinfo->ipi_ifindex == LOOPBACK_IFINDEX) 1391 pktinfo->ipi_ifindex = inet_iif(skb); 1392 else if (l3slave && rt && rt->rt_iif) 1393 pktinfo->ipi_ifindex = rt->rt_iif; 1394 1395 pktinfo->ipi_spec_dst.s_addr = fib_compute_spec_dst(skb); 1396 } else { 1397 pktinfo->ipi_ifindex = 0; 1398 pktinfo->ipi_spec_dst.s_addr = 0; 1399 } 1400 skb_dst_drop(skb); 1401 } 1402 1403 int ip_setsockopt(struct sock *sk, int level, int optname, sockptr_t optval, 1404 unsigned int optlen) 1405 { 1406 int err; 1407 1408 if (level != SOL_IP) 1409 return -ENOPROTOOPT; 1410 1411 err = do_ip_setsockopt(sk, level, optname, optval, optlen); 1412 #ifdef CONFIG_NETFILTER 1413 /* we need to exclude all possible ENOPROTOOPTs except default case */ 1414 if (err == -ENOPROTOOPT && optname != IP_HDRINCL && 1415 optname != IP_IPSEC_POLICY && 1416 optname != IP_XFRM_POLICY && 1417 !ip_mroute_opt(optname)) 1418 err = nf_setsockopt(sk, PF_INET, optname, optval, optlen); 1419 #endif 1420 return err; 1421 } 1422 EXPORT_SYMBOL(ip_setsockopt); 1423 1424 /* 1425 * Get the options. Note for future reference. The GET of IP options gets 1426 * the _received_ ones. The set sets the _sent_ ones. 1427 */ 1428 1429 static bool getsockopt_needs_rtnl(int optname) 1430 { 1431 switch (optname) { 1432 case IP_MSFILTER: 1433 case MCAST_MSFILTER: 1434 return true; 1435 } 1436 return false; 1437 } 1438 1439 static int ip_get_mcast_msfilter(struct sock *sk, sockptr_t optval, 1440 sockptr_t optlen, int len) 1441 { 1442 const int size0 = offsetof(struct group_filter, gf_slist_flex); 1443 struct group_filter gsf; 1444 int num, gsf_size; 1445 int err; 1446 1447 if (len < size0) 1448 return -EINVAL; 1449 if (copy_from_sockptr(&gsf, optval, size0)) 1450 return -EFAULT; 1451 1452 num = gsf.gf_numsrc; 1453 err = ip_mc_gsfget(sk, &gsf, optval, 1454 offsetof(struct group_filter, gf_slist_flex)); 1455 if (err) 1456 return err; 1457 if (gsf.gf_numsrc < num) 1458 num = gsf.gf_numsrc; 1459 gsf_size = GROUP_FILTER_SIZE(num); 1460 if (copy_to_sockptr(optlen, &gsf_size, sizeof(int)) || 1461 copy_to_sockptr(optval, &gsf, size0)) 1462 return -EFAULT; 1463 return 0; 1464 } 1465 1466 static int compat_ip_get_mcast_msfilter(struct sock *sk, sockptr_t optval, 1467 sockptr_t optlen, int len) 1468 { 1469 const int size0 = offsetof(struct compat_group_filter, gf_slist_flex); 1470 struct compat_group_filter gf32; 1471 struct group_filter gf; 1472 int num; 1473 int err; 1474 1475 if (len < size0) 1476 return -EINVAL; 1477 if (copy_from_sockptr(&gf32, optval, size0)) 1478 return -EFAULT; 1479 1480 gf.gf_interface = gf32.gf_interface; 1481 gf.gf_fmode = gf32.gf_fmode; 1482 num = gf.gf_numsrc = gf32.gf_numsrc; 1483 gf.gf_group = gf32.gf_group; 1484 1485 err = ip_mc_gsfget(sk, &gf, optval, 1486 offsetof(struct compat_group_filter, gf_slist_flex)); 1487 if (err) 1488 return err; 1489 if (gf.gf_numsrc < num) 1490 num = gf.gf_numsrc; 1491 len = GROUP_FILTER_SIZE(num) - (sizeof(gf) - sizeof(gf32)); 1492 if (copy_to_sockptr(optlen, &len, sizeof(int)) || 1493 copy_to_sockptr_offset(optval, offsetof(struct compat_group_filter, gf_fmode), 1494 &gf.gf_fmode, sizeof(gf.gf_fmode)) || 1495 copy_to_sockptr_offset(optval, offsetof(struct compat_group_filter, gf_numsrc), 1496 &gf.gf_numsrc, sizeof(gf.gf_numsrc))) 1497 return -EFAULT; 1498 return 0; 1499 } 1500 1501 int do_ip_getsockopt(struct sock *sk, int level, int optname, 1502 sockptr_t optval, sockptr_t optlen) 1503 { 1504 struct inet_sock *inet = inet_sk(sk); 1505 bool needs_rtnl = getsockopt_needs_rtnl(optname); 1506 int val, err = 0; 1507 int len; 1508 1509 if (level != SOL_IP) 1510 return -EOPNOTSUPP; 1511 1512 if (ip_mroute_opt(optname)) 1513 return ip_mroute_getsockopt(sk, optname, optval, optlen); 1514 1515 if (copy_from_sockptr(&len, optlen, sizeof(int))) 1516 return -EFAULT; 1517 if (len < 0) 1518 return -EINVAL; 1519 1520 /* Handle options that can be read without locking the socket. */ 1521 switch (optname) { 1522 case IP_PKTINFO: 1523 val = inet_test_bit(PKTINFO, sk); 1524 goto copyval; 1525 case IP_RECVTTL: 1526 val = inet_test_bit(TTL, sk); 1527 goto copyval; 1528 case IP_RECVTOS: 1529 val = inet_test_bit(TOS, sk); 1530 goto copyval; 1531 case IP_RECVOPTS: 1532 val = inet_test_bit(RECVOPTS, sk); 1533 goto copyval; 1534 case IP_RETOPTS: 1535 val = inet_test_bit(RETOPTS, sk); 1536 goto copyval; 1537 case IP_PASSSEC: 1538 val = inet_test_bit(PASSSEC, sk); 1539 goto copyval; 1540 case IP_RECVORIGDSTADDR: 1541 val = inet_test_bit(ORIGDSTADDR, sk); 1542 goto copyval; 1543 case IP_CHECKSUM: 1544 val = inet_test_bit(CHECKSUM, sk); 1545 goto copyval; 1546 case IP_RECVFRAGSIZE: 1547 val = inet_test_bit(RECVFRAGSIZE, sk); 1548 goto copyval; 1549 case IP_RECVERR: 1550 val = inet_test_bit(RECVERR, sk); 1551 goto copyval; 1552 case IP_RECVERR_RFC4884: 1553 val = inet_test_bit(RECVERR_RFC4884, sk); 1554 goto copyval; 1555 case IP_FREEBIND: 1556 val = inet_test_bit(FREEBIND, sk); 1557 goto copyval; 1558 case IP_HDRINCL: 1559 val = inet_test_bit(HDRINCL, sk); 1560 goto copyval; 1561 case IP_MULTICAST_LOOP: 1562 val = inet_test_bit(MC_LOOP, sk); 1563 goto copyval; 1564 case IP_MULTICAST_ALL: 1565 val = inet_test_bit(MC_ALL, sk); 1566 goto copyval; 1567 case IP_TRANSPARENT: 1568 val = inet_test_bit(TRANSPARENT, sk); 1569 goto copyval; 1570 case IP_NODEFRAG: 1571 val = inet_test_bit(NODEFRAG, sk); 1572 goto copyval; 1573 case IP_BIND_ADDRESS_NO_PORT: 1574 val = inet_test_bit(BIND_ADDRESS_NO_PORT, sk); 1575 goto copyval; 1576 case IP_TTL: 1577 val = READ_ONCE(inet->uc_ttl); 1578 if (val < 0) 1579 val = READ_ONCE(sock_net(sk)->ipv4.sysctl_ip_default_ttl); 1580 goto copyval; 1581 case IP_MINTTL: 1582 val = READ_ONCE(inet->min_ttl); 1583 goto copyval; 1584 case IP_MULTICAST_TTL: 1585 val = READ_ONCE(inet->mc_ttl); 1586 goto copyval; 1587 case IP_MTU_DISCOVER: 1588 val = READ_ONCE(inet->pmtudisc); 1589 goto copyval; 1590 case IP_TOS: 1591 val = READ_ONCE(inet->tos); 1592 goto copyval; 1593 case IP_OPTIONS: 1594 { 1595 unsigned char optbuf[sizeof(struct ip_options)+40]; 1596 struct ip_options *opt = (struct ip_options *)optbuf; 1597 struct ip_options_rcu *inet_opt; 1598 1599 rcu_read_lock(); 1600 inet_opt = rcu_dereference(inet->inet_opt); 1601 opt->optlen = 0; 1602 if (inet_opt) 1603 memcpy(optbuf, &inet_opt->opt, 1604 sizeof(struct ip_options) + 1605 inet_opt->opt.optlen); 1606 rcu_read_unlock(); 1607 1608 if (opt->optlen == 0) { 1609 len = 0; 1610 return copy_to_sockptr(optlen, &len, sizeof(int)); 1611 } 1612 1613 ip_options_undo(opt); 1614 1615 len = min_t(unsigned int, len, opt->optlen); 1616 if (copy_to_sockptr(optlen, &len, sizeof(int))) 1617 return -EFAULT; 1618 if (copy_to_sockptr(optval, opt->__data, len)) 1619 return -EFAULT; 1620 return 0; 1621 } 1622 case IP_MTU: 1623 { 1624 struct dst_entry *dst; 1625 val = 0; 1626 dst = sk_dst_get(sk); 1627 if (dst) { 1628 val = dst_mtu(dst); 1629 dst_release(dst); 1630 } 1631 if (!val) 1632 return -ENOTCONN; 1633 goto copyval; 1634 } 1635 case IP_PKTOPTIONS: 1636 { 1637 struct msghdr msg; 1638 1639 if (sk->sk_type != SOCK_STREAM) 1640 return -ENOPROTOOPT; 1641 1642 if (optval.is_kernel) { 1643 msg.msg_control_is_user = false; 1644 msg.msg_control = optval.kernel; 1645 } else { 1646 msg.msg_control_is_user = true; 1647 msg.msg_control_user = optval.user; 1648 } 1649 msg.msg_controllen = len; 1650 msg.msg_flags = in_compat_syscall() ? MSG_CMSG_COMPAT : 0; 1651 1652 if (inet_test_bit(PKTINFO, sk)) { 1653 struct in_pktinfo info; 1654 1655 info.ipi_addr.s_addr = READ_ONCE(inet->inet_rcv_saddr); 1656 info.ipi_spec_dst.s_addr = READ_ONCE(inet->inet_rcv_saddr); 1657 info.ipi_ifindex = READ_ONCE(inet->mc_index); 1658 put_cmsg(&msg, SOL_IP, IP_PKTINFO, sizeof(info), &info); 1659 } 1660 if (inet_test_bit(TTL, sk)) { 1661 int hlim = READ_ONCE(inet->mc_ttl); 1662 1663 put_cmsg(&msg, SOL_IP, IP_TTL, sizeof(hlim), &hlim); 1664 } 1665 if (inet_test_bit(TOS, sk)) { 1666 int tos = READ_ONCE(inet->rcv_tos); 1667 put_cmsg(&msg, SOL_IP, IP_TOS, sizeof(tos), &tos); 1668 } 1669 len -= msg.msg_controllen; 1670 return copy_to_sockptr(optlen, &len, sizeof(int)); 1671 } 1672 case IP_UNICAST_IF: 1673 val = (__force int)htonl((__u32) READ_ONCE(inet->uc_index)); 1674 goto copyval; 1675 case IP_MULTICAST_IF: 1676 { 1677 struct in_addr addr; 1678 len = min_t(unsigned int, len, sizeof(struct in_addr)); 1679 addr.s_addr = READ_ONCE(inet->mc_addr); 1680 1681 if (copy_to_sockptr(optlen, &len, sizeof(int))) 1682 return -EFAULT; 1683 if (copy_to_sockptr(optval, &addr, len)) 1684 return -EFAULT; 1685 return 0; 1686 } 1687 case IP_LOCAL_PORT_RANGE: 1688 val = READ_ONCE(inet->local_port_range); 1689 goto copyval; 1690 } 1691 1692 if (needs_rtnl) 1693 rtnl_lock(); 1694 sockopt_lock_sock(sk); 1695 1696 switch (optname) { 1697 case IP_MSFILTER: 1698 { 1699 struct ip_msfilter msf; 1700 1701 if (len < IP_MSFILTER_SIZE(0)) { 1702 err = -EINVAL; 1703 goto out; 1704 } 1705 if (copy_from_sockptr(&msf, optval, IP_MSFILTER_SIZE(0))) { 1706 err = -EFAULT; 1707 goto out; 1708 } 1709 err = ip_mc_msfget(sk, &msf, optval, optlen); 1710 goto out; 1711 } 1712 case MCAST_MSFILTER: 1713 if (in_compat_syscall()) 1714 err = compat_ip_get_mcast_msfilter(sk, optval, optlen, 1715 len); 1716 else 1717 err = ip_get_mcast_msfilter(sk, optval, optlen, len); 1718 goto out; 1719 case IP_PROTOCOL: 1720 val = inet_sk(sk)->inet_num; 1721 break; 1722 default: 1723 sockopt_release_sock(sk); 1724 return -ENOPROTOOPT; 1725 } 1726 sockopt_release_sock(sk); 1727 copyval: 1728 if (len < sizeof(int) && len > 0 && val >= 0 && val <= 255) { 1729 unsigned char ucval = (unsigned char)val; 1730 len = 1; 1731 if (copy_to_sockptr(optlen, &len, sizeof(int))) 1732 return -EFAULT; 1733 if (copy_to_sockptr(optval, &ucval, 1)) 1734 return -EFAULT; 1735 } else { 1736 len = min_t(unsigned int, sizeof(int), len); 1737 if (copy_to_sockptr(optlen, &len, sizeof(int))) 1738 return -EFAULT; 1739 if (copy_to_sockptr(optval, &val, len)) 1740 return -EFAULT; 1741 } 1742 return 0; 1743 1744 out: 1745 sockopt_release_sock(sk); 1746 if (needs_rtnl) 1747 rtnl_unlock(); 1748 return err; 1749 } 1750 1751 int ip_getsockopt(struct sock *sk, int level, 1752 int optname, char __user *optval, int __user *optlen) 1753 { 1754 int err; 1755 1756 err = do_ip_getsockopt(sk, level, optname, 1757 USER_SOCKPTR(optval), USER_SOCKPTR(optlen)); 1758 1759 #ifdef CONFIG_NETFILTER 1760 /* we need to exclude all possible ENOPROTOOPTs except default case */ 1761 if (err == -ENOPROTOOPT && optname != IP_PKTOPTIONS && 1762 !ip_mroute_opt(optname)) { 1763 int len; 1764 1765 if (get_user(len, optlen)) 1766 return -EFAULT; 1767 1768 err = nf_getsockopt(sk, PF_INET, optname, optval, &len); 1769 if (err >= 0) 1770 err = put_user(len, optlen); 1771 return err; 1772 } 1773 #endif 1774 return err; 1775 } 1776 EXPORT_SYMBOL(ip_getsockopt); 1777