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 (!(READ_ONCE(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_errqueue_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))
1014 inet_assign_bit(TRANSPARENT, sk, val);
1017 if (sk->sk_type != SOCK_RAW)
1018 return -ENOPROTOOPT;
1019 inet_assign_bit(NODEFRAG, sk, val);
1021 case IP_BIND_ADDRESS_NO_PORT:
1022 inet_assign_bit(BIND_ADDRESS_NO_PORT, sk, val);
1027 if (val != -1 && (val < 1 || val > 255))
1029 WRITE_ONCE(inet->uc_ttl, val);
1034 if (val < 0 || val > 255)
1038 static_branch_enable(&ip4_min_ttl);
1040 WRITE_ONCE(inet->min_ttl, val);
1047 sockopt_lock_sock(sk);
1052 struct ip_options_rcu *old, *opt = NULL;
1056 err = ip_options_get(sock_net(sk), &opt, optval, optlen);
1059 old = rcu_dereference_protected(inet->inet_opt,
1060 lockdep_sock_is_held(sk));
1061 if (inet_test_bit(IS_ICSK, sk)) {
1062 struct inet_connection_sock *icsk = inet_csk(sk);
1063 #if IS_ENABLED(CONFIG_IPV6)
1064 if (sk->sk_family == PF_INET ||
1065 (!((1 << sk->sk_state) &
1066 (TCPF_LISTEN | TCPF_CLOSE)) &&
1067 inet->inet_daddr != LOOPBACK4_IPV6)) {
1070 icsk->icsk_ext_hdr_len -= old->opt.optlen;
1072 icsk->icsk_ext_hdr_len += opt->opt.optlen;
1073 icsk->icsk_sync_mss(sk, icsk->icsk_pmtu_cookie);
1074 #if IS_ENABLED(CONFIG_IPV6)
1078 rcu_assign_pointer(inet->inet_opt, opt);
1080 kfree_rcu(old, rcu);
1085 if (!(inet_test_bit(CHECKSUM, sk))) {
1086 inet_inc_convert_csum(sk);
1087 inet_set_bit(CHECKSUM, sk);
1090 if (inet_test_bit(CHECKSUM, sk)) {
1091 inet_dec_convert_csum(sk);
1092 inet_clear_bit(CHECKSUM, sk);
1096 case IP_TOS: /* This sets both TOS and Precedence */
1097 __ip_sock_set_tos(sk, val);
1099 case IP_MTU_DISCOVER:
1100 if (val < IP_PMTUDISC_DONT || val > IP_PMTUDISC_OMIT)
1102 inet->pmtudisc = val;
1104 case IP_MULTICAST_TTL:
1105 if (sk->sk_type == SOCK_STREAM)
1111 if (val < 0 || val > 255)
1117 struct net_device *dev = NULL;
1121 if (optlen != sizeof(int))
1124 ifindex = (__force int)ntohl((__force __be32)val);
1131 dev = dev_get_by_index(sock_net(sk), ifindex);
1132 err = -EADDRNOTAVAIL;
1136 midx = l3mdev_master_ifindex(dev);
1140 if (sk->sk_bound_dev_if && midx != sk->sk_bound_dev_if)
1143 inet->uc_index = ifindex;
1147 case IP_MULTICAST_IF:
1149 struct ip_mreqn mreq;
1150 struct net_device *dev = NULL;
1153 if (sk->sk_type == SOCK_STREAM)
1156 * Check the arguments are allowable
1159 if (optlen < sizeof(struct in_addr))
1163 if (optlen >= sizeof(struct ip_mreqn)) {
1164 if (copy_from_sockptr(&mreq, optval, sizeof(mreq)))
1167 memset(&mreq, 0, sizeof(mreq));
1168 if (optlen >= sizeof(struct ip_mreq)) {
1169 if (copy_from_sockptr(&mreq, optval,
1170 sizeof(struct ip_mreq)))
1172 } else if (optlen >= sizeof(struct in_addr)) {
1173 if (copy_from_sockptr(&mreq.imr_address, optval,
1174 sizeof(struct in_addr)))
1179 if (!mreq.imr_ifindex) {
1180 if (mreq.imr_address.s_addr == htonl(INADDR_ANY)) {
1186 dev = ip_dev_find(sock_net(sk), mreq.imr_address.s_addr);
1188 mreq.imr_ifindex = dev->ifindex;
1190 dev = dev_get_by_index(sock_net(sk), mreq.imr_ifindex);
1193 err = -EADDRNOTAVAIL;
1197 midx = l3mdev_master_ifindex(dev);
1202 if (sk->sk_bound_dev_if &&
1203 mreq.imr_ifindex != sk->sk_bound_dev_if &&
1204 midx != sk->sk_bound_dev_if)
1207 inet->mc_index = mreq.imr_ifindex;
1208 inet->mc_addr = mreq.imr_address.s_addr;
1213 case IP_ADD_MEMBERSHIP:
1214 case IP_DROP_MEMBERSHIP:
1216 struct ip_mreqn mreq;
1219 if (inet_test_bit(IS_ICSK, sk))
1222 if (optlen < sizeof(struct ip_mreq))
1225 if (optlen >= sizeof(struct ip_mreqn)) {
1226 if (copy_from_sockptr(&mreq, optval, sizeof(mreq)))
1229 memset(&mreq, 0, sizeof(mreq));
1230 if (copy_from_sockptr(&mreq, optval,
1231 sizeof(struct ip_mreq)))
1235 if (optname == IP_ADD_MEMBERSHIP)
1236 err = ip_mc_join_group(sk, &mreq);
1238 err = ip_mc_leave_group(sk, &mreq);
1243 struct ip_msfilter *msf;
1245 if (optlen < IP_MSFILTER_SIZE(0))
1247 if (optlen > READ_ONCE(sysctl_optmem_max)) {
1251 msf = memdup_sockptr(optval, optlen);
1256 /* numsrc >= (1G-4) overflow in 32 bits */
1257 if (msf->imsf_numsrc >= 0x3ffffffcU ||
1258 msf->imsf_numsrc > READ_ONCE(net->ipv4.sysctl_igmp_max_msf)) {
1263 if (IP_MSFILTER_SIZE(msf->imsf_numsrc) > optlen) {
1268 err = ip_mc_msfilter(sk, msf, 0);
1272 case IP_BLOCK_SOURCE:
1273 case IP_UNBLOCK_SOURCE:
1274 case IP_ADD_SOURCE_MEMBERSHIP:
1275 case IP_DROP_SOURCE_MEMBERSHIP:
1277 struct ip_mreq_source mreqs;
1280 if (optlen != sizeof(struct ip_mreq_source))
1282 if (copy_from_sockptr(&mreqs, optval, sizeof(mreqs))) {
1286 if (optname == IP_BLOCK_SOURCE) {
1287 omode = MCAST_EXCLUDE;
1289 } else if (optname == IP_UNBLOCK_SOURCE) {
1290 omode = MCAST_EXCLUDE;
1292 } else if (optname == IP_ADD_SOURCE_MEMBERSHIP) {
1293 struct ip_mreqn mreq;
1295 mreq.imr_multiaddr.s_addr = mreqs.imr_multiaddr;
1296 mreq.imr_address.s_addr = mreqs.imr_interface;
1297 mreq.imr_ifindex = 0;
1298 err = ip_mc_join_group_ssm(sk, &mreq, MCAST_INCLUDE);
1299 if (err && err != -EADDRINUSE)
1301 omode = MCAST_INCLUDE;
1303 } else /* IP_DROP_SOURCE_MEMBERSHIP */ {
1304 omode = MCAST_INCLUDE;
1307 err = ip_mc_source(add, omode, sk, &mreqs, 0);
1310 case MCAST_JOIN_GROUP:
1311 case MCAST_LEAVE_GROUP:
1312 if (in_compat_syscall())
1313 err = compat_ip_mcast_join_leave(sk, optname, optval,
1316 err = ip_mcast_join_leave(sk, optname, optval, optlen);
1318 case MCAST_JOIN_SOURCE_GROUP:
1319 case MCAST_LEAVE_SOURCE_GROUP:
1320 case MCAST_BLOCK_SOURCE:
1321 case MCAST_UNBLOCK_SOURCE:
1322 err = do_mcast_group_source(sk, optname, optval, optlen);
1324 case MCAST_MSFILTER:
1325 if (in_compat_syscall())
1326 err = compat_ip_set_mcast_msfilter(sk, optval, optlen);
1328 err = ip_set_mcast_msfilter(sk, optval, optlen);
1330 case IP_IPSEC_POLICY:
1331 case IP_XFRM_POLICY:
1333 if (!sockopt_ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
1335 err = xfrm_user_policy(sk, optname, optval, optlen);
1338 case IP_LOCAL_PORT_RANGE:
1340 const __u16 lo = val;
1341 const __u16 hi = val >> 16;
1343 if (optlen != sizeof(__u32))
1345 if (lo != 0 && hi != 0 && lo > hi)
1348 inet->local_port_range.lo = lo;
1349 inet->local_port_range.hi = hi;
1356 sockopt_release_sock(sk);
1362 sockopt_release_sock(sk);
1369 * ipv4_pktinfo_prepare - transfer some info from rtable to skb
1373 * To support IP_CMSG_PKTINFO option, we store rt_iif and specific
1374 * destination in skb->cb[] before dst drop.
1375 * This way, receiver doesn't make cache line misses to read rtable.
1377 void ipv4_pktinfo_prepare(const struct sock *sk, struct sk_buff *skb)
1379 struct in_pktinfo *pktinfo = PKTINFO_SKB_CB(skb);
1380 bool prepare = inet_test_bit(PKTINFO, sk) ||
1383 if (prepare && skb_rtable(skb)) {
1384 /* skb->cb is overloaded: prior to this point it is IP{6}CB
1385 * which has interface index (iif) as the first member of the
1386 * underlying inet{6}_skb_parm struct. This code then overlays
1387 * PKTINFO_SKB_CB and in_pktinfo also has iif as the first
1388 * element so the iif is picked up from the prior IPCB. If iif
1389 * is the loopback interface, then return the sending interface
1390 * (e.g., process binds socket to eth0 for Tx which is
1391 * redirected to loopback in the rtable/dst).
1393 struct rtable *rt = skb_rtable(skb);
1394 bool l3slave = ipv4_l3mdev_skb(IPCB(skb)->flags);
1396 if (pktinfo->ipi_ifindex == LOOPBACK_IFINDEX)
1397 pktinfo->ipi_ifindex = inet_iif(skb);
1398 else if (l3slave && rt && rt->rt_iif)
1399 pktinfo->ipi_ifindex = rt->rt_iif;
1401 pktinfo->ipi_spec_dst.s_addr = fib_compute_spec_dst(skb);
1403 pktinfo->ipi_ifindex = 0;
1404 pktinfo->ipi_spec_dst.s_addr = 0;
1409 int ip_setsockopt(struct sock *sk, int level, int optname, sockptr_t optval,
1410 unsigned int optlen)
1414 if (level != SOL_IP)
1415 return -ENOPROTOOPT;
1417 err = do_ip_setsockopt(sk, level, optname, optval, optlen);
1418 #if IS_ENABLED(CONFIG_BPFILTER_UMH)
1419 if (optname >= BPFILTER_IPT_SO_SET_REPLACE &&
1420 optname < BPFILTER_IPT_SET_MAX)
1421 err = bpfilter_ip_set_sockopt(sk, optname, optval, optlen);
1423 #ifdef CONFIG_NETFILTER
1424 /* we need to exclude all possible ENOPROTOOPTs except default case */
1425 if (err == -ENOPROTOOPT && optname != IP_HDRINCL &&
1426 optname != IP_IPSEC_POLICY &&
1427 optname != IP_XFRM_POLICY &&
1428 !ip_mroute_opt(optname))
1429 err = nf_setsockopt(sk, PF_INET, optname, optval, optlen);
1433 EXPORT_SYMBOL(ip_setsockopt);
1436 * Get the options. Note for future reference. The GET of IP options gets
1437 * the _received_ ones. The set sets the _sent_ ones.
1440 static bool getsockopt_needs_rtnl(int optname)
1444 case MCAST_MSFILTER:
1450 static int ip_get_mcast_msfilter(struct sock *sk, sockptr_t optval,
1451 sockptr_t optlen, int len)
1453 const int size0 = offsetof(struct group_filter, gf_slist_flex);
1454 struct group_filter gsf;
1460 if (copy_from_sockptr(&gsf, optval, size0))
1463 num = gsf.gf_numsrc;
1464 err = ip_mc_gsfget(sk, &gsf, optval,
1465 offsetof(struct group_filter, gf_slist_flex));
1468 if (gsf.gf_numsrc < num)
1469 num = gsf.gf_numsrc;
1470 gsf_size = GROUP_FILTER_SIZE(num);
1471 if (copy_to_sockptr(optlen, &gsf_size, sizeof(int)) ||
1472 copy_to_sockptr(optval, &gsf, size0))
1477 static int compat_ip_get_mcast_msfilter(struct sock *sk, sockptr_t optval,
1478 sockptr_t optlen, int len)
1480 const int size0 = offsetof(struct compat_group_filter, gf_slist_flex);
1481 struct compat_group_filter gf32;
1482 struct group_filter gf;
1488 if (copy_from_sockptr(&gf32, optval, size0))
1491 gf.gf_interface = gf32.gf_interface;
1492 gf.gf_fmode = gf32.gf_fmode;
1493 num = gf.gf_numsrc = gf32.gf_numsrc;
1494 gf.gf_group = gf32.gf_group;
1496 err = ip_mc_gsfget(sk, &gf, optval,
1497 offsetof(struct compat_group_filter, gf_slist_flex));
1500 if (gf.gf_numsrc < num)
1502 len = GROUP_FILTER_SIZE(num) - (sizeof(gf) - sizeof(gf32));
1503 if (copy_to_sockptr(optlen, &len, sizeof(int)) ||
1504 copy_to_sockptr_offset(optval, offsetof(struct compat_group_filter, gf_fmode),
1505 &gf.gf_fmode, sizeof(gf.gf_fmode)) ||
1506 copy_to_sockptr_offset(optval, offsetof(struct compat_group_filter, gf_numsrc),
1507 &gf.gf_numsrc, sizeof(gf.gf_numsrc)))
1512 int do_ip_getsockopt(struct sock *sk, int level, int optname,
1513 sockptr_t optval, sockptr_t optlen)
1515 struct inet_sock *inet = inet_sk(sk);
1516 bool needs_rtnl = getsockopt_needs_rtnl(optname);
1520 if (level != SOL_IP)
1523 if (ip_mroute_opt(optname))
1524 return ip_mroute_getsockopt(sk, optname, optval, optlen);
1526 if (copy_from_sockptr(&len, optlen, sizeof(int)))
1531 /* Handle options that can be read without locking the socket. */
1534 val = inet_test_bit(PKTINFO, sk);
1537 val = inet_test_bit(TTL, sk);
1540 val = inet_test_bit(TOS, sk);
1543 val = inet_test_bit(RECVOPTS, sk);
1546 val = inet_test_bit(RETOPTS, sk);
1549 val = inet_test_bit(PASSSEC, sk);
1551 case IP_RECVORIGDSTADDR:
1552 val = inet_test_bit(ORIGDSTADDR, sk);
1555 val = inet_test_bit(CHECKSUM, sk);
1557 case IP_RECVFRAGSIZE:
1558 val = inet_test_bit(RECVFRAGSIZE, sk);
1561 val = inet_test_bit(RECVERR, sk);
1563 case IP_RECVERR_RFC4884:
1564 val = inet_test_bit(RECVERR_RFC4884, sk);
1567 val = inet_test_bit(FREEBIND, sk);
1570 val = inet_test_bit(HDRINCL, sk);
1572 case IP_MULTICAST_LOOP:
1573 val = inet_test_bit(MC_LOOP, sk);
1575 case IP_MULTICAST_ALL:
1576 val = inet_test_bit(MC_ALL, sk);
1578 case IP_TRANSPARENT:
1579 val = inet_test_bit(TRANSPARENT, sk);
1582 val = inet_test_bit(NODEFRAG, sk);
1584 case IP_BIND_ADDRESS_NO_PORT:
1585 val = inet_test_bit(BIND_ADDRESS_NO_PORT, sk);
1588 val = READ_ONCE(inet->uc_ttl);
1590 val = READ_ONCE(sock_net(sk)->ipv4.sysctl_ip_default_ttl);
1593 val = READ_ONCE(inet->min_ttl);
1599 sockopt_lock_sock(sk);
1604 unsigned char optbuf[sizeof(struct ip_options)+40];
1605 struct ip_options *opt = (struct ip_options *)optbuf;
1606 struct ip_options_rcu *inet_opt;
1608 inet_opt = rcu_dereference_protected(inet->inet_opt,
1609 lockdep_sock_is_held(sk));
1612 memcpy(optbuf, &inet_opt->opt,
1613 sizeof(struct ip_options) +
1614 inet_opt->opt.optlen);
1615 sockopt_release_sock(sk);
1617 if (opt->optlen == 0) {
1619 return copy_to_sockptr(optlen, &len, sizeof(int));
1622 ip_options_undo(opt);
1624 len = min_t(unsigned int, len, opt->optlen);
1625 if (copy_to_sockptr(optlen, &len, sizeof(int)))
1627 if (copy_to_sockptr(optval, opt->__data, len))
1634 case IP_MTU_DISCOVER:
1635 val = inet->pmtudisc;
1639 struct dst_entry *dst;
1641 dst = sk_dst_get(sk);
1647 sockopt_release_sock(sk);
1652 case IP_MULTICAST_TTL:
1656 val = (__force int)htonl((__u32) inet->uc_index);
1658 case IP_MULTICAST_IF:
1660 struct in_addr addr;
1661 len = min_t(unsigned int, len, sizeof(struct in_addr));
1662 addr.s_addr = inet->mc_addr;
1663 sockopt_release_sock(sk);
1665 if (copy_to_sockptr(optlen, &len, sizeof(int)))
1667 if (copy_to_sockptr(optval, &addr, len))
1673 struct ip_msfilter msf;
1675 if (len < IP_MSFILTER_SIZE(0)) {
1679 if (copy_from_sockptr(&msf, optval, IP_MSFILTER_SIZE(0))) {
1683 err = ip_mc_msfget(sk, &msf, optval, optlen);
1686 case MCAST_MSFILTER:
1687 if (in_compat_syscall())
1688 err = compat_ip_get_mcast_msfilter(sk, optval, optlen,
1691 err = ip_get_mcast_msfilter(sk, optval, optlen, len);
1697 sockopt_release_sock(sk);
1699 if (sk->sk_type != SOCK_STREAM)
1700 return -ENOPROTOOPT;
1702 if (optval.is_kernel) {
1703 msg.msg_control_is_user = false;
1704 msg.msg_control = optval.kernel;
1706 msg.msg_control_is_user = true;
1707 msg.msg_control_user = optval.user;
1709 msg.msg_controllen = len;
1710 msg.msg_flags = in_compat_syscall() ? MSG_CMSG_COMPAT : 0;
1712 if (inet_test_bit(PKTINFO, sk)) {
1713 struct in_pktinfo info;
1715 info.ipi_addr.s_addr = inet->inet_rcv_saddr;
1716 info.ipi_spec_dst.s_addr = inet->inet_rcv_saddr;
1717 info.ipi_ifindex = inet->mc_index;
1718 put_cmsg(&msg, SOL_IP, IP_PKTINFO, sizeof(info), &info);
1720 if (inet_test_bit(TTL, sk)) {
1721 int hlim = inet->mc_ttl;
1722 put_cmsg(&msg, SOL_IP, IP_TTL, sizeof(hlim), &hlim);
1724 if (inet_test_bit(TOS, sk)) {
1725 int tos = inet->rcv_tos;
1726 put_cmsg(&msg, SOL_IP, IP_TOS, sizeof(tos), &tos);
1728 len -= msg.msg_controllen;
1729 return copy_to_sockptr(optlen, &len, sizeof(int));
1731 case IP_LOCAL_PORT_RANGE:
1732 val = inet->local_port_range.hi << 16 | inet->local_port_range.lo;
1735 val = inet_sk(sk)->inet_num;
1738 sockopt_release_sock(sk);
1739 return -ENOPROTOOPT;
1741 sockopt_release_sock(sk);
1743 if (len < sizeof(int) && len > 0 && val >= 0 && val <= 255) {
1744 unsigned char ucval = (unsigned char)val;
1746 if (copy_to_sockptr(optlen, &len, sizeof(int)))
1748 if (copy_to_sockptr(optval, &ucval, 1))
1751 len = min_t(unsigned int, sizeof(int), len);
1752 if (copy_to_sockptr(optlen, &len, sizeof(int)))
1754 if (copy_to_sockptr(optval, &val, len))
1760 sockopt_release_sock(sk);
1766 int ip_getsockopt(struct sock *sk, int level,
1767 int optname, char __user *optval, int __user *optlen)
1771 err = do_ip_getsockopt(sk, level, optname,
1772 USER_SOCKPTR(optval), USER_SOCKPTR(optlen));
1774 #if IS_ENABLED(CONFIG_BPFILTER_UMH)
1775 if (optname >= BPFILTER_IPT_SO_GET_INFO &&
1776 optname < BPFILTER_IPT_GET_MAX)
1777 err = bpfilter_ip_get_sockopt(sk, optname, optval, optlen);
1779 #ifdef CONFIG_NETFILTER
1780 /* we need to exclude all possible ENOPROTOOPTs except default case */
1781 if (err == -ENOPROTOOPT && optname != IP_PKTOPTIONS &&
1782 !ip_mroute_opt(optname)) {
1785 if (get_user(len, optlen))
1788 err = nf_getsockopt(sk, PF_INET, optname, optval, &len);
1790 err = put_user(len, optlen);
1796 EXPORT_SYMBOL(ip_getsockopt);