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 struct sockaddr_in sin; 150 const struct iphdr *iph = ip_hdr(skb); 151 __be16 *ports = (__be16 *)skb_transport_header(skb); 152 153 if (skb_transport_offset(skb) + 4 > (int)skb->len) 154 return; 155 156 /* All current transport protocols have the port numbers in the 157 * first four bytes of the transport header and this function is 158 * written with this assumption in mind. 159 */ 160 161 sin.sin_family = AF_INET; 162 sin.sin_addr.s_addr = iph->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 struct inet_sock *inet = inet_sk(sk); 173 unsigned int flags = inet->cmsg_flags; 174 175 /* Ordered by supposed usage frequency */ 176 if (flags & IP_CMSG_PKTINFO) { 177 ip_cmsg_recv_pktinfo(msg, skb); 178 179 flags &= ~IP_CMSG_PKTINFO; 180 if (!flags) 181 return; 182 } 183 184 if (flags & IP_CMSG_TTL) { 185 ip_cmsg_recv_ttl(msg, skb); 186 187 flags &= ~IP_CMSG_TTL; 188 if (!flags) 189 return; 190 } 191 192 if (flags & IP_CMSG_TOS) { 193 ip_cmsg_recv_tos(msg, skb); 194 195 flags &= ~IP_CMSG_TOS; 196 if (!flags) 197 return; 198 } 199 200 if (flags & IP_CMSG_RECVOPTS) { 201 ip_cmsg_recv_opts(msg, skb); 202 203 flags &= ~IP_CMSG_RECVOPTS; 204 if (!flags) 205 return; 206 } 207 208 if (flags & IP_CMSG_RETOPTS) { 209 ip_cmsg_recv_retopts(sock_net(sk), msg, skb); 210 211 flags &= ~IP_CMSG_RETOPTS; 212 if (!flags) 213 return; 214 } 215 216 if (flags & IP_CMSG_PASSSEC) { 217 ip_cmsg_recv_security(msg, skb); 218 219 flags &= ~IP_CMSG_PASSSEC; 220 if (!flags) 221 return; 222 } 223 224 if (flags & IP_CMSG_ORIGDSTADDR) { 225 ip_cmsg_recv_dstaddr(msg, skb); 226 227 flags &= ~IP_CMSG_ORIGDSTADDR; 228 if (!flags) 229 return; 230 } 231 232 if (flags & IP_CMSG_CHECKSUM) 233 ip_cmsg_recv_checksum(msg, skb, tlen, offset); 234 235 if (flags & IP_CMSG_RECVFRAGSIZE) 236 ip_cmsg_recv_fragsize(msg, skb); 237 } 238 EXPORT_SYMBOL(ip_cmsg_recv_offset); 239 240 int ip_cmsg_send(struct sock *sk, struct msghdr *msg, struct ipcm_cookie *ipc, 241 bool allow_ipv6) 242 { 243 int err, val; 244 struct cmsghdr *cmsg; 245 struct net *net = sock_net(sk); 246 247 for_each_cmsghdr(cmsg, msg) { 248 if (!CMSG_OK(msg, cmsg)) 249 return -EINVAL; 250 #if IS_ENABLED(CONFIG_IPV6) 251 if (allow_ipv6 && 252 cmsg->cmsg_level == SOL_IPV6 && 253 cmsg->cmsg_type == IPV6_PKTINFO) { 254 struct in6_pktinfo *src_info; 255 256 if (cmsg->cmsg_len < CMSG_LEN(sizeof(*src_info))) 257 return -EINVAL; 258 src_info = (struct in6_pktinfo *)CMSG_DATA(cmsg); 259 if (!ipv6_addr_v4mapped(&src_info->ipi6_addr)) 260 return -EINVAL; 261 if (src_info->ipi6_ifindex) 262 ipc->oif = src_info->ipi6_ifindex; 263 ipc->addr = src_info->ipi6_addr.s6_addr32[3]; 264 continue; 265 } 266 #endif 267 if (cmsg->cmsg_level == SOL_SOCKET) { 268 err = __sock_cmsg_send(sk, msg, cmsg, &ipc->sockc); 269 if (err) 270 return err; 271 continue; 272 } 273 274 if (cmsg->cmsg_level != SOL_IP) 275 continue; 276 switch (cmsg->cmsg_type) { 277 case IP_RETOPTS: 278 err = cmsg->cmsg_len - sizeof(struct cmsghdr); 279 280 /* Our caller is responsible for freeing ipc->opt */ 281 err = ip_options_get(net, &ipc->opt, CMSG_DATA(cmsg), 282 err < 40 ? err : 40); 283 if (err) 284 return err; 285 break; 286 case IP_PKTINFO: 287 { 288 struct in_pktinfo *info; 289 if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct in_pktinfo))) 290 return -EINVAL; 291 info = (struct in_pktinfo *)CMSG_DATA(cmsg); 292 if (info->ipi_ifindex) 293 ipc->oif = info->ipi_ifindex; 294 ipc->addr = info->ipi_spec_dst.s_addr; 295 break; 296 } 297 case IP_TTL: 298 if (cmsg->cmsg_len != CMSG_LEN(sizeof(int))) 299 return -EINVAL; 300 val = *(int *)CMSG_DATA(cmsg); 301 if (val < 1 || val > 255) 302 return -EINVAL; 303 ipc->ttl = val; 304 break; 305 case IP_TOS: 306 if (cmsg->cmsg_len == CMSG_LEN(sizeof(int))) 307 val = *(int *)CMSG_DATA(cmsg); 308 else if (cmsg->cmsg_len == CMSG_LEN(sizeof(u8))) 309 val = *(u8 *)CMSG_DATA(cmsg); 310 else 311 return -EINVAL; 312 if (val < 0 || val > 255) 313 return -EINVAL; 314 ipc->tos = val; 315 ipc->priority = rt_tos2priority(ipc->tos); 316 break; 317 318 default: 319 return -EINVAL; 320 } 321 } 322 return 0; 323 } 324 325 static void ip_ra_destroy_rcu(struct rcu_head *head) 326 { 327 struct ip_ra_chain *ra = container_of(head, struct ip_ra_chain, rcu); 328 329 sock_put(ra->saved_sk); 330 kfree(ra); 331 } 332 333 int ip_ra_control(struct sock *sk, unsigned char on, 334 void (*destructor)(struct sock *)) 335 { 336 struct ip_ra_chain *ra, *new_ra; 337 struct ip_ra_chain __rcu **rap; 338 struct net *net = sock_net(sk); 339 340 if (sk->sk_type != SOCK_RAW || inet_sk(sk)->inet_num == IPPROTO_RAW) 341 return -EINVAL; 342 343 new_ra = on ? kmalloc(sizeof(*new_ra), GFP_KERNEL) : NULL; 344 345 mutex_lock(&net->ipv4.ra_mutex); 346 for (rap = &net->ipv4.ra_chain; 347 (ra = rcu_dereference_protected(*rap, 348 lockdep_is_held(&net->ipv4.ra_mutex))) != NULL; 349 rap = &ra->next) { 350 if (ra->sk == sk) { 351 if (on) { 352 mutex_unlock(&net->ipv4.ra_mutex); 353 kfree(new_ra); 354 return -EADDRINUSE; 355 } 356 /* dont let ip_call_ra_chain() use sk again */ 357 ra->sk = NULL; 358 RCU_INIT_POINTER(*rap, ra->next); 359 mutex_unlock(&net->ipv4.ra_mutex); 360 361 if (ra->destructor) 362 ra->destructor(sk); 363 /* 364 * Delay sock_put(sk) and kfree(ra) after one rcu grace 365 * period. This guarantee ip_call_ra_chain() dont need 366 * to mess with socket refcounts. 367 */ 368 ra->saved_sk = sk; 369 call_rcu(&ra->rcu, ip_ra_destroy_rcu); 370 return 0; 371 } 372 } 373 if (!new_ra) { 374 mutex_unlock(&net->ipv4.ra_mutex); 375 return -ENOBUFS; 376 } 377 new_ra->sk = sk; 378 new_ra->destructor = destructor; 379 380 RCU_INIT_POINTER(new_ra->next, ra); 381 rcu_assign_pointer(*rap, new_ra); 382 sock_hold(sk); 383 mutex_unlock(&net->ipv4.ra_mutex); 384 385 return 0; 386 } 387 388 void ip_icmp_error(struct sock *sk, struct sk_buff *skb, int err, 389 __be16 port, u32 info, u8 *payload) 390 { 391 struct sock_exterr_skb *serr; 392 393 skb = skb_clone(skb, GFP_ATOMIC); 394 if (!skb) 395 return; 396 397 serr = SKB_EXT_ERR(skb); 398 serr->ee.ee_errno = err; 399 serr->ee.ee_origin = SO_EE_ORIGIN_ICMP; 400 serr->ee.ee_type = icmp_hdr(skb)->type; 401 serr->ee.ee_code = icmp_hdr(skb)->code; 402 serr->ee.ee_pad = 0; 403 serr->ee.ee_info = info; 404 serr->ee.ee_data = 0; 405 serr->addr_offset = (u8 *)&(((struct iphdr *)(icmp_hdr(skb) + 1))->daddr) - 406 skb_network_header(skb); 407 serr->port = port; 408 409 if (skb_pull(skb, payload - skb->data)) { 410 skb_reset_transport_header(skb); 411 if (sock_queue_err_skb(sk, skb) == 0) 412 return; 413 } 414 kfree_skb(skb); 415 } 416 417 void ip_local_error(struct sock *sk, int err, __be32 daddr, __be16 port, u32 info) 418 { 419 struct inet_sock *inet = inet_sk(sk); 420 struct sock_exterr_skb *serr; 421 struct iphdr *iph; 422 struct sk_buff *skb; 423 424 if (!inet->recverr) 425 return; 426 427 skb = alloc_skb(sizeof(struct iphdr), GFP_ATOMIC); 428 if (!skb) 429 return; 430 431 skb_put(skb, sizeof(struct iphdr)); 432 skb_reset_network_header(skb); 433 iph = ip_hdr(skb); 434 iph->daddr = daddr; 435 436 serr = SKB_EXT_ERR(skb); 437 serr->ee.ee_errno = err; 438 serr->ee.ee_origin = SO_EE_ORIGIN_LOCAL; 439 serr->ee.ee_type = 0; 440 serr->ee.ee_code = 0; 441 serr->ee.ee_pad = 0; 442 serr->ee.ee_info = info; 443 serr->ee.ee_data = 0; 444 serr->addr_offset = (u8 *)&iph->daddr - skb_network_header(skb); 445 serr->port = port; 446 447 __skb_pull(skb, skb_tail_pointer(skb) - skb->data); 448 skb_reset_transport_header(skb); 449 450 if (sock_queue_err_skb(sk, skb)) 451 kfree_skb(skb); 452 } 453 454 /* For some errors we have valid addr_offset even with zero payload and 455 * zero port. Also, addr_offset should be supported if port is set. 456 */ 457 static inline bool ipv4_datagram_support_addr(struct sock_exterr_skb *serr) 458 { 459 return serr->ee.ee_origin == SO_EE_ORIGIN_ICMP || 460 serr->ee.ee_origin == SO_EE_ORIGIN_LOCAL || serr->port; 461 } 462 463 /* IPv4 supports cmsg on all imcp errors and some timestamps 464 * 465 * Timestamp code paths do not initialize the fields expected by cmsg: 466 * the PKTINFO fields in skb->cb[]. Fill those in here. 467 */ 468 static bool ipv4_datagram_support_cmsg(const struct sock *sk, 469 struct sk_buff *skb, 470 int ee_origin) 471 { 472 struct in_pktinfo *info; 473 474 if (ee_origin == SO_EE_ORIGIN_ICMP) 475 return true; 476 477 if (ee_origin == SO_EE_ORIGIN_LOCAL) 478 return false; 479 480 /* Support IP_PKTINFO on tstamp packets if requested, to correlate 481 * timestamp with egress dev. Not possible for packets without iif 482 * or without payload (SOF_TIMESTAMPING_OPT_TSONLY). 483 */ 484 info = PKTINFO_SKB_CB(skb); 485 if (!(sk->sk_tsflags & SOF_TIMESTAMPING_OPT_CMSG) || 486 !info->ipi_ifindex) 487 return false; 488 489 info->ipi_spec_dst.s_addr = ip_hdr(skb)->saddr; 490 return true; 491 } 492 493 /* 494 * Handle MSG_ERRQUEUE 495 */ 496 int ip_recv_error(struct sock *sk, struct msghdr *msg, int len, int *addr_len) 497 { 498 struct sock_exterr_skb *serr; 499 struct sk_buff *skb; 500 DECLARE_SOCKADDR(struct sockaddr_in *, sin, msg->msg_name); 501 struct { 502 struct sock_extended_err ee; 503 struct sockaddr_in offender; 504 } errhdr; 505 int err; 506 int copied; 507 508 WARN_ON_ONCE(sk->sk_family == AF_INET6); 509 510 err = -EAGAIN; 511 skb = sock_dequeue_err_skb(sk); 512 if (!skb) 513 goto out; 514 515 copied = skb->len; 516 if (copied > len) { 517 msg->msg_flags |= MSG_TRUNC; 518 copied = len; 519 } 520 err = skb_copy_datagram_msg(skb, 0, msg, copied); 521 if (unlikely(err)) { 522 kfree_skb(skb); 523 return err; 524 } 525 sock_recv_timestamp(msg, sk, skb); 526 527 serr = SKB_EXT_ERR(skb); 528 529 if (sin && ipv4_datagram_support_addr(serr)) { 530 sin->sin_family = AF_INET; 531 sin->sin_addr.s_addr = *(__be32 *)(skb_network_header(skb) + 532 serr->addr_offset); 533 sin->sin_port = serr->port; 534 memset(&sin->sin_zero, 0, sizeof(sin->sin_zero)); 535 *addr_len = sizeof(*sin); 536 } 537 538 memcpy(&errhdr.ee, &serr->ee, sizeof(struct sock_extended_err)); 539 sin = &errhdr.offender; 540 memset(sin, 0, sizeof(*sin)); 541 542 if (ipv4_datagram_support_cmsg(sk, skb, serr->ee.ee_origin)) { 543 sin->sin_family = AF_INET; 544 sin->sin_addr.s_addr = ip_hdr(skb)->saddr; 545 if (inet_sk(sk)->cmsg_flags) 546 ip_cmsg_recv(msg, skb); 547 } 548 549 put_cmsg(msg, SOL_IP, IP_RECVERR, sizeof(errhdr), &errhdr); 550 551 /* Now we could try to dump offended packet options */ 552 553 msg->msg_flags |= MSG_ERRQUEUE; 554 err = copied; 555 556 consume_skb(skb); 557 out: 558 return err; 559 } 560 561 562 /* 563 * Socket option code for IP. This is the end of the line after any 564 * TCP,UDP etc options on an IP socket. 565 */ 566 static bool setsockopt_needs_rtnl(int optname) 567 { 568 switch (optname) { 569 case IP_ADD_MEMBERSHIP: 570 case IP_ADD_SOURCE_MEMBERSHIP: 571 case IP_BLOCK_SOURCE: 572 case IP_DROP_MEMBERSHIP: 573 case IP_DROP_SOURCE_MEMBERSHIP: 574 case IP_MSFILTER: 575 case IP_UNBLOCK_SOURCE: 576 case MCAST_BLOCK_SOURCE: 577 case MCAST_MSFILTER: 578 case MCAST_JOIN_GROUP: 579 case MCAST_JOIN_SOURCE_GROUP: 580 case MCAST_LEAVE_GROUP: 581 case MCAST_LEAVE_SOURCE_GROUP: 582 case MCAST_UNBLOCK_SOURCE: 583 return true; 584 } 585 return false; 586 } 587 588 static int do_ip_setsockopt(struct sock *sk, int level, 589 int optname, char __user *optval, unsigned int optlen) 590 { 591 struct inet_sock *inet = inet_sk(sk); 592 struct net *net = sock_net(sk); 593 int val = 0, err; 594 bool needs_rtnl = setsockopt_needs_rtnl(optname); 595 596 switch (optname) { 597 case IP_PKTINFO: 598 case IP_RECVTTL: 599 case IP_RECVOPTS: 600 case IP_RECVTOS: 601 case IP_RETOPTS: 602 case IP_TOS: 603 case IP_TTL: 604 case IP_HDRINCL: 605 case IP_MTU_DISCOVER: 606 case IP_RECVERR: 607 case IP_ROUTER_ALERT: 608 case IP_FREEBIND: 609 case IP_PASSSEC: 610 case IP_TRANSPARENT: 611 case IP_MINTTL: 612 case IP_NODEFRAG: 613 case IP_BIND_ADDRESS_NO_PORT: 614 case IP_UNICAST_IF: 615 case IP_MULTICAST_TTL: 616 case IP_MULTICAST_ALL: 617 case IP_MULTICAST_LOOP: 618 case IP_RECVORIGDSTADDR: 619 case IP_CHECKSUM: 620 case IP_RECVFRAGSIZE: 621 if (optlen >= sizeof(int)) { 622 if (get_user(val, (int __user *) optval)) 623 return -EFAULT; 624 } else if (optlen >= sizeof(char)) { 625 unsigned char ucval; 626 627 if (get_user(ucval, (unsigned char __user *) optval)) 628 return -EFAULT; 629 val = (int) ucval; 630 } 631 } 632 633 /* If optlen==0, it is equivalent to val == 0 */ 634 635 if (optname == IP_ROUTER_ALERT) 636 return ip_ra_control(sk, val ? 1 : 0, NULL); 637 if (ip_mroute_opt(optname)) 638 return ip_mroute_setsockopt(sk, optname, optval, optlen); 639 640 err = 0; 641 if (needs_rtnl) 642 rtnl_lock(); 643 lock_sock(sk); 644 645 switch (optname) { 646 case IP_OPTIONS: 647 { 648 struct ip_options_rcu *old, *opt = NULL; 649 650 if (optlen > 40) 651 goto e_inval; 652 err = ip_options_get_from_user(sock_net(sk), &opt, 653 optval, optlen); 654 if (err) 655 break; 656 old = rcu_dereference_protected(inet->inet_opt, 657 lockdep_sock_is_held(sk)); 658 if (inet->is_icsk) { 659 struct inet_connection_sock *icsk = inet_csk(sk); 660 #if IS_ENABLED(CONFIG_IPV6) 661 if (sk->sk_family == PF_INET || 662 (!((1 << sk->sk_state) & 663 (TCPF_LISTEN | TCPF_CLOSE)) && 664 inet->inet_daddr != LOOPBACK4_IPV6)) { 665 #endif 666 if (old) 667 icsk->icsk_ext_hdr_len -= old->opt.optlen; 668 if (opt) 669 icsk->icsk_ext_hdr_len += opt->opt.optlen; 670 icsk->icsk_sync_mss(sk, icsk->icsk_pmtu_cookie); 671 #if IS_ENABLED(CONFIG_IPV6) 672 } 673 #endif 674 } 675 rcu_assign_pointer(inet->inet_opt, opt); 676 if (old) 677 kfree_rcu(old, rcu); 678 break; 679 } 680 case IP_PKTINFO: 681 if (val) 682 inet->cmsg_flags |= IP_CMSG_PKTINFO; 683 else 684 inet->cmsg_flags &= ~IP_CMSG_PKTINFO; 685 break; 686 case IP_RECVTTL: 687 if (val) 688 inet->cmsg_flags |= IP_CMSG_TTL; 689 else 690 inet->cmsg_flags &= ~IP_CMSG_TTL; 691 break; 692 case IP_RECVTOS: 693 if (val) 694 inet->cmsg_flags |= IP_CMSG_TOS; 695 else 696 inet->cmsg_flags &= ~IP_CMSG_TOS; 697 break; 698 case IP_RECVOPTS: 699 if (val) 700 inet->cmsg_flags |= IP_CMSG_RECVOPTS; 701 else 702 inet->cmsg_flags &= ~IP_CMSG_RECVOPTS; 703 break; 704 case IP_RETOPTS: 705 if (val) 706 inet->cmsg_flags |= IP_CMSG_RETOPTS; 707 else 708 inet->cmsg_flags &= ~IP_CMSG_RETOPTS; 709 break; 710 case IP_PASSSEC: 711 if (val) 712 inet->cmsg_flags |= IP_CMSG_PASSSEC; 713 else 714 inet->cmsg_flags &= ~IP_CMSG_PASSSEC; 715 break; 716 case IP_RECVORIGDSTADDR: 717 if (val) 718 inet->cmsg_flags |= IP_CMSG_ORIGDSTADDR; 719 else 720 inet->cmsg_flags &= ~IP_CMSG_ORIGDSTADDR; 721 break; 722 case IP_CHECKSUM: 723 if (val) { 724 if (!(inet->cmsg_flags & IP_CMSG_CHECKSUM)) { 725 inet_inc_convert_csum(sk); 726 inet->cmsg_flags |= IP_CMSG_CHECKSUM; 727 } 728 } else { 729 if (inet->cmsg_flags & IP_CMSG_CHECKSUM) { 730 inet_dec_convert_csum(sk); 731 inet->cmsg_flags &= ~IP_CMSG_CHECKSUM; 732 } 733 } 734 break; 735 case IP_RECVFRAGSIZE: 736 if (sk->sk_type != SOCK_RAW && sk->sk_type != SOCK_DGRAM) 737 goto e_inval; 738 if (val) 739 inet->cmsg_flags |= IP_CMSG_RECVFRAGSIZE; 740 else 741 inet->cmsg_flags &= ~IP_CMSG_RECVFRAGSIZE; 742 break; 743 case IP_TOS: /* This sets both TOS and Precedence */ 744 if (sk->sk_type == SOCK_STREAM) { 745 val &= ~INET_ECN_MASK; 746 val |= inet->tos & INET_ECN_MASK; 747 } 748 if (inet->tos != val) { 749 inet->tos = val; 750 sk->sk_priority = rt_tos2priority(val); 751 sk_dst_reset(sk); 752 } 753 break; 754 case IP_TTL: 755 if (optlen < 1) 756 goto e_inval; 757 if (val != -1 && (val < 1 || val > 255)) 758 goto e_inval; 759 inet->uc_ttl = val; 760 break; 761 case IP_HDRINCL: 762 if (sk->sk_type != SOCK_RAW) { 763 err = -ENOPROTOOPT; 764 break; 765 } 766 inet->hdrincl = val ? 1 : 0; 767 break; 768 case IP_NODEFRAG: 769 if (sk->sk_type != SOCK_RAW) { 770 err = -ENOPROTOOPT; 771 break; 772 } 773 inet->nodefrag = val ? 1 : 0; 774 break; 775 case IP_BIND_ADDRESS_NO_PORT: 776 inet->bind_address_no_port = val ? 1 : 0; 777 break; 778 case IP_MTU_DISCOVER: 779 if (val < IP_PMTUDISC_DONT || val > IP_PMTUDISC_OMIT) 780 goto e_inval; 781 inet->pmtudisc = val; 782 break; 783 case IP_RECVERR: 784 inet->recverr = !!val; 785 if (!val) 786 skb_queue_purge(&sk->sk_error_queue); 787 break; 788 case IP_MULTICAST_TTL: 789 if (sk->sk_type == SOCK_STREAM) 790 goto e_inval; 791 if (optlen < 1) 792 goto e_inval; 793 if (val == -1) 794 val = 1; 795 if (val < 0 || val > 255) 796 goto e_inval; 797 inet->mc_ttl = val; 798 break; 799 case IP_MULTICAST_LOOP: 800 if (optlen < 1) 801 goto e_inval; 802 inet->mc_loop = !!val; 803 break; 804 case IP_UNICAST_IF: 805 { 806 struct net_device *dev = NULL; 807 int ifindex; 808 int midx; 809 810 if (optlen != sizeof(int)) 811 goto e_inval; 812 813 ifindex = (__force int)ntohl((__force __be32)val); 814 if (ifindex == 0) { 815 inet->uc_index = 0; 816 err = 0; 817 break; 818 } 819 820 dev = dev_get_by_index(sock_net(sk), ifindex); 821 err = -EADDRNOTAVAIL; 822 if (!dev) 823 break; 824 825 midx = l3mdev_master_ifindex(dev); 826 dev_put(dev); 827 828 err = -EINVAL; 829 if (sk->sk_bound_dev_if && 830 (!midx || midx != sk->sk_bound_dev_if)) 831 break; 832 833 inet->uc_index = ifindex; 834 err = 0; 835 break; 836 } 837 case IP_MULTICAST_IF: 838 { 839 struct ip_mreqn mreq; 840 struct net_device *dev = NULL; 841 int midx; 842 843 if (sk->sk_type == SOCK_STREAM) 844 goto e_inval; 845 /* 846 * Check the arguments are allowable 847 */ 848 849 if (optlen < sizeof(struct in_addr)) 850 goto e_inval; 851 852 err = -EFAULT; 853 if (optlen >= sizeof(struct ip_mreqn)) { 854 if (copy_from_user(&mreq, optval, sizeof(mreq))) 855 break; 856 } else { 857 memset(&mreq, 0, sizeof(mreq)); 858 if (optlen >= sizeof(struct ip_mreq)) { 859 if (copy_from_user(&mreq, optval, 860 sizeof(struct ip_mreq))) 861 break; 862 } else if (optlen >= sizeof(struct in_addr)) { 863 if (copy_from_user(&mreq.imr_address, optval, 864 sizeof(struct in_addr))) 865 break; 866 } 867 } 868 869 if (!mreq.imr_ifindex) { 870 if (mreq.imr_address.s_addr == htonl(INADDR_ANY)) { 871 inet->mc_index = 0; 872 inet->mc_addr = 0; 873 err = 0; 874 break; 875 } 876 dev = ip_dev_find(sock_net(sk), mreq.imr_address.s_addr); 877 if (dev) 878 mreq.imr_ifindex = dev->ifindex; 879 } else 880 dev = dev_get_by_index(sock_net(sk), mreq.imr_ifindex); 881 882 883 err = -EADDRNOTAVAIL; 884 if (!dev) 885 break; 886 887 midx = l3mdev_master_ifindex(dev); 888 889 dev_put(dev); 890 891 err = -EINVAL; 892 if (sk->sk_bound_dev_if && 893 mreq.imr_ifindex != sk->sk_bound_dev_if && 894 (!midx || midx != sk->sk_bound_dev_if)) 895 break; 896 897 inet->mc_index = mreq.imr_ifindex; 898 inet->mc_addr = mreq.imr_address.s_addr; 899 err = 0; 900 break; 901 } 902 903 case IP_ADD_MEMBERSHIP: 904 case IP_DROP_MEMBERSHIP: 905 { 906 struct ip_mreqn mreq; 907 908 err = -EPROTO; 909 if (inet_sk(sk)->is_icsk) 910 break; 911 912 if (optlen < sizeof(struct ip_mreq)) 913 goto e_inval; 914 err = -EFAULT; 915 if (optlen >= sizeof(struct ip_mreqn)) { 916 if (copy_from_user(&mreq, optval, sizeof(mreq))) 917 break; 918 } else { 919 memset(&mreq, 0, sizeof(mreq)); 920 if (copy_from_user(&mreq, optval, sizeof(struct ip_mreq))) 921 break; 922 } 923 924 if (optname == IP_ADD_MEMBERSHIP) 925 err = ip_mc_join_group(sk, &mreq); 926 else 927 err = ip_mc_leave_group(sk, &mreq); 928 break; 929 } 930 case IP_MSFILTER: 931 { 932 struct ip_msfilter *msf; 933 934 if (optlen < IP_MSFILTER_SIZE(0)) 935 goto e_inval; 936 if (optlen > sysctl_optmem_max) { 937 err = -ENOBUFS; 938 break; 939 } 940 msf = memdup_user(optval, optlen); 941 if (IS_ERR(msf)) { 942 err = PTR_ERR(msf); 943 break; 944 } 945 /* numsrc >= (1G-4) overflow in 32 bits */ 946 if (msf->imsf_numsrc >= 0x3ffffffcU || 947 msf->imsf_numsrc > net->ipv4.sysctl_igmp_max_msf) { 948 kfree(msf); 949 err = -ENOBUFS; 950 break; 951 } 952 if (IP_MSFILTER_SIZE(msf->imsf_numsrc) > optlen) { 953 kfree(msf); 954 err = -EINVAL; 955 break; 956 } 957 err = ip_mc_msfilter(sk, msf, 0); 958 kfree(msf); 959 break; 960 } 961 case IP_BLOCK_SOURCE: 962 case IP_UNBLOCK_SOURCE: 963 case IP_ADD_SOURCE_MEMBERSHIP: 964 case IP_DROP_SOURCE_MEMBERSHIP: 965 { 966 struct ip_mreq_source mreqs; 967 int omode, add; 968 969 if (optlen != sizeof(struct ip_mreq_source)) 970 goto e_inval; 971 if (copy_from_user(&mreqs, optval, sizeof(mreqs))) { 972 err = -EFAULT; 973 break; 974 } 975 if (optname == IP_BLOCK_SOURCE) { 976 omode = MCAST_EXCLUDE; 977 add = 1; 978 } else if (optname == IP_UNBLOCK_SOURCE) { 979 omode = MCAST_EXCLUDE; 980 add = 0; 981 } else if (optname == IP_ADD_SOURCE_MEMBERSHIP) { 982 struct ip_mreqn mreq; 983 984 mreq.imr_multiaddr.s_addr = mreqs.imr_multiaddr; 985 mreq.imr_address.s_addr = mreqs.imr_interface; 986 mreq.imr_ifindex = 0; 987 err = ip_mc_join_group(sk, &mreq); 988 if (err && err != -EADDRINUSE) 989 break; 990 omode = MCAST_INCLUDE; 991 add = 1; 992 } else /* IP_DROP_SOURCE_MEMBERSHIP */ { 993 omode = MCAST_INCLUDE; 994 add = 0; 995 } 996 err = ip_mc_source(add, omode, sk, &mreqs, 0); 997 break; 998 } 999 case MCAST_JOIN_GROUP: 1000 case MCAST_LEAVE_GROUP: 1001 { 1002 struct group_req greq; 1003 struct sockaddr_in *psin; 1004 struct ip_mreqn mreq; 1005 1006 if (optlen < sizeof(struct group_req)) 1007 goto e_inval; 1008 err = -EFAULT; 1009 if (copy_from_user(&greq, optval, sizeof(greq))) 1010 break; 1011 psin = (struct sockaddr_in *)&greq.gr_group; 1012 if (psin->sin_family != AF_INET) 1013 goto e_inval; 1014 memset(&mreq, 0, sizeof(mreq)); 1015 mreq.imr_multiaddr = psin->sin_addr; 1016 mreq.imr_ifindex = greq.gr_interface; 1017 1018 if (optname == MCAST_JOIN_GROUP) 1019 err = ip_mc_join_group(sk, &mreq); 1020 else 1021 err = ip_mc_leave_group(sk, &mreq); 1022 break; 1023 } 1024 case MCAST_JOIN_SOURCE_GROUP: 1025 case MCAST_LEAVE_SOURCE_GROUP: 1026 case MCAST_BLOCK_SOURCE: 1027 case MCAST_UNBLOCK_SOURCE: 1028 { 1029 struct group_source_req greqs; 1030 struct ip_mreq_source mreqs; 1031 struct sockaddr_in *psin; 1032 int omode, add; 1033 1034 if (optlen != sizeof(struct group_source_req)) 1035 goto e_inval; 1036 if (copy_from_user(&greqs, optval, sizeof(greqs))) { 1037 err = -EFAULT; 1038 break; 1039 } 1040 if (greqs.gsr_group.ss_family != AF_INET || 1041 greqs.gsr_source.ss_family != AF_INET) { 1042 err = -EADDRNOTAVAIL; 1043 break; 1044 } 1045 psin = (struct sockaddr_in *)&greqs.gsr_group; 1046 mreqs.imr_multiaddr = psin->sin_addr.s_addr; 1047 psin = (struct sockaddr_in *)&greqs.gsr_source; 1048 mreqs.imr_sourceaddr = psin->sin_addr.s_addr; 1049 mreqs.imr_interface = 0; /* use index for mc_source */ 1050 1051 if (optname == MCAST_BLOCK_SOURCE) { 1052 omode = MCAST_EXCLUDE; 1053 add = 1; 1054 } else if (optname == MCAST_UNBLOCK_SOURCE) { 1055 omode = MCAST_EXCLUDE; 1056 add = 0; 1057 } else if (optname == MCAST_JOIN_SOURCE_GROUP) { 1058 struct ip_mreqn mreq; 1059 1060 psin = (struct sockaddr_in *)&greqs.gsr_group; 1061 mreq.imr_multiaddr = psin->sin_addr; 1062 mreq.imr_address.s_addr = 0; 1063 mreq.imr_ifindex = greqs.gsr_interface; 1064 err = ip_mc_join_group(sk, &mreq); 1065 if (err && err != -EADDRINUSE) 1066 break; 1067 greqs.gsr_interface = mreq.imr_ifindex; 1068 omode = MCAST_INCLUDE; 1069 add = 1; 1070 } else /* MCAST_LEAVE_SOURCE_GROUP */ { 1071 omode = MCAST_INCLUDE; 1072 add = 0; 1073 } 1074 err = ip_mc_source(add, omode, sk, &mreqs, 1075 greqs.gsr_interface); 1076 break; 1077 } 1078 case MCAST_MSFILTER: 1079 { 1080 struct sockaddr_in *psin; 1081 struct ip_msfilter *msf = NULL; 1082 struct group_filter *gsf = NULL; 1083 int msize, i, ifindex; 1084 1085 if (optlen < GROUP_FILTER_SIZE(0)) 1086 goto e_inval; 1087 if (optlen > sysctl_optmem_max) { 1088 err = -ENOBUFS; 1089 break; 1090 } 1091 gsf = memdup_user(optval, optlen); 1092 if (IS_ERR(gsf)) { 1093 err = PTR_ERR(gsf); 1094 break; 1095 } 1096 1097 /* numsrc >= (4G-140)/128 overflow in 32 bits */ 1098 if (gsf->gf_numsrc >= 0x1ffffff || 1099 gsf->gf_numsrc > net->ipv4.sysctl_igmp_max_msf) { 1100 err = -ENOBUFS; 1101 goto mc_msf_out; 1102 } 1103 if (GROUP_FILTER_SIZE(gsf->gf_numsrc) > optlen) { 1104 err = -EINVAL; 1105 goto mc_msf_out; 1106 } 1107 msize = IP_MSFILTER_SIZE(gsf->gf_numsrc); 1108 msf = kmalloc(msize, GFP_KERNEL); 1109 if (!msf) { 1110 err = -ENOBUFS; 1111 goto mc_msf_out; 1112 } 1113 ifindex = gsf->gf_interface; 1114 psin = (struct sockaddr_in *)&gsf->gf_group; 1115 if (psin->sin_family != AF_INET) { 1116 err = -EADDRNOTAVAIL; 1117 goto mc_msf_out; 1118 } 1119 msf->imsf_multiaddr = psin->sin_addr.s_addr; 1120 msf->imsf_interface = 0; 1121 msf->imsf_fmode = gsf->gf_fmode; 1122 msf->imsf_numsrc = gsf->gf_numsrc; 1123 err = -EADDRNOTAVAIL; 1124 for (i = 0; i < gsf->gf_numsrc; ++i) { 1125 psin = (struct sockaddr_in *)&gsf->gf_slist[i]; 1126 1127 if (psin->sin_family != AF_INET) 1128 goto mc_msf_out; 1129 msf->imsf_slist[i] = psin->sin_addr.s_addr; 1130 } 1131 kfree(gsf); 1132 gsf = NULL; 1133 1134 err = ip_mc_msfilter(sk, msf, ifindex); 1135 mc_msf_out: 1136 kfree(msf); 1137 kfree(gsf); 1138 break; 1139 } 1140 case IP_MULTICAST_ALL: 1141 if (optlen < 1) 1142 goto e_inval; 1143 if (val != 0 && val != 1) 1144 goto e_inval; 1145 inet->mc_all = val; 1146 break; 1147 1148 case IP_FREEBIND: 1149 if (optlen < 1) 1150 goto e_inval; 1151 inet->freebind = !!val; 1152 break; 1153 1154 case IP_IPSEC_POLICY: 1155 case IP_XFRM_POLICY: 1156 err = -EPERM; 1157 if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN)) 1158 break; 1159 err = xfrm_user_policy(sk, optname, optval, optlen); 1160 break; 1161 1162 case IP_TRANSPARENT: 1163 if (!!val && !ns_capable(sock_net(sk)->user_ns, CAP_NET_RAW) && 1164 !ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN)) { 1165 err = -EPERM; 1166 break; 1167 } 1168 if (optlen < 1) 1169 goto e_inval; 1170 inet->transparent = !!val; 1171 break; 1172 1173 case IP_MINTTL: 1174 if (optlen < 1) 1175 goto e_inval; 1176 if (val < 0 || val > 255) 1177 goto e_inval; 1178 inet->min_ttl = val; 1179 break; 1180 1181 default: 1182 err = -ENOPROTOOPT; 1183 break; 1184 } 1185 release_sock(sk); 1186 if (needs_rtnl) 1187 rtnl_unlock(); 1188 return err; 1189 1190 e_inval: 1191 release_sock(sk); 1192 if (needs_rtnl) 1193 rtnl_unlock(); 1194 return -EINVAL; 1195 } 1196 1197 /** 1198 * ipv4_pktinfo_prepare - transfer some info from rtable to skb 1199 * @sk: socket 1200 * @skb: buffer 1201 * 1202 * To support IP_CMSG_PKTINFO option, we store rt_iif and specific 1203 * destination in skb->cb[] before dst drop. 1204 * This way, receiver doesn't make cache line misses to read rtable. 1205 */ 1206 void ipv4_pktinfo_prepare(const struct sock *sk, struct sk_buff *skb) 1207 { 1208 struct in_pktinfo *pktinfo = PKTINFO_SKB_CB(skb); 1209 bool prepare = (inet_sk(sk)->cmsg_flags & IP_CMSG_PKTINFO) || 1210 ipv6_sk_rxinfo(sk); 1211 1212 if (prepare && skb_rtable(skb)) { 1213 /* skb->cb is overloaded: prior to this point it is IP{6}CB 1214 * which has interface index (iif) as the first member of the 1215 * underlying inet{6}_skb_parm struct. This code then overlays 1216 * PKTINFO_SKB_CB and in_pktinfo also has iif as the first 1217 * element so the iif is picked up from the prior IPCB. If iif 1218 * is the loopback interface, then return the sending interface 1219 * (e.g., process binds socket to eth0 for Tx which is 1220 * redirected to loopback in the rtable/dst). 1221 */ 1222 struct rtable *rt = skb_rtable(skb); 1223 bool l3slave = ipv4_l3mdev_skb(IPCB(skb)->flags); 1224 1225 if (pktinfo->ipi_ifindex == LOOPBACK_IFINDEX) 1226 pktinfo->ipi_ifindex = inet_iif(skb); 1227 else if (l3slave && rt && rt->rt_iif) 1228 pktinfo->ipi_ifindex = rt->rt_iif; 1229 1230 pktinfo->ipi_spec_dst.s_addr = fib_compute_spec_dst(skb); 1231 } else { 1232 pktinfo->ipi_ifindex = 0; 1233 pktinfo->ipi_spec_dst.s_addr = 0; 1234 } 1235 skb_dst_drop(skb); 1236 } 1237 1238 int ip_setsockopt(struct sock *sk, int level, 1239 int optname, char __user *optval, unsigned int optlen) 1240 { 1241 int err; 1242 1243 if (level != SOL_IP) 1244 return -ENOPROTOOPT; 1245 1246 err = do_ip_setsockopt(sk, level, optname, optval, optlen); 1247 #ifdef CONFIG_NETFILTER 1248 /* we need to exclude all possible ENOPROTOOPTs except default case */ 1249 if (err == -ENOPROTOOPT && optname != IP_HDRINCL && 1250 optname != IP_IPSEC_POLICY && 1251 optname != IP_XFRM_POLICY && 1252 !ip_mroute_opt(optname)) 1253 err = nf_setsockopt(sk, PF_INET, optname, optval, optlen); 1254 #endif 1255 return err; 1256 } 1257 EXPORT_SYMBOL(ip_setsockopt); 1258 1259 #ifdef CONFIG_COMPAT 1260 int compat_ip_setsockopt(struct sock *sk, int level, int optname, 1261 char __user *optval, unsigned int optlen) 1262 { 1263 int err; 1264 1265 if (level != SOL_IP) 1266 return -ENOPROTOOPT; 1267 1268 if (optname >= MCAST_JOIN_GROUP && optname <= MCAST_MSFILTER) 1269 return compat_mc_setsockopt(sk, level, optname, optval, optlen, 1270 ip_setsockopt); 1271 1272 err = do_ip_setsockopt(sk, level, optname, optval, optlen); 1273 #ifdef CONFIG_NETFILTER 1274 /* we need to exclude all possible ENOPROTOOPTs except default case */ 1275 if (err == -ENOPROTOOPT && optname != IP_HDRINCL && 1276 optname != IP_IPSEC_POLICY && 1277 optname != IP_XFRM_POLICY && 1278 !ip_mroute_opt(optname)) 1279 err = compat_nf_setsockopt(sk, PF_INET, optname, optval, 1280 optlen); 1281 #endif 1282 return err; 1283 } 1284 EXPORT_SYMBOL(compat_ip_setsockopt); 1285 #endif 1286 1287 /* 1288 * Get the options. Note for future reference. The GET of IP options gets 1289 * the _received_ ones. The set sets the _sent_ ones. 1290 */ 1291 1292 static bool getsockopt_needs_rtnl(int optname) 1293 { 1294 switch (optname) { 1295 case IP_MSFILTER: 1296 case MCAST_MSFILTER: 1297 return true; 1298 } 1299 return false; 1300 } 1301 1302 static int do_ip_getsockopt(struct sock *sk, int level, int optname, 1303 char __user *optval, int __user *optlen, unsigned int flags) 1304 { 1305 struct inet_sock *inet = inet_sk(sk); 1306 bool needs_rtnl = getsockopt_needs_rtnl(optname); 1307 int val, err = 0; 1308 int len; 1309 1310 if (level != SOL_IP) 1311 return -EOPNOTSUPP; 1312 1313 if (ip_mroute_opt(optname)) 1314 return ip_mroute_getsockopt(sk, optname, optval, optlen); 1315 1316 if (get_user(len, optlen)) 1317 return -EFAULT; 1318 if (len < 0) 1319 return -EINVAL; 1320 1321 if (needs_rtnl) 1322 rtnl_lock(); 1323 lock_sock(sk); 1324 1325 switch (optname) { 1326 case IP_OPTIONS: 1327 { 1328 unsigned char optbuf[sizeof(struct ip_options)+40]; 1329 struct ip_options *opt = (struct ip_options *)optbuf; 1330 struct ip_options_rcu *inet_opt; 1331 1332 inet_opt = rcu_dereference_protected(inet->inet_opt, 1333 lockdep_sock_is_held(sk)); 1334 opt->optlen = 0; 1335 if (inet_opt) 1336 memcpy(optbuf, &inet_opt->opt, 1337 sizeof(struct ip_options) + 1338 inet_opt->opt.optlen); 1339 release_sock(sk); 1340 1341 if (opt->optlen == 0) 1342 return put_user(0, optlen); 1343 1344 ip_options_undo(opt); 1345 1346 len = min_t(unsigned int, len, opt->optlen); 1347 if (put_user(len, optlen)) 1348 return -EFAULT; 1349 if (copy_to_user(optval, opt->__data, len)) 1350 return -EFAULT; 1351 return 0; 1352 } 1353 case IP_PKTINFO: 1354 val = (inet->cmsg_flags & IP_CMSG_PKTINFO) != 0; 1355 break; 1356 case IP_RECVTTL: 1357 val = (inet->cmsg_flags & IP_CMSG_TTL) != 0; 1358 break; 1359 case IP_RECVTOS: 1360 val = (inet->cmsg_flags & IP_CMSG_TOS) != 0; 1361 break; 1362 case IP_RECVOPTS: 1363 val = (inet->cmsg_flags & IP_CMSG_RECVOPTS) != 0; 1364 break; 1365 case IP_RETOPTS: 1366 val = (inet->cmsg_flags & IP_CMSG_RETOPTS) != 0; 1367 break; 1368 case IP_PASSSEC: 1369 val = (inet->cmsg_flags & IP_CMSG_PASSSEC) != 0; 1370 break; 1371 case IP_RECVORIGDSTADDR: 1372 val = (inet->cmsg_flags & IP_CMSG_ORIGDSTADDR) != 0; 1373 break; 1374 case IP_CHECKSUM: 1375 val = (inet->cmsg_flags & IP_CMSG_CHECKSUM) != 0; 1376 break; 1377 case IP_RECVFRAGSIZE: 1378 val = (inet->cmsg_flags & IP_CMSG_RECVFRAGSIZE) != 0; 1379 break; 1380 case IP_TOS: 1381 val = inet->tos; 1382 break; 1383 case IP_TTL: 1384 { 1385 struct net *net = sock_net(sk); 1386 val = (inet->uc_ttl == -1 ? 1387 net->ipv4.sysctl_ip_default_ttl : 1388 inet->uc_ttl); 1389 break; 1390 } 1391 case IP_HDRINCL: 1392 val = inet->hdrincl; 1393 break; 1394 case IP_NODEFRAG: 1395 val = inet->nodefrag; 1396 break; 1397 case IP_BIND_ADDRESS_NO_PORT: 1398 val = inet->bind_address_no_port; 1399 break; 1400 case IP_MTU_DISCOVER: 1401 val = inet->pmtudisc; 1402 break; 1403 case IP_MTU: 1404 { 1405 struct dst_entry *dst; 1406 val = 0; 1407 dst = sk_dst_get(sk); 1408 if (dst) { 1409 val = dst_mtu(dst); 1410 dst_release(dst); 1411 } 1412 if (!val) { 1413 release_sock(sk); 1414 return -ENOTCONN; 1415 } 1416 break; 1417 } 1418 case IP_RECVERR: 1419 val = inet->recverr; 1420 break; 1421 case IP_MULTICAST_TTL: 1422 val = inet->mc_ttl; 1423 break; 1424 case IP_MULTICAST_LOOP: 1425 val = inet->mc_loop; 1426 break; 1427 case IP_UNICAST_IF: 1428 val = (__force int)htonl((__u32) inet->uc_index); 1429 break; 1430 case IP_MULTICAST_IF: 1431 { 1432 struct in_addr addr; 1433 len = min_t(unsigned int, len, sizeof(struct in_addr)); 1434 addr.s_addr = inet->mc_addr; 1435 release_sock(sk); 1436 1437 if (put_user(len, optlen)) 1438 return -EFAULT; 1439 if (copy_to_user(optval, &addr, len)) 1440 return -EFAULT; 1441 return 0; 1442 } 1443 case IP_MSFILTER: 1444 { 1445 struct ip_msfilter msf; 1446 1447 if (len < IP_MSFILTER_SIZE(0)) { 1448 err = -EINVAL; 1449 goto out; 1450 } 1451 if (copy_from_user(&msf, optval, IP_MSFILTER_SIZE(0))) { 1452 err = -EFAULT; 1453 goto out; 1454 } 1455 err = ip_mc_msfget(sk, &msf, 1456 (struct ip_msfilter __user *)optval, optlen); 1457 goto out; 1458 } 1459 case MCAST_MSFILTER: 1460 { 1461 struct group_filter gsf; 1462 1463 if (len < GROUP_FILTER_SIZE(0)) { 1464 err = -EINVAL; 1465 goto out; 1466 } 1467 if (copy_from_user(&gsf, optval, GROUP_FILTER_SIZE(0))) { 1468 err = -EFAULT; 1469 goto out; 1470 } 1471 err = ip_mc_gsfget(sk, &gsf, 1472 (struct group_filter __user *)optval, 1473 optlen); 1474 goto out; 1475 } 1476 case IP_MULTICAST_ALL: 1477 val = inet->mc_all; 1478 break; 1479 case IP_PKTOPTIONS: 1480 { 1481 struct msghdr msg; 1482 1483 release_sock(sk); 1484 1485 if (sk->sk_type != SOCK_STREAM) 1486 return -ENOPROTOOPT; 1487 1488 msg.msg_control = (__force void *) optval; 1489 msg.msg_controllen = len; 1490 msg.msg_flags = flags; 1491 1492 if (inet->cmsg_flags & IP_CMSG_PKTINFO) { 1493 struct in_pktinfo info; 1494 1495 info.ipi_addr.s_addr = inet->inet_rcv_saddr; 1496 info.ipi_spec_dst.s_addr = inet->inet_rcv_saddr; 1497 info.ipi_ifindex = inet->mc_index; 1498 put_cmsg(&msg, SOL_IP, IP_PKTINFO, sizeof(info), &info); 1499 } 1500 if (inet->cmsg_flags & IP_CMSG_TTL) { 1501 int hlim = inet->mc_ttl; 1502 put_cmsg(&msg, SOL_IP, IP_TTL, sizeof(hlim), &hlim); 1503 } 1504 if (inet->cmsg_flags & IP_CMSG_TOS) { 1505 int tos = inet->rcv_tos; 1506 put_cmsg(&msg, SOL_IP, IP_TOS, sizeof(tos), &tos); 1507 } 1508 len -= msg.msg_controllen; 1509 return put_user(len, optlen); 1510 } 1511 case IP_FREEBIND: 1512 val = inet->freebind; 1513 break; 1514 case IP_TRANSPARENT: 1515 val = inet->transparent; 1516 break; 1517 case IP_MINTTL: 1518 val = inet->min_ttl; 1519 break; 1520 default: 1521 release_sock(sk); 1522 return -ENOPROTOOPT; 1523 } 1524 release_sock(sk); 1525 1526 if (len < sizeof(int) && len > 0 && val >= 0 && val <= 255) { 1527 unsigned char ucval = (unsigned char)val; 1528 len = 1; 1529 if (put_user(len, optlen)) 1530 return -EFAULT; 1531 if (copy_to_user(optval, &ucval, 1)) 1532 return -EFAULT; 1533 } else { 1534 len = min_t(unsigned int, sizeof(int), len); 1535 if (put_user(len, optlen)) 1536 return -EFAULT; 1537 if (copy_to_user(optval, &val, len)) 1538 return -EFAULT; 1539 } 1540 return 0; 1541 1542 out: 1543 release_sock(sk); 1544 if (needs_rtnl) 1545 rtnl_unlock(); 1546 return err; 1547 } 1548 1549 int ip_getsockopt(struct sock *sk, int level, 1550 int optname, char __user *optval, int __user *optlen) 1551 { 1552 int err; 1553 1554 err = do_ip_getsockopt(sk, level, optname, optval, optlen, 0); 1555 #ifdef CONFIG_NETFILTER 1556 /* we need to exclude all possible ENOPROTOOPTs except default case */ 1557 if (err == -ENOPROTOOPT && optname != IP_PKTOPTIONS && 1558 !ip_mroute_opt(optname)) { 1559 int len; 1560 1561 if (get_user(len, optlen)) 1562 return -EFAULT; 1563 1564 err = nf_getsockopt(sk, PF_INET, optname, optval, &len); 1565 if (err >= 0) 1566 err = put_user(len, optlen); 1567 return err; 1568 } 1569 #endif 1570 return err; 1571 } 1572 EXPORT_SYMBOL(ip_getsockopt); 1573 1574 #ifdef CONFIG_COMPAT 1575 int compat_ip_getsockopt(struct sock *sk, int level, int optname, 1576 char __user *optval, int __user *optlen) 1577 { 1578 int err; 1579 1580 if (optname == MCAST_MSFILTER) 1581 return compat_mc_getsockopt(sk, level, optname, optval, optlen, 1582 ip_getsockopt); 1583 1584 err = do_ip_getsockopt(sk, level, optname, optval, optlen, 1585 MSG_CMSG_COMPAT); 1586 1587 #ifdef CONFIG_NETFILTER 1588 /* we need to exclude all possible ENOPROTOOPTs except default case */ 1589 if (err == -ENOPROTOOPT && optname != IP_PKTOPTIONS && 1590 !ip_mroute_opt(optname)) { 1591 int len; 1592 1593 if (get_user(len, optlen)) 1594 return -EFAULT; 1595 1596 err = compat_nf_getsockopt(sk, PF_INET, optname, optval, &len); 1597 if (err >= 0) 1598 err = put_user(len, optlen); 1599 return err; 1600 } 1601 #endif 1602 return err; 1603 } 1604 EXPORT_SYMBOL(compat_ip_getsockopt); 1605 #endif 1606