1 // SPDX-License-Identifier: GPL-2.0
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.
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
16 * Mike McLagan : Routing by source
19 #include <linux/module.h>
20 #include <linux/types.h>
22 #include <linux/skbuff.h>
24 #include <linux/icmp.h>
25 #include <linux/inetdevice.h>
26 #include <linux/netdevice.h>
27 #include <linux/slab.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>
40 #include <net/compat.h>
41 #include <net/checksum.h>
42 #if IS_ENABLED(CONFIG_IPV6)
43 #include <net/transp_v6.h>
45 #include <net/ip_fib.h>
47 #include <linux/errqueue.h>
48 #include <linux/uaccess.h>
50 #include <linux/bpfilter.h>
53 * SOL_IP control messages.
56 static void ip_cmsg_recv_pktinfo(struct msghdr *msg, struct sk_buff *skb)
58 struct in_pktinfo info = *PKTINFO_SKB_CB(skb);
60 info.ipi_addr.s_addr = ip_hdr(skb)->daddr;
62 put_cmsg(msg, SOL_IP, IP_PKTINFO, sizeof(info), &info);
65 static void ip_cmsg_recv_ttl(struct msghdr *msg, struct sk_buff *skb)
67 int ttl = ip_hdr(skb)->ttl;
68 put_cmsg(msg, SOL_IP, IP_TTL, sizeof(int), &ttl);
71 static void ip_cmsg_recv_tos(struct msghdr *msg, struct sk_buff *skb)
73 put_cmsg(msg, SOL_IP, IP_TOS, 1, &ip_hdr(skb)->tos);
76 static void ip_cmsg_recv_opts(struct msghdr *msg, struct sk_buff *skb)
78 if (IPCB(skb)->opt.optlen == 0)
81 put_cmsg(msg, SOL_IP, IP_RECVOPTS, IPCB(skb)->opt.optlen,
86 static void ip_cmsg_recv_retopts(struct net *net, struct msghdr *msg,
89 unsigned char optbuf[sizeof(struct ip_options) + 40];
90 struct ip_options *opt = (struct ip_options *)optbuf;
92 if (IPCB(skb)->opt.optlen == 0)
95 if (ip_options_echo(net, opt, skb)) {
96 msg->msg_flags |= MSG_CTRUNC;
101 put_cmsg(msg, SOL_IP, IP_RETOPTS, opt->optlen, opt->__data);
104 static void ip_cmsg_recv_fragsize(struct msghdr *msg, struct sk_buff *skb)
108 if (IPCB(skb)->frag_max_size == 0)
111 val = IPCB(skb)->frag_max_size;
112 put_cmsg(msg, SOL_IP, IP_RECVFRAGSIZE, sizeof(val), &val);
115 static void ip_cmsg_recv_checksum(struct msghdr *msg, struct sk_buff *skb,
116 int tlen, int offset)
118 __wsum csum = skb->csum;
120 if (skb->ip_summed != CHECKSUM_COMPLETE)
124 int tend_off = skb_transport_offset(skb) + tlen;
125 csum = csum_sub(csum, skb_checksum(skb, tend_off, offset, 0));
128 put_cmsg(msg, SOL_IP, IP_CHECKSUM, sizeof(__wsum), &csum);
131 static void ip_cmsg_recv_security(struct msghdr *msg, struct sk_buff *skb)
137 err = security_socket_getpeersec_dgram(NULL, skb, &secid);
141 err = security_secid_to_secctx(secid, &secdata, &seclen);
145 put_cmsg(msg, SOL_IP, SCM_SECURITY, seclen, secdata);
146 security_release_secctx(secdata, seclen);
149 static void ip_cmsg_recv_dstaddr(struct msghdr *msg, struct sk_buff *skb)
151 __be16 _ports[2], *ports;
152 struct sockaddr_in sin;
154 /* All current transport protocols have the port numbers in the
155 * first four bytes of the transport header and this function is
156 * written with this assumption in mind.
158 ports = skb_header_pointer(skb, skb_transport_offset(skb),
159 sizeof(_ports), &_ports);
163 sin.sin_family = AF_INET;
164 sin.sin_addr.s_addr = ip_hdr(skb)->daddr;
165 sin.sin_port = ports[1];
166 memset(sin.sin_zero, 0, sizeof(sin.sin_zero));
168 put_cmsg(msg, SOL_IP, IP_ORIGDSTADDR, sizeof(sin), &sin);
171 void ip_cmsg_recv_offset(struct msghdr *msg, struct sock *sk,
172 struct sk_buff *skb, int tlen, int offset)
174 unsigned long flags = inet_cmsg_flags(inet_sk(sk));
179 /* Ordered by supposed usage frequency */
180 if (flags & IP_CMSG_PKTINFO) {
181 ip_cmsg_recv_pktinfo(msg, skb);
183 flags &= ~IP_CMSG_PKTINFO;
188 if (flags & IP_CMSG_TTL) {
189 ip_cmsg_recv_ttl(msg, skb);
191 flags &= ~IP_CMSG_TTL;
196 if (flags & IP_CMSG_TOS) {
197 ip_cmsg_recv_tos(msg, skb);
199 flags &= ~IP_CMSG_TOS;
204 if (flags & IP_CMSG_RECVOPTS) {
205 ip_cmsg_recv_opts(msg, skb);
207 flags &= ~IP_CMSG_RECVOPTS;
212 if (flags & IP_CMSG_RETOPTS) {
213 ip_cmsg_recv_retopts(sock_net(sk), msg, skb);
215 flags &= ~IP_CMSG_RETOPTS;
220 if (flags & IP_CMSG_PASSSEC) {
221 ip_cmsg_recv_security(msg, skb);
223 flags &= ~IP_CMSG_PASSSEC;
228 if (flags & IP_CMSG_ORIGDSTADDR) {
229 ip_cmsg_recv_dstaddr(msg, skb);
231 flags &= ~IP_CMSG_ORIGDSTADDR;
236 if (flags & IP_CMSG_CHECKSUM)
237 ip_cmsg_recv_checksum(msg, skb, tlen, offset);
239 if (flags & IP_CMSG_RECVFRAGSIZE)
240 ip_cmsg_recv_fragsize(msg, skb);
242 EXPORT_SYMBOL(ip_cmsg_recv_offset);
244 int ip_cmsg_send(struct sock *sk, struct msghdr *msg, struct ipcm_cookie *ipc,
248 struct cmsghdr *cmsg;
249 struct net *net = sock_net(sk);
251 for_each_cmsghdr(cmsg, msg) {
252 if (!CMSG_OK(msg, cmsg))
254 #if IS_ENABLED(CONFIG_IPV6)
256 cmsg->cmsg_level == SOL_IPV6 &&
257 cmsg->cmsg_type == IPV6_PKTINFO) {
258 struct in6_pktinfo *src_info;
260 if (cmsg->cmsg_len < CMSG_LEN(sizeof(*src_info)))
262 src_info = (struct in6_pktinfo *)CMSG_DATA(cmsg);
263 if (!ipv6_addr_v4mapped(&src_info->ipi6_addr))
265 if (src_info->ipi6_ifindex)
266 ipc->oif = src_info->ipi6_ifindex;
267 ipc->addr = src_info->ipi6_addr.s6_addr32[3];
271 if (cmsg->cmsg_level == SOL_SOCKET) {
272 err = __sock_cmsg_send(sk, cmsg, &ipc->sockc);
278 if (cmsg->cmsg_level != SOL_IP)
280 switch (cmsg->cmsg_type) {
282 err = cmsg->cmsg_len - sizeof(struct cmsghdr);
284 /* Our caller is responsible for freeing ipc->opt */
285 err = ip_options_get(net, &ipc->opt,
286 KERNEL_SOCKPTR(CMSG_DATA(cmsg)),
287 err < 40 ? err : 40);
293 struct in_pktinfo *info;
294 if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct in_pktinfo)))
296 info = (struct in_pktinfo *)CMSG_DATA(cmsg);
297 if (info->ipi_ifindex)
298 ipc->oif = info->ipi_ifindex;
299 ipc->addr = info->ipi_spec_dst.s_addr;
303 if (cmsg->cmsg_len != CMSG_LEN(sizeof(int)))
305 val = *(int *)CMSG_DATA(cmsg);
306 if (val < 1 || val > 255)
311 if (cmsg->cmsg_len == CMSG_LEN(sizeof(int)))
312 val = *(int *)CMSG_DATA(cmsg);
313 else if (cmsg->cmsg_len == CMSG_LEN(sizeof(u8)))
314 val = *(u8 *)CMSG_DATA(cmsg);
317 if (val < 0 || val > 255)
320 ipc->priority = rt_tos2priority(ipc->tos);
323 if (cmsg->cmsg_len != CMSG_LEN(sizeof(int)))
325 val = *(int *)CMSG_DATA(cmsg);
326 if (val < 1 || val > 255)
337 static void ip_ra_destroy_rcu(struct rcu_head *head)
339 struct ip_ra_chain *ra = container_of(head, struct ip_ra_chain, rcu);
341 sock_put(ra->saved_sk);
345 int ip_ra_control(struct sock *sk, unsigned char on,
346 void (*destructor)(struct sock *))
348 struct ip_ra_chain *ra, *new_ra;
349 struct ip_ra_chain __rcu **rap;
350 struct net *net = sock_net(sk);
352 if (sk->sk_type != SOCK_RAW || inet_sk(sk)->inet_num == IPPROTO_RAW)
355 new_ra = on ? kmalloc(sizeof(*new_ra), GFP_KERNEL) : NULL;
359 mutex_lock(&net->ipv4.ra_mutex);
360 for (rap = &net->ipv4.ra_chain;
361 (ra = rcu_dereference_protected(*rap,
362 lockdep_is_held(&net->ipv4.ra_mutex))) != NULL;
366 mutex_unlock(&net->ipv4.ra_mutex);
370 /* dont let ip_call_ra_chain() use sk again */
372 RCU_INIT_POINTER(*rap, ra->next);
373 mutex_unlock(&net->ipv4.ra_mutex);
378 * Delay sock_put(sk) and kfree(ra) after one rcu grace
379 * period. This guarantee ip_call_ra_chain() dont need
380 * to mess with socket refcounts.
383 call_rcu(&ra->rcu, ip_ra_destroy_rcu);
388 mutex_unlock(&net->ipv4.ra_mutex);
392 new_ra->destructor = destructor;
394 RCU_INIT_POINTER(new_ra->next, ra);
395 rcu_assign_pointer(*rap, new_ra);
397 mutex_unlock(&net->ipv4.ra_mutex);
402 static void ipv4_icmp_error_rfc4884(const struct sk_buff *skb,
403 struct sock_ee_data_rfc4884 *out)
405 switch (icmp_hdr(skb)->type) {
406 case ICMP_DEST_UNREACH:
407 case ICMP_TIME_EXCEEDED:
408 case ICMP_PARAMETERPROB:
409 ip_icmp_error_rfc4884(skb, out, sizeof(struct icmphdr),
410 icmp_hdr(skb)->un.reserved[1] * 4);
414 void ip_icmp_error(struct sock *sk, struct sk_buff *skb, int err,
415 __be16 port, u32 info, u8 *payload)
417 struct sock_exterr_skb *serr;
419 skb = skb_clone(skb, GFP_ATOMIC);
423 serr = SKB_EXT_ERR(skb);
424 serr->ee.ee_errno = err;
425 serr->ee.ee_origin = SO_EE_ORIGIN_ICMP;
426 serr->ee.ee_type = icmp_hdr(skb)->type;
427 serr->ee.ee_code = icmp_hdr(skb)->code;
429 serr->ee.ee_info = info;
430 serr->ee.ee_data = 0;
431 serr->addr_offset = (u8 *)&(((struct iphdr *)(icmp_hdr(skb) + 1))->daddr) -
432 skb_network_header(skb);
435 if (skb_pull(skb, payload - skb->data)) {
436 if (inet_test_bit(RECVERR_RFC4884, sk))
437 ipv4_icmp_error_rfc4884(skb, &serr->ee.ee_rfc4884);
439 skb_reset_transport_header(skb);
440 if (sock_queue_err_skb(sk, skb) == 0)
445 EXPORT_SYMBOL_GPL(ip_icmp_error);
447 void ip_local_error(struct sock *sk, int err, __be32 daddr, __be16 port, u32 info)
449 struct sock_exterr_skb *serr;
453 if (!inet_test_bit(RECVERR, sk))
456 skb = alloc_skb(sizeof(struct iphdr), GFP_ATOMIC);
460 skb_put(skb, sizeof(struct iphdr));
461 skb_reset_network_header(skb);
465 serr = SKB_EXT_ERR(skb);
466 serr->ee.ee_errno = err;
467 serr->ee.ee_origin = SO_EE_ORIGIN_LOCAL;
468 serr->ee.ee_type = 0;
469 serr->ee.ee_code = 0;
471 serr->ee.ee_info = info;
472 serr->ee.ee_data = 0;
473 serr->addr_offset = (u8 *)&iph->daddr - skb_network_header(skb);
476 __skb_pull(skb, skb_tail_pointer(skb) - skb->data);
477 skb_reset_transport_header(skb);
479 if (sock_queue_err_skb(sk, skb))
483 /* For some errors we have valid addr_offset even with zero payload and
484 * zero port. Also, addr_offset should be supported if port is set.
486 static inline bool ipv4_datagram_support_addr(struct sock_exterr_skb *serr)
488 return serr->ee.ee_origin == SO_EE_ORIGIN_ICMP ||
489 serr->ee.ee_origin == SO_EE_ORIGIN_LOCAL || serr->port;
492 /* IPv4 supports cmsg on all imcp errors and some timestamps
494 * Timestamp code paths do not initialize the fields expected by cmsg:
495 * the PKTINFO fields in skb->cb[]. Fill those in here.
497 static bool ipv4_datagram_support_cmsg(const struct sock *sk,
501 struct in_pktinfo *info;
503 if (ee_origin == SO_EE_ORIGIN_ICMP)
506 if (ee_origin == SO_EE_ORIGIN_LOCAL)
509 /* Support IP_PKTINFO on tstamp packets if requested, to correlate
510 * timestamp with egress dev. Not possible for packets without iif
511 * or without payload (SOF_TIMESTAMPING_OPT_TSONLY).
513 info = PKTINFO_SKB_CB(skb);
514 if (!(sk->sk_tsflags & SOF_TIMESTAMPING_OPT_CMSG) ||
518 info->ipi_spec_dst.s_addr = ip_hdr(skb)->saddr;
523 * Handle MSG_ERRQUEUE
525 int ip_recv_error(struct sock *sk, struct msghdr *msg, int len, int *addr_len)
527 struct sock_exterr_skb *serr;
529 DECLARE_SOCKADDR(struct sockaddr_in *, sin, msg->msg_name);
531 struct sock_extended_err ee;
532 struct sockaddr_in offender;
538 skb = sock_dequeue_err_skb(sk);
544 msg->msg_flags |= MSG_TRUNC;
547 err = skb_copy_datagram_msg(skb, 0, msg, copied);
552 sock_recv_timestamp(msg, sk, skb);
554 serr = SKB_EXT_ERR(skb);
556 if (sin && ipv4_datagram_support_addr(serr)) {
557 sin->sin_family = AF_INET;
558 sin->sin_addr.s_addr = *(__be32 *)(skb_network_header(skb) +
560 sin->sin_port = serr->port;
561 memset(&sin->sin_zero, 0, sizeof(sin->sin_zero));
562 *addr_len = sizeof(*sin);
565 memcpy(&errhdr.ee, &serr->ee, sizeof(struct sock_extended_err));
566 sin = &errhdr.offender;
567 memset(sin, 0, sizeof(*sin));
569 if (ipv4_datagram_support_cmsg(sk, skb, serr->ee.ee_origin)) {
570 sin->sin_family = AF_INET;
571 sin->sin_addr.s_addr = ip_hdr(skb)->saddr;
572 if (inet_cmsg_flags(inet_sk(sk)))
573 ip_cmsg_recv(msg, skb);
576 put_cmsg(msg, SOL_IP, IP_RECVERR, sizeof(errhdr), &errhdr);
578 /* Now we could try to dump offended packet options */
580 msg->msg_flags |= MSG_ERRQUEUE;
588 void __ip_sock_set_tos(struct sock *sk, int val)
590 if (sk->sk_type == SOCK_STREAM) {
591 val &= ~INET_ECN_MASK;
592 val |= inet_sk(sk)->tos & INET_ECN_MASK;
594 if (inet_sk(sk)->tos != val) {
595 inet_sk(sk)->tos = val;
596 WRITE_ONCE(sk->sk_priority, rt_tos2priority(val));
601 void ip_sock_set_tos(struct sock *sk, int val)
604 __ip_sock_set_tos(sk, val);
607 EXPORT_SYMBOL(ip_sock_set_tos);
609 void ip_sock_set_freebind(struct sock *sk)
611 inet_set_bit(FREEBIND, sk);
613 EXPORT_SYMBOL(ip_sock_set_freebind);
615 void ip_sock_set_recverr(struct sock *sk)
617 inet_set_bit(RECVERR, sk);
619 EXPORT_SYMBOL(ip_sock_set_recverr);
621 int ip_sock_set_mtu_discover(struct sock *sk, int val)
623 if (val < IP_PMTUDISC_DONT || val > IP_PMTUDISC_OMIT)
626 inet_sk(sk)->pmtudisc = val;
630 EXPORT_SYMBOL(ip_sock_set_mtu_discover);
632 void ip_sock_set_pktinfo(struct sock *sk)
634 inet_set_bit(PKTINFO, sk);
636 EXPORT_SYMBOL(ip_sock_set_pktinfo);
639 * Socket option code for IP. This is the end of the line after any
640 * TCP,UDP etc options on an IP socket.
642 static bool setsockopt_needs_rtnl(int optname)
645 case IP_ADD_MEMBERSHIP:
646 case IP_ADD_SOURCE_MEMBERSHIP:
647 case IP_BLOCK_SOURCE:
648 case IP_DROP_MEMBERSHIP:
649 case IP_DROP_SOURCE_MEMBERSHIP:
651 case IP_UNBLOCK_SOURCE:
652 case MCAST_BLOCK_SOURCE:
654 case MCAST_JOIN_GROUP:
655 case MCAST_JOIN_SOURCE_GROUP:
656 case MCAST_LEAVE_GROUP:
657 case MCAST_LEAVE_SOURCE_GROUP:
658 case MCAST_UNBLOCK_SOURCE:
664 static int set_mcast_msfilter(struct sock *sk, int ifindex,
665 int numsrc, int fmode,
666 struct sockaddr_storage *group,
667 struct sockaddr_storage *list)
669 struct ip_msfilter *msf;
670 struct sockaddr_in *psin;
673 msf = kmalloc(IP_MSFILTER_SIZE(numsrc), GFP_KERNEL);
677 psin = (struct sockaddr_in *)group;
678 if (psin->sin_family != AF_INET)
680 msf->imsf_multiaddr = psin->sin_addr.s_addr;
681 msf->imsf_interface = 0;
682 msf->imsf_fmode = fmode;
683 msf->imsf_numsrc = numsrc;
684 for (i = 0; i < numsrc; ++i) {
685 psin = (struct sockaddr_in *)&list[i];
687 if (psin->sin_family != AF_INET)
689 msf->imsf_slist_flex[i] = psin->sin_addr.s_addr;
691 err = ip_mc_msfilter(sk, msf, ifindex);
697 return -EADDRNOTAVAIL;
700 static int copy_group_source_from_sockptr(struct group_source_req *greqs,
701 sockptr_t optval, int optlen)
703 if (in_compat_syscall()) {
704 struct compat_group_source_req gr32;
706 if (optlen != sizeof(gr32))
708 if (copy_from_sockptr(&gr32, optval, sizeof(gr32)))
710 greqs->gsr_interface = gr32.gsr_interface;
711 greqs->gsr_group = gr32.gsr_group;
712 greqs->gsr_source = gr32.gsr_source;
714 if (optlen != sizeof(*greqs))
716 if (copy_from_sockptr(greqs, optval, sizeof(*greqs)))
723 static int do_mcast_group_source(struct sock *sk, int optname,
724 sockptr_t optval, int optlen)
726 struct group_source_req greqs;
727 struct ip_mreq_source mreqs;
728 struct sockaddr_in *psin;
731 err = copy_group_source_from_sockptr(&greqs, optval, optlen);
735 if (greqs.gsr_group.ss_family != AF_INET ||
736 greqs.gsr_source.ss_family != AF_INET)
737 return -EADDRNOTAVAIL;
739 psin = (struct sockaddr_in *)&greqs.gsr_group;
740 mreqs.imr_multiaddr = psin->sin_addr.s_addr;
741 psin = (struct sockaddr_in *)&greqs.gsr_source;
742 mreqs.imr_sourceaddr = psin->sin_addr.s_addr;
743 mreqs.imr_interface = 0; /* use index for mc_source */
745 if (optname == MCAST_BLOCK_SOURCE) {
746 omode = MCAST_EXCLUDE;
748 } else if (optname == MCAST_UNBLOCK_SOURCE) {
749 omode = MCAST_EXCLUDE;
751 } else if (optname == MCAST_JOIN_SOURCE_GROUP) {
752 struct ip_mreqn mreq;
754 psin = (struct sockaddr_in *)&greqs.gsr_group;
755 mreq.imr_multiaddr = psin->sin_addr;
756 mreq.imr_address.s_addr = 0;
757 mreq.imr_ifindex = greqs.gsr_interface;
758 err = ip_mc_join_group_ssm(sk, &mreq, MCAST_INCLUDE);
759 if (err && err != -EADDRINUSE)
761 greqs.gsr_interface = mreq.imr_ifindex;
762 omode = MCAST_INCLUDE;
764 } else /* MCAST_LEAVE_SOURCE_GROUP */ {
765 omode = MCAST_INCLUDE;
768 return ip_mc_source(add, omode, sk, &mreqs, greqs.gsr_interface);
771 static int ip_set_mcast_msfilter(struct sock *sk, sockptr_t optval, int optlen)
773 struct group_filter *gsf = NULL;
776 if (optlen < GROUP_FILTER_SIZE(0))
778 if (optlen > READ_ONCE(sysctl_optmem_max))
781 gsf = memdup_sockptr(optval, optlen);
785 /* numsrc >= (4G-140)/128 overflow in 32 bits */
787 if (gsf->gf_numsrc >= 0x1ffffff ||
788 gsf->gf_numsrc > READ_ONCE(sock_net(sk)->ipv4.sysctl_igmp_max_msf))
792 if (GROUP_FILTER_SIZE(gsf->gf_numsrc) > optlen)
795 err = set_mcast_msfilter(sk, gsf->gf_interface, gsf->gf_numsrc,
796 gsf->gf_fmode, &gsf->gf_group,
803 static int compat_ip_set_mcast_msfilter(struct sock *sk, sockptr_t optval,
806 const int size0 = offsetof(struct compat_group_filter, gf_slist_flex);
807 struct compat_group_filter *gf32;
814 if (optlen > READ_ONCE(sysctl_optmem_max) - 4)
817 p = kmalloc(optlen + 4, GFP_KERNEL);
820 gf32 = p + 4; /* we want ->gf_group and ->gf_slist_flex aligned */
823 if (copy_from_sockptr(gf32, optval, optlen))
826 /* numsrc >= (4G-140)/128 overflow in 32 bits */
833 if (offsetof(struct compat_group_filter, gf_slist_flex[n]) > optlen)
836 /* numsrc >= (4G-140)/128 overflow in 32 bits */
838 if (n > READ_ONCE(sock_net(sk)->ipv4.sysctl_igmp_max_msf))
840 err = set_mcast_msfilter(sk, gf32->gf_interface, n, gf32->gf_fmode,
841 &gf32->gf_group, gf32->gf_slist_flex);
847 static int ip_mcast_join_leave(struct sock *sk, int optname,
848 sockptr_t optval, int optlen)
850 struct ip_mreqn mreq = { };
851 struct sockaddr_in *psin;
852 struct group_req greq;
854 if (optlen < sizeof(struct group_req))
856 if (copy_from_sockptr(&greq, optval, sizeof(greq)))
859 psin = (struct sockaddr_in *)&greq.gr_group;
860 if (psin->sin_family != AF_INET)
862 mreq.imr_multiaddr = psin->sin_addr;
863 mreq.imr_ifindex = greq.gr_interface;
864 if (optname == MCAST_JOIN_GROUP)
865 return ip_mc_join_group(sk, &mreq);
866 return ip_mc_leave_group(sk, &mreq);
869 static int compat_ip_mcast_join_leave(struct sock *sk, int optname,
870 sockptr_t optval, int optlen)
872 struct compat_group_req greq;
873 struct ip_mreqn mreq = { };
874 struct sockaddr_in *psin;
876 if (optlen < sizeof(struct compat_group_req))
878 if (copy_from_sockptr(&greq, optval, sizeof(greq)))
881 psin = (struct sockaddr_in *)&greq.gr_group;
882 if (psin->sin_family != AF_INET)
884 mreq.imr_multiaddr = psin->sin_addr;
885 mreq.imr_ifindex = greq.gr_interface;
887 if (optname == MCAST_JOIN_GROUP)
888 return ip_mc_join_group(sk, &mreq);
889 return ip_mc_leave_group(sk, &mreq);
892 DEFINE_STATIC_KEY_FALSE(ip4_min_ttl);
894 int do_ip_setsockopt(struct sock *sk, int level, int optname,
895 sockptr_t optval, unsigned int optlen)
897 struct inet_sock *inet = inet_sk(sk);
898 struct net *net = sock_net(sk);
900 bool needs_rtnl = setsockopt_needs_rtnl(optname);
911 case IP_MTU_DISCOVER:
913 case IP_ROUTER_ALERT:
919 case IP_BIND_ADDRESS_NO_PORT:
921 case IP_MULTICAST_TTL:
922 case IP_MULTICAST_ALL:
923 case IP_MULTICAST_LOOP:
924 case IP_RECVORIGDSTADDR:
926 case IP_RECVFRAGSIZE:
927 case IP_RECVERR_RFC4884:
928 case IP_LOCAL_PORT_RANGE:
929 if (optlen >= sizeof(int)) {
930 if (copy_from_sockptr(&val, optval, sizeof(val)))
932 } else if (optlen >= sizeof(char)) {
935 if (copy_from_sockptr(&ucval, optval, sizeof(ucval)))
941 /* If optlen==0, it is equivalent to val == 0 */
943 if (optname == IP_ROUTER_ALERT)
944 return ip_ra_control(sk, val ? 1 : 0, NULL);
945 if (ip_mroute_opt(optname))
946 return ip_mroute_setsockopt(sk, optname, optval, optlen);
948 /* Handle options that can be set without locking the socket. */
951 inet_assign_bit(PKTINFO, sk, val);
954 inet_assign_bit(TTL, sk, val);
957 inet_assign_bit(TOS, sk, val);
960 inet_assign_bit(RECVOPTS, sk, val);
963 inet_assign_bit(RETOPTS, sk, val);
966 inet_assign_bit(PASSSEC, sk, val);
968 case IP_RECVORIGDSTADDR:
969 inet_assign_bit(ORIGDSTADDR, sk, val);
971 case IP_RECVFRAGSIZE:
972 if (sk->sk_type != SOCK_RAW && sk->sk_type != SOCK_DGRAM)
974 inet_assign_bit(RECVFRAGSIZE, sk, val);
977 inet_assign_bit(RECVERR, sk, val);
979 skb_queue_purge(&sk->sk_error_queue);
981 case IP_RECVERR_RFC4884:
982 if (val < 0 || val > 1)
984 inet_assign_bit(RECVERR_RFC4884, sk, val);
989 inet_assign_bit(FREEBIND, sk, val);
992 if (sk->sk_type != SOCK_RAW)
994 inet_assign_bit(HDRINCL, sk, val);
996 case IP_MULTICAST_LOOP:
999 inet_assign_bit(MC_LOOP, sk, val);
1001 case IP_MULTICAST_ALL:
1004 if (val != 0 && val != 1)
1006 inet_assign_bit(MC_ALL, sk, val);
1008 case IP_TRANSPARENT:
1009 if (!!val && !sockopt_ns_capable(sock_net(sk)->user_ns, CAP_NET_RAW) &&
1010 !sockopt_ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN)) {
1016 inet_assign_bit(TRANSPARENT, sk, val);
1019 if (sk->sk_type != SOCK_RAW)
1020 return -ENOPROTOOPT;
1021 inet_assign_bit(NODEFRAG, sk, val);
1028 sockopt_lock_sock(sk);
1033 struct ip_options_rcu *old, *opt = NULL;
1037 err = ip_options_get(sock_net(sk), &opt, optval, optlen);
1040 old = rcu_dereference_protected(inet->inet_opt,
1041 lockdep_sock_is_held(sk));
1042 if (inet_test_bit(IS_ICSK, sk)) {
1043 struct inet_connection_sock *icsk = inet_csk(sk);
1044 #if IS_ENABLED(CONFIG_IPV6)
1045 if (sk->sk_family == PF_INET ||
1046 (!((1 << sk->sk_state) &
1047 (TCPF_LISTEN | TCPF_CLOSE)) &&
1048 inet->inet_daddr != LOOPBACK4_IPV6)) {
1051 icsk->icsk_ext_hdr_len -= old->opt.optlen;
1053 icsk->icsk_ext_hdr_len += opt->opt.optlen;
1054 icsk->icsk_sync_mss(sk, icsk->icsk_pmtu_cookie);
1055 #if IS_ENABLED(CONFIG_IPV6)
1059 rcu_assign_pointer(inet->inet_opt, opt);
1061 kfree_rcu(old, rcu);
1066 if (!(inet_test_bit(CHECKSUM, sk))) {
1067 inet_inc_convert_csum(sk);
1068 inet_set_bit(CHECKSUM, sk);
1071 if (inet_test_bit(CHECKSUM, sk)) {
1072 inet_dec_convert_csum(sk);
1073 inet_clear_bit(CHECKSUM, sk);
1077 case IP_TOS: /* This sets both TOS and Precedence */
1078 __ip_sock_set_tos(sk, val);
1083 if (val != -1 && (val < 1 || val > 255))
1087 case IP_BIND_ADDRESS_NO_PORT:
1088 inet->bind_address_no_port = val ? 1 : 0;
1090 case IP_MTU_DISCOVER:
1091 if (val < IP_PMTUDISC_DONT || val > IP_PMTUDISC_OMIT)
1093 inet->pmtudisc = val;
1095 case IP_MULTICAST_TTL:
1096 if (sk->sk_type == SOCK_STREAM)
1102 if (val < 0 || val > 255)
1108 struct net_device *dev = NULL;
1112 if (optlen != sizeof(int))
1115 ifindex = (__force int)ntohl((__force __be32)val);
1122 dev = dev_get_by_index(sock_net(sk), ifindex);
1123 err = -EADDRNOTAVAIL;
1127 midx = l3mdev_master_ifindex(dev);
1131 if (sk->sk_bound_dev_if && midx != sk->sk_bound_dev_if)
1134 inet->uc_index = ifindex;
1138 case IP_MULTICAST_IF:
1140 struct ip_mreqn mreq;
1141 struct net_device *dev = NULL;
1144 if (sk->sk_type == SOCK_STREAM)
1147 * Check the arguments are allowable
1150 if (optlen < sizeof(struct in_addr))
1154 if (optlen >= sizeof(struct ip_mreqn)) {
1155 if (copy_from_sockptr(&mreq, optval, sizeof(mreq)))
1158 memset(&mreq, 0, sizeof(mreq));
1159 if (optlen >= sizeof(struct ip_mreq)) {
1160 if (copy_from_sockptr(&mreq, optval,
1161 sizeof(struct ip_mreq)))
1163 } else if (optlen >= sizeof(struct in_addr)) {
1164 if (copy_from_sockptr(&mreq.imr_address, optval,
1165 sizeof(struct in_addr)))
1170 if (!mreq.imr_ifindex) {
1171 if (mreq.imr_address.s_addr == htonl(INADDR_ANY)) {
1177 dev = ip_dev_find(sock_net(sk), mreq.imr_address.s_addr);
1179 mreq.imr_ifindex = dev->ifindex;
1181 dev = dev_get_by_index(sock_net(sk), mreq.imr_ifindex);
1184 err = -EADDRNOTAVAIL;
1188 midx = l3mdev_master_ifindex(dev);
1193 if (sk->sk_bound_dev_if &&
1194 mreq.imr_ifindex != sk->sk_bound_dev_if &&
1195 midx != sk->sk_bound_dev_if)
1198 inet->mc_index = mreq.imr_ifindex;
1199 inet->mc_addr = mreq.imr_address.s_addr;
1204 case IP_ADD_MEMBERSHIP:
1205 case IP_DROP_MEMBERSHIP:
1207 struct ip_mreqn mreq;
1210 if (inet_test_bit(IS_ICSK, sk))
1213 if (optlen < sizeof(struct ip_mreq))
1216 if (optlen >= sizeof(struct ip_mreqn)) {
1217 if (copy_from_sockptr(&mreq, optval, sizeof(mreq)))
1220 memset(&mreq, 0, sizeof(mreq));
1221 if (copy_from_sockptr(&mreq, optval,
1222 sizeof(struct ip_mreq)))
1226 if (optname == IP_ADD_MEMBERSHIP)
1227 err = ip_mc_join_group(sk, &mreq);
1229 err = ip_mc_leave_group(sk, &mreq);
1234 struct ip_msfilter *msf;
1236 if (optlen < IP_MSFILTER_SIZE(0))
1238 if (optlen > READ_ONCE(sysctl_optmem_max)) {
1242 msf = memdup_sockptr(optval, optlen);
1247 /* numsrc >= (1G-4) overflow in 32 bits */
1248 if (msf->imsf_numsrc >= 0x3ffffffcU ||
1249 msf->imsf_numsrc > READ_ONCE(net->ipv4.sysctl_igmp_max_msf)) {
1254 if (IP_MSFILTER_SIZE(msf->imsf_numsrc) > optlen) {
1259 err = ip_mc_msfilter(sk, msf, 0);
1263 case IP_BLOCK_SOURCE:
1264 case IP_UNBLOCK_SOURCE:
1265 case IP_ADD_SOURCE_MEMBERSHIP:
1266 case IP_DROP_SOURCE_MEMBERSHIP:
1268 struct ip_mreq_source mreqs;
1271 if (optlen != sizeof(struct ip_mreq_source))
1273 if (copy_from_sockptr(&mreqs, optval, sizeof(mreqs))) {
1277 if (optname == IP_BLOCK_SOURCE) {
1278 omode = MCAST_EXCLUDE;
1280 } else if (optname == IP_UNBLOCK_SOURCE) {
1281 omode = MCAST_EXCLUDE;
1283 } else if (optname == IP_ADD_SOURCE_MEMBERSHIP) {
1284 struct ip_mreqn mreq;
1286 mreq.imr_multiaddr.s_addr = mreqs.imr_multiaddr;
1287 mreq.imr_address.s_addr = mreqs.imr_interface;
1288 mreq.imr_ifindex = 0;
1289 err = ip_mc_join_group_ssm(sk, &mreq, MCAST_INCLUDE);
1290 if (err && err != -EADDRINUSE)
1292 omode = MCAST_INCLUDE;
1294 } else /* IP_DROP_SOURCE_MEMBERSHIP */ {
1295 omode = MCAST_INCLUDE;
1298 err = ip_mc_source(add, omode, sk, &mreqs, 0);
1301 case MCAST_JOIN_GROUP:
1302 case MCAST_LEAVE_GROUP:
1303 if (in_compat_syscall())
1304 err = compat_ip_mcast_join_leave(sk, optname, optval,
1307 err = ip_mcast_join_leave(sk, optname, optval, optlen);
1309 case MCAST_JOIN_SOURCE_GROUP:
1310 case MCAST_LEAVE_SOURCE_GROUP:
1311 case MCAST_BLOCK_SOURCE:
1312 case MCAST_UNBLOCK_SOURCE:
1313 err = do_mcast_group_source(sk, optname, optval, optlen);
1315 case MCAST_MSFILTER:
1316 if (in_compat_syscall())
1317 err = compat_ip_set_mcast_msfilter(sk, optval, optlen);
1319 err = ip_set_mcast_msfilter(sk, optval, optlen);
1321 case IP_IPSEC_POLICY:
1322 case IP_XFRM_POLICY:
1324 if (!sockopt_ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
1326 err = xfrm_user_policy(sk, optname, optval, optlen);
1332 if (val < 0 || val > 255)
1336 static_branch_enable(&ip4_min_ttl);
1338 /* tcp_v4_err() and tcp_v4_rcv() might read min_ttl
1339 * while we are changint it.
1341 WRITE_ONCE(inet->min_ttl, val);
1344 case IP_LOCAL_PORT_RANGE:
1346 const __u16 lo = val;
1347 const __u16 hi = val >> 16;
1349 if (optlen != sizeof(__u32))
1351 if (lo != 0 && hi != 0 && lo > hi)
1354 inet->local_port_range.lo = lo;
1355 inet->local_port_range.hi = hi;
1362 sockopt_release_sock(sk);
1368 sockopt_release_sock(sk);
1375 * ipv4_pktinfo_prepare - transfer some info from rtable to skb
1379 * To support IP_CMSG_PKTINFO option, we store rt_iif and specific
1380 * destination in skb->cb[] before dst drop.
1381 * This way, receiver doesn't make cache line misses to read rtable.
1383 void ipv4_pktinfo_prepare(const struct sock *sk, struct sk_buff *skb)
1385 struct in_pktinfo *pktinfo = PKTINFO_SKB_CB(skb);
1386 bool prepare = inet_test_bit(PKTINFO, sk) ||
1389 if (prepare && skb_rtable(skb)) {
1390 /* skb->cb is overloaded: prior to this point it is IP{6}CB
1391 * which has interface index (iif) as the first member of the
1392 * underlying inet{6}_skb_parm struct. This code then overlays
1393 * PKTINFO_SKB_CB and in_pktinfo also has iif as the first
1394 * element so the iif is picked up from the prior IPCB. If iif
1395 * is the loopback interface, then return the sending interface
1396 * (e.g., process binds socket to eth0 for Tx which is
1397 * redirected to loopback in the rtable/dst).
1399 struct rtable *rt = skb_rtable(skb);
1400 bool l3slave = ipv4_l3mdev_skb(IPCB(skb)->flags);
1402 if (pktinfo->ipi_ifindex == LOOPBACK_IFINDEX)
1403 pktinfo->ipi_ifindex = inet_iif(skb);
1404 else if (l3slave && rt && rt->rt_iif)
1405 pktinfo->ipi_ifindex = rt->rt_iif;
1407 pktinfo->ipi_spec_dst.s_addr = fib_compute_spec_dst(skb);
1409 pktinfo->ipi_ifindex = 0;
1410 pktinfo->ipi_spec_dst.s_addr = 0;
1415 int ip_setsockopt(struct sock *sk, int level, int optname, sockptr_t optval,
1416 unsigned int optlen)
1420 if (level != SOL_IP)
1421 return -ENOPROTOOPT;
1423 err = do_ip_setsockopt(sk, level, optname, optval, optlen);
1424 #if IS_ENABLED(CONFIG_BPFILTER_UMH)
1425 if (optname >= BPFILTER_IPT_SO_SET_REPLACE &&
1426 optname < BPFILTER_IPT_SET_MAX)
1427 err = bpfilter_ip_set_sockopt(sk, optname, optval, optlen);
1429 #ifdef CONFIG_NETFILTER
1430 /* we need to exclude all possible ENOPROTOOPTs except default case */
1431 if (err == -ENOPROTOOPT && optname != IP_HDRINCL &&
1432 optname != IP_IPSEC_POLICY &&
1433 optname != IP_XFRM_POLICY &&
1434 !ip_mroute_opt(optname))
1435 err = nf_setsockopt(sk, PF_INET, optname, optval, optlen);
1439 EXPORT_SYMBOL(ip_setsockopt);
1442 * Get the options. Note for future reference. The GET of IP options gets
1443 * the _received_ ones. The set sets the _sent_ ones.
1446 static bool getsockopt_needs_rtnl(int optname)
1450 case MCAST_MSFILTER:
1456 static int ip_get_mcast_msfilter(struct sock *sk, sockptr_t optval,
1457 sockptr_t optlen, int len)
1459 const int size0 = offsetof(struct group_filter, gf_slist_flex);
1460 struct group_filter gsf;
1466 if (copy_from_sockptr(&gsf, optval, size0))
1469 num = gsf.gf_numsrc;
1470 err = ip_mc_gsfget(sk, &gsf, optval,
1471 offsetof(struct group_filter, gf_slist_flex));
1474 if (gsf.gf_numsrc < num)
1475 num = gsf.gf_numsrc;
1476 gsf_size = GROUP_FILTER_SIZE(num);
1477 if (copy_to_sockptr(optlen, &gsf_size, sizeof(int)) ||
1478 copy_to_sockptr(optval, &gsf, size0))
1483 static int compat_ip_get_mcast_msfilter(struct sock *sk, sockptr_t optval,
1484 sockptr_t optlen, int len)
1486 const int size0 = offsetof(struct compat_group_filter, gf_slist_flex);
1487 struct compat_group_filter gf32;
1488 struct group_filter gf;
1494 if (copy_from_sockptr(&gf32, optval, size0))
1497 gf.gf_interface = gf32.gf_interface;
1498 gf.gf_fmode = gf32.gf_fmode;
1499 num = gf.gf_numsrc = gf32.gf_numsrc;
1500 gf.gf_group = gf32.gf_group;
1502 err = ip_mc_gsfget(sk, &gf, optval,
1503 offsetof(struct compat_group_filter, gf_slist_flex));
1506 if (gf.gf_numsrc < num)
1508 len = GROUP_FILTER_SIZE(num) - (sizeof(gf) - sizeof(gf32));
1509 if (copy_to_sockptr(optlen, &len, sizeof(int)) ||
1510 copy_to_sockptr_offset(optval, offsetof(struct compat_group_filter, gf_fmode),
1511 &gf.gf_fmode, sizeof(gf.gf_fmode)) ||
1512 copy_to_sockptr_offset(optval, offsetof(struct compat_group_filter, gf_numsrc),
1513 &gf.gf_numsrc, sizeof(gf.gf_numsrc)))
1518 int do_ip_getsockopt(struct sock *sk, int level, int optname,
1519 sockptr_t optval, sockptr_t optlen)
1521 struct inet_sock *inet = inet_sk(sk);
1522 bool needs_rtnl = getsockopt_needs_rtnl(optname);
1526 if (level != SOL_IP)
1529 if (ip_mroute_opt(optname))
1530 return ip_mroute_getsockopt(sk, optname, optval, optlen);
1532 if (copy_from_sockptr(&len, optlen, sizeof(int)))
1537 /* Handle options that can be read without locking the socket. */
1540 val = inet_test_bit(PKTINFO, sk);
1543 val = inet_test_bit(TTL, sk);
1546 val = inet_test_bit(TOS, sk);
1549 val = inet_test_bit(RECVOPTS, sk);
1552 val = inet_test_bit(RETOPTS, sk);
1555 val = inet_test_bit(PASSSEC, sk);
1557 case IP_RECVORIGDSTADDR:
1558 val = inet_test_bit(ORIGDSTADDR, sk);
1561 val = inet_test_bit(CHECKSUM, sk);
1563 case IP_RECVFRAGSIZE:
1564 val = inet_test_bit(RECVFRAGSIZE, sk);
1567 val = inet_test_bit(RECVERR, sk);
1569 case IP_RECVERR_RFC4884:
1570 val = inet_test_bit(RECVERR_RFC4884, sk);
1573 val = inet_test_bit(FREEBIND, sk);
1576 val = inet_test_bit(HDRINCL, sk);
1578 case IP_MULTICAST_LOOP:
1579 val = inet_test_bit(MC_LOOP, sk);
1581 case IP_MULTICAST_ALL:
1582 val = inet_test_bit(MC_ALL, sk);
1584 case IP_TRANSPARENT:
1585 val = inet_test_bit(TRANSPARENT, sk);
1588 val = inet_test_bit(NODEFRAG, sk);
1594 sockopt_lock_sock(sk);
1599 unsigned char optbuf[sizeof(struct ip_options)+40];
1600 struct ip_options *opt = (struct ip_options *)optbuf;
1601 struct ip_options_rcu *inet_opt;
1603 inet_opt = rcu_dereference_protected(inet->inet_opt,
1604 lockdep_sock_is_held(sk));
1607 memcpy(optbuf, &inet_opt->opt,
1608 sizeof(struct ip_options) +
1609 inet_opt->opt.optlen);
1610 sockopt_release_sock(sk);
1612 if (opt->optlen == 0) {
1614 return copy_to_sockptr(optlen, &len, sizeof(int));
1617 ip_options_undo(opt);
1619 len = min_t(unsigned int, len, opt->optlen);
1620 if (copy_to_sockptr(optlen, &len, sizeof(int)))
1622 if (copy_to_sockptr(optval, opt->__data, len))
1631 struct net *net = sock_net(sk);
1632 val = (inet->uc_ttl == -1 ?
1633 READ_ONCE(net->ipv4.sysctl_ip_default_ttl) :
1637 case IP_BIND_ADDRESS_NO_PORT:
1638 val = inet->bind_address_no_port;
1640 case IP_MTU_DISCOVER:
1641 val = inet->pmtudisc;
1645 struct dst_entry *dst;
1647 dst = sk_dst_get(sk);
1653 sockopt_release_sock(sk);
1658 case IP_MULTICAST_TTL:
1662 val = (__force int)htonl((__u32) inet->uc_index);
1664 case IP_MULTICAST_IF:
1666 struct in_addr addr;
1667 len = min_t(unsigned int, len, sizeof(struct in_addr));
1668 addr.s_addr = inet->mc_addr;
1669 sockopt_release_sock(sk);
1671 if (copy_to_sockptr(optlen, &len, sizeof(int)))
1673 if (copy_to_sockptr(optval, &addr, len))
1679 struct ip_msfilter msf;
1681 if (len < IP_MSFILTER_SIZE(0)) {
1685 if (copy_from_sockptr(&msf, optval, IP_MSFILTER_SIZE(0))) {
1689 err = ip_mc_msfget(sk, &msf, optval, optlen);
1692 case MCAST_MSFILTER:
1693 if (in_compat_syscall())
1694 err = compat_ip_get_mcast_msfilter(sk, optval, optlen,
1697 err = ip_get_mcast_msfilter(sk, optval, optlen, len);
1703 sockopt_release_sock(sk);
1705 if (sk->sk_type != SOCK_STREAM)
1706 return -ENOPROTOOPT;
1708 if (optval.is_kernel) {
1709 msg.msg_control_is_user = false;
1710 msg.msg_control = optval.kernel;
1712 msg.msg_control_is_user = true;
1713 msg.msg_control_user = optval.user;
1715 msg.msg_controllen = len;
1716 msg.msg_flags = in_compat_syscall() ? MSG_CMSG_COMPAT : 0;
1718 if (inet_test_bit(PKTINFO, sk)) {
1719 struct in_pktinfo info;
1721 info.ipi_addr.s_addr = inet->inet_rcv_saddr;
1722 info.ipi_spec_dst.s_addr = inet->inet_rcv_saddr;
1723 info.ipi_ifindex = inet->mc_index;
1724 put_cmsg(&msg, SOL_IP, IP_PKTINFO, sizeof(info), &info);
1726 if (inet_test_bit(TTL, sk)) {
1727 int hlim = inet->mc_ttl;
1728 put_cmsg(&msg, SOL_IP, IP_TTL, sizeof(hlim), &hlim);
1730 if (inet_test_bit(TOS, sk)) {
1731 int tos = inet->rcv_tos;
1732 put_cmsg(&msg, SOL_IP, IP_TOS, sizeof(tos), &tos);
1734 len -= msg.msg_controllen;
1735 return copy_to_sockptr(optlen, &len, sizeof(int));
1738 val = inet->min_ttl;
1740 case IP_LOCAL_PORT_RANGE:
1741 val = inet->local_port_range.hi << 16 | inet->local_port_range.lo;
1744 val = inet_sk(sk)->inet_num;
1747 sockopt_release_sock(sk);
1748 return -ENOPROTOOPT;
1750 sockopt_release_sock(sk);
1752 if (len < sizeof(int) && len > 0 && val >= 0 && val <= 255) {
1753 unsigned char ucval = (unsigned char)val;
1755 if (copy_to_sockptr(optlen, &len, sizeof(int)))
1757 if (copy_to_sockptr(optval, &ucval, 1))
1760 len = min_t(unsigned int, sizeof(int), len);
1761 if (copy_to_sockptr(optlen, &len, sizeof(int)))
1763 if (copy_to_sockptr(optval, &val, len))
1769 sockopt_release_sock(sk);
1775 int ip_getsockopt(struct sock *sk, int level,
1776 int optname, char __user *optval, int __user *optlen)
1780 err = do_ip_getsockopt(sk, level, optname,
1781 USER_SOCKPTR(optval), USER_SOCKPTR(optlen));
1783 #if IS_ENABLED(CONFIG_BPFILTER_UMH)
1784 if (optname >= BPFILTER_IPT_SO_GET_INFO &&
1785 optname < BPFILTER_IPT_GET_MAX)
1786 err = bpfilter_ip_get_sockopt(sk, optname, optval, optlen);
1788 #ifdef CONFIG_NETFILTER
1789 /* we need to exclude all possible ENOPROTOOPTs except default case */
1790 if (err == -ENOPROTOOPT && optname != IP_PKTOPTIONS &&
1791 !ip_mroute_opt(optname)) {
1794 if (get_user(len, optlen))
1797 err = nf_getsockopt(sk, PF_INET, optname, optval, &len);
1799 err = put_user(len, optlen);
1805 EXPORT_SYMBOL(ip_getsockopt);