Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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. |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 8 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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. |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 14 | * Alan Cox : Fixed a couple of oopses in Martin's |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | * TOS tweaks. |
| 16 | * Mike McLagan : Routing by source |
| 17 | */ |
| 18 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | #include <linux/module.h> |
| 20 | #include <linux/types.h> |
| 21 | #include <linux/mm.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | #include <linux/skbuff.h> |
| 23 | #include <linux/ip.h> |
| 24 | #include <linux/icmp.h> |
Arnaldo Carvalho de Melo | 14c8502 | 2005-12-27 02:43:12 -0200 | [diff] [blame] | 25 | #include <linux/inetdevice.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | #include <linux/netdevice.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 27 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | #include <net/sock.h> |
| 29 | #include <net/ip.h> |
| 30 | #include <net/icmp.h> |
Arnaldo Carvalho de Melo | d83d846 | 2005-12-13 23:26:10 -0800 | [diff] [blame] | 31 | #include <net/tcp_states.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> |
Maciej Żenczykowski | 2c67e9a | 2011-10-22 00:07:47 -0400 | [diff] [blame] | 37 | #include <net/inet_ecn.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | #include <net/route.h> |
| 39 | #include <net/xfrm.h> |
David L Stevens | dae5029 | 2008-04-27 01:06:07 -0700 | [diff] [blame] | 40 | #include <net/compat.h> |
Tom Herbert | ad6f939 | 2015-01-05 13:56:17 -0800 | [diff] [blame] | 41 | #include <net/checksum.h> |
Eric Dumazet | dfd56b8 | 2011-12-10 09:48:31 +0000 | [diff] [blame] | 42 | #if IS_ENABLED(CONFIG_IPV6) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | #include <net/transp_v6.h> |
| 44 | #endif |
David S. Miller | 35ebf65 | 2012-06-28 03:59:11 -0700 | [diff] [blame] | 45 | #include <net/ip_fib.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | |
| 47 | #include <linux/errqueue.h> |
Linus Torvalds | 7c0f6ba | 2016-12-24 11:46:01 -0800 | [diff] [blame] | 48 | #include <linux/uaccess.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | |
Alexei Starovoitov | d2ba09c | 2018-05-21 19:22:30 -0700 | [diff] [blame] | 50 | #include <linux/bpfilter.h> |
| 51 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | /* |
| 53 | * SOL_IP control messages. |
| 54 | */ |
| 55 | |
| 56 | static void ip_cmsg_recv_pktinfo(struct msghdr *msg, struct sk_buff *skb) |
| 57 | { |
Eric Dumazet | d826eb1 | 2011-11-09 07:24:35 +0000 | [diff] [blame] | 58 | struct in_pktinfo info = *PKTINFO_SKB_CB(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 60 | info.ipi_addr.s_addr = ip_hdr(skb)->daddr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | |
| 62 | put_cmsg(msg, SOL_IP, IP_PKTINFO, sizeof(info), &info); |
| 63 | } |
| 64 | |
| 65 | static void ip_cmsg_recv_ttl(struct msghdr *msg, struct sk_buff *skb) |
| 66 | { |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 67 | int ttl = ip_hdr(skb)->ttl; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | put_cmsg(msg, SOL_IP, IP_TTL, sizeof(int), &ttl); |
| 69 | } |
| 70 | |
| 71 | static void ip_cmsg_recv_tos(struct msghdr *msg, struct sk_buff *skb) |
| 72 | { |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 73 | put_cmsg(msg, SOL_IP, IP_TOS, 1, &ip_hdr(skb)->tos); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | static void ip_cmsg_recv_opts(struct msghdr *msg, struct sk_buff *skb) |
| 77 | { |
| 78 | if (IPCB(skb)->opt.optlen == 0) |
| 79 | return; |
| 80 | |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 81 | put_cmsg(msg, SOL_IP, IP_RECVOPTS, IPCB(skb)->opt.optlen, |
| 82 | ip_hdr(skb) + 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | |
Paolo Abeni | 91ed1e6 | 2017-08-03 18:07:06 +0200 | [diff] [blame] | 86 | static void ip_cmsg_recv_retopts(struct net *net, struct msghdr *msg, |
| 87 | struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | { |
| 89 | unsigned char optbuf[sizeof(struct ip_options) + 40]; |
Daniel Baluta | 5e73ea1 | 2012-04-15 01:34:41 +0000 | [diff] [blame] | 90 | struct ip_options *opt = (struct ip_options *)optbuf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | |
| 92 | if (IPCB(skb)->opt.optlen == 0) |
| 93 | return; |
| 94 | |
Paolo Abeni | 91ed1e6 | 2017-08-03 18:07:06 +0200 | [diff] [blame] | 95 | if (ip_options_echo(net, opt, skb)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | msg->msg_flags |= MSG_CTRUNC; |
| 97 | return; |
| 98 | } |
| 99 | ip_options_undo(opt); |
| 100 | |
| 101 | put_cmsg(msg, SOL_IP, IP_RETOPTS, opt->optlen, opt->__data); |
| 102 | } |
| 103 | |
Willem de Bruijn | 70ecc24 | 2016-11-02 11:02:16 -0400 | [diff] [blame] | 104 | static void ip_cmsg_recv_fragsize(struct msghdr *msg, struct sk_buff *skb) |
| 105 | { |
| 106 | int val; |
| 107 | |
| 108 | if (IPCB(skb)->frag_max_size == 0) |
| 109 | return; |
| 110 | |
| 111 | val = IPCB(skb)->frag_max_size; |
| 112 | put_cmsg(msg, SOL_IP, IP_RECVFRAGSIZE, sizeof(val), &val); |
| 113 | } |
| 114 | |
Tom Herbert | ad6f939 | 2015-01-05 13:56:17 -0800 | [diff] [blame] | 115 | static void ip_cmsg_recv_checksum(struct msghdr *msg, struct sk_buff *skb, |
Eric Dumazet | 10df8e6 | 2016-10-23 18:03:06 -0700 | [diff] [blame] | 116 | int tlen, int offset) |
Tom Herbert | ad6f939 | 2015-01-05 13:56:17 -0800 | [diff] [blame] | 117 | { |
| 118 | __wsum csum = skb->csum; |
| 119 | |
| 120 | if (skb->ip_summed != CHECKSUM_COMPLETE) |
| 121 | return; |
| 122 | |
Paolo Abeni | ca4ef45 | 2017-02-21 09:33:18 +0100 | [diff] [blame] | 123 | if (offset != 0) { |
| 124 | int tend_off = skb_transport_offset(skb) + tlen; |
| 125 | csum = csum_sub(csum, skb_checksum(skb, tend_off, offset, 0)); |
| 126 | } |
Tom Herbert | ad6f939 | 2015-01-05 13:56:17 -0800 | [diff] [blame] | 127 | |
| 128 | put_cmsg(msg, SOL_IP, IP_CHECKSUM, sizeof(__wsum), &csum); |
| 129 | } |
| 130 | |
Catherine Zhang | 2c7946a | 2006-03-20 22:41:23 -0800 | [diff] [blame] | 131 | static void ip_cmsg_recv_security(struct msghdr *msg, struct sk_buff *skb) |
| 132 | { |
| 133 | char *secdata; |
Catherine Zhang | dc49c1f | 2006-08-02 14:12:06 -0700 | [diff] [blame] | 134 | u32 seclen, secid; |
Catherine Zhang | 2c7946a | 2006-03-20 22:41:23 -0800 | [diff] [blame] | 135 | int err; |
| 136 | |
Catherine Zhang | dc49c1f | 2006-08-02 14:12:06 -0700 | [diff] [blame] | 137 | err = security_socket_getpeersec_dgram(NULL, skb, &secid); |
| 138 | if (err) |
| 139 | return; |
| 140 | |
| 141 | err = security_secid_to_secctx(secid, &secdata, &seclen); |
Catherine Zhang | 2c7946a | 2006-03-20 22:41:23 -0800 | [diff] [blame] | 142 | if (err) |
| 143 | return; |
| 144 | |
| 145 | put_cmsg(msg, SOL_IP, SCM_SECURITY, seclen, secdata); |
Catherine Zhang | dc49c1f | 2006-08-02 14:12:06 -0700 | [diff] [blame] | 146 | security_release_secctx(secdata, seclen); |
Catherine Zhang | 2c7946a | 2006-03-20 22:41:23 -0800 | [diff] [blame] | 147 | } |
| 148 | |
Harvey Harrison | 21d1a16 | 2008-11-20 01:54:27 -0800 | [diff] [blame] | 149 | static void ip_cmsg_recv_dstaddr(struct msghdr *msg, struct sk_buff *skb) |
Balazs Scheidler | e8b2dfe | 2008-11-16 19:32:39 -0800 | [diff] [blame] | 150 | { |
| 151 | struct sockaddr_in sin; |
Eric Dumazet | b71d1d4 | 2011-04-22 04:53:02 +0000 | [diff] [blame] | 152 | const struct iphdr *iph = ip_hdr(skb); |
Harvey Harrison | 21d1a16 | 2008-11-20 01:54:27 -0800 | [diff] [blame] | 153 | __be16 *ports = (__be16 *)skb_transport_header(skb); |
Balazs Scheidler | e8b2dfe | 2008-11-16 19:32:39 -0800 | [diff] [blame] | 154 | |
Willem de Bruijn | 39b2dd7 | 2016-12-22 18:19:16 -0500 | [diff] [blame] | 155 | if (skb_transport_offset(skb) + 4 > (int)skb->len) |
Balazs Scheidler | e8b2dfe | 2008-11-16 19:32:39 -0800 | [diff] [blame] | 156 | return; |
| 157 | |
| 158 | /* All current transport protocols have the port numbers in the |
| 159 | * first four bytes of the transport header and this function is |
| 160 | * written with this assumption in mind. |
| 161 | */ |
| 162 | |
| 163 | sin.sin_family = AF_INET; |
| 164 | sin.sin_addr.s_addr = iph->daddr; |
| 165 | sin.sin_port = ports[1]; |
| 166 | memset(sin.sin_zero, 0, sizeof(sin.sin_zero)); |
| 167 | |
| 168 | put_cmsg(msg, SOL_IP, IP_ORIGDSTADDR, sizeof(sin), &sin); |
| 169 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | |
Paolo Abeni | ad95903 | 2016-11-04 11:28:58 +0100 | [diff] [blame] | 171 | void ip_cmsg_recv_offset(struct msghdr *msg, struct sock *sk, |
| 172 | struct sk_buff *skb, int tlen, int offset) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | { |
Paolo Abeni | ad95903 | 2016-11-04 11:28:58 +0100 | [diff] [blame] | 174 | struct inet_sock *inet = inet_sk(sk); |
Eric Dumazet | 95c9617 | 2012-04-15 05:58:06 +0000 | [diff] [blame] | 175 | unsigned int flags = inet->cmsg_flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | |
| 177 | /* Ordered by supposed usage frequency */ |
Tom Herbert | c44d13d | 2015-01-05 13:56:15 -0800 | [diff] [blame] | 178 | if (flags & IP_CMSG_PKTINFO) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | ip_cmsg_recv_pktinfo(msg, skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | |
Tom Herbert | c44d13d | 2015-01-05 13:56:15 -0800 | [diff] [blame] | 181 | flags &= ~IP_CMSG_PKTINFO; |
| 182 | if (!flags) |
| 183 | return; |
| 184 | } |
| 185 | |
| 186 | if (flags & IP_CMSG_TTL) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | ip_cmsg_recv_ttl(msg, skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | |
Tom Herbert | c44d13d | 2015-01-05 13:56:15 -0800 | [diff] [blame] | 189 | flags &= ~IP_CMSG_TTL; |
| 190 | if (!flags) |
| 191 | return; |
| 192 | } |
| 193 | |
| 194 | if (flags & IP_CMSG_TOS) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | ip_cmsg_recv_tos(msg, skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | |
Tom Herbert | c44d13d | 2015-01-05 13:56:15 -0800 | [diff] [blame] | 197 | flags &= ~IP_CMSG_TOS; |
| 198 | if (!flags) |
| 199 | return; |
| 200 | } |
| 201 | |
| 202 | if (flags & IP_CMSG_RECVOPTS) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | ip_cmsg_recv_opts(msg, skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | |
Tom Herbert | c44d13d | 2015-01-05 13:56:15 -0800 | [diff] [blame] | 205 | flags &= ~IP_CMSG_RECVOPTS; |
| 206 | if (!flags) |
| 207 | return; |
| 208 | } |
| 209 | |
| 210 | if (flags & IP_CMSG_RETOPTS) { |
Paolo Abeni | 91ed1e6 | 2017-08-03 18:07:06 +0200 | [diff] [blame] | 211 | ip_cmsg_recv_retopts(sock_net(sk), msg, skb); |
Catherine Zhang | 2c7946a | 2006-03-20 22:41:23 -0800 | [diff] [blame] | 212 | |
Tom Herbert | c44d13d | 2015-01-05 13:56:15 -0800 | [diff] [blame] | 213 | flags &= ~IP_CMSG_RETOPTS; |
| 214 | if (!flags) |
| 215 | return; |
| 216 | } |
| 217 | |
| 218 | if (flags & IP_CMSG_PASSSEC) { |
Catherine Zhang | 2c7946a | 2006-03-20 22:41:23 -0800 | [diff] [blame] | 219 | ip_cmsg_recv_security(msg, skb); |
Balazs Scheidler | e8b2dfe | 2008-11-16 19:32:39 -0800 | [diff] [blame] | 220 | |
Tom Herbert | c44d13d | 2015-01-05 13:56:15 -0800 | [diff] [blame] | 221 | flags &= ~IP_CMSG_PASSSEC; |
| 222 | if (!flags) |
| 223 | return; |
| 224 | } |
| 225 | |
Tom Herbert | ad6f939 | 2015-01-05 13:56:17 -0800 | [diff] [blame] | 226 | if (flags & IP_CMSG_ORIGDSTADDR) { |
Balazs Scheidler | e8b2dfe | 2008-11-16 19:32:39 -0800 | [diff] [blame] | 227 | ip_cmsg_recv_dstaddr(msg, skb); |
| 228 | |
Tom Herbert | ad6f939 | 2015-01-05 13:56:17 -0800 | [diff] [blame] | 229 | flags &= ~IP_CMSG_ORIGDSTADDR; |
| 230 | if (!flags) |
| 231 | return; |
| 232 | } |
| 233 | |
| 234 | if (flags & IP_CMSG_CHECKSUM) |
Eric Dumazet | 10df8e6 | 2016-10-23 18:03:06 -0700 | [diff] [blame] | 235 | ip_cmsg_recv_checksum(msg, skb, tlen, offset); |
Willem de Bruijn | 70ecc24 | 2016-11-02 11:02:16 -0400 | [diff] [blame] | 236 | |
| 237 | if (flags & IP_CMSG_RECVFRAGSIZE) |
| 238 | ip_cmsg_recv_fragsize(msg, skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | } |
Tom Herbert | 5961de9 | 2015-01-05 13:56:16 -0800 | [diff] [blame] | 240 | EXPORT_SYMBOL(ip_cmsg_recv_offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | |
Soheil Hassas Yeganeh | 24025c4 | 2016-04-02 23:08:10 -0400 | [diff] [blame] | 242 | int ip_cmsg_send(struct sock *sk, struct msghdr *msg, struct ipcm_cookie *ipc, |
Hannes Frederic Sowa | c8e6ad0 | 2014-02-18 21:38:08 +0100 | [diff] [blame] | 243 | bool allow_ipv6) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | { |
Francesco Fusco | f02db31 | 2013-09-24 15:43:08 +0200 | [diff] [blame] | 245 | int err, val; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | struct cmsghdr *cmsg; |
Soheil Hassas Yeganeh | 24025c4 | 2016-04-02 23:08:10 -0400 | [diff] [blame] | 247 | struct net *net = sock_net(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | |
Gu Zheng | f95b414 | 2014-12-11 11:22:04 +0800 | [diff] [blame] | 249 | for_each_cmsghdr(cmsg, msg) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 250 | if (!CMSG_OK(msg, cmsg)) |
| 251 | return -EINVAL; |
Eric Dumazet | 5337b5b | 2014-11-10 17:54:25 -0800 | [diff] [blame] | 252 | #if IS_ENABLED(CONFIG_IPV6) |
Hannes Frederic Sowa | c8e6ad0 | 2014-02-18 21:38:08 +0100 | [diff] [blame] | 253 | if (allow_ipv6 && |
| 254 | cmsg->cmsg_level == SOL_IPV6 && |
| 255 | cmsg->cmsg_type == IPV6_PKTINFO) { |
| 256 | struct in6_pktinfo *src_info; |
| 257 | |
| 258 | if (cmsg->cmsg_len < CMSG_LEN(sizeof(*src_info))) |
| 259 | return -EINVAL; |
| 260 | src_info = (struct in6_pktinfo *)CMSG_DATA(cmsg); |
| 261 | if (!ipv6_addr_v4mapped(&src_info->ipi6_addr)) |
| 262 | return -EINVAL; |
David Ahern | 1cbec07 | 2018-02-16 11:03:03 -0800 | [diff] [blame] | 263 | if (src_info->ipi6_ifindex) |
| 264 | ipc->oif = src_info->ipi6_ifindex; |
Hannes Frederic Sowa | c8e6ad0 | 2014-02-18 21:38:08 +0100 | [diff] [blame] | 265 | ipc->addr = src_info->ipi6_addr.s6_addr32[3]; |
| 266 | continue; |
| 267 | } |
| 268 | #endif |
Soheil Hassas Yeganeh | 24025c4 | 2016-04-02 23:08:10 -0400 | [diff] [blame] | 269 | if (cmsg->cmsg_level == SOL_SOCKET) { |
Eric Dumazet | 2632616 | 2016-05-13 06:14:37 -0700 | [diff] [blame] | 270 | err = __sock_cmsg_send(sk, msg, cmsg, &ipc->sockc); |
| 271 | if (err) |
| 272 | return err; |
Soheil Hassas Yeganeh | 24025c4 | 2016-04-02 23:08:10 -0400 | [diff] [blame] | 273 | continue; |
| 274 | } |
| 275 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | if (cmsg->cmsg_level != SOL_IP) |
| 277 | continue; |
| 278 | switch (cmsg->cmsg_type) { |
| 279 | case IP_RETOPTS: |
yuan linyu | 1ff8ceb | 2017-01-03 20:42:17 +0800 | [diff] [blame] | 280 | err = cmsg->cmsg_len - sizeof(struct cmsghdr); |
Eric Dumazet | 9194830 | 2016-02-04 06:23:28 -0800 | [diff] [blame] | 281 | |
| 282 | /* Our caller is responsible for freeing ipc->opt */ |
Eric Dumazet | 4d52cfb | 2009-06-02 00:42:16 -0700 | [diff] [blame] | 283 | err = ip_options_get(net, &ipc->opt, CMSG_DATA(cmsg), |
| 284 | err < 40 ? err : 40); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | if (err) |
| 286 | return err; |
| 287 | break; |
| 288 | case IP_PKTINFO: |
| 289 | { |
| 290 | struct in_pktinfo *info; |
| 291 | if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct in_pktinfo))) |
| 292 | return -EINVAL; |
| 293 | info = (struct in_pktinfo *)CMSG_DATA(cmsg); |
David Ahern | 1cbec07 | 2018-02-16 11:03:03 -0800 | [diff] [blame] | 294 | if (info->ipi_ifindex) |
| 295 | ipc->oif = info->ipi_ifindex; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 296 | ipc->addr = info->ipi_spec_dst.s_addr; |
| 297 | break; |
| 298 | } |
Francesco Fusco | f02db31 | 2013-09-24 15:43:08 +0200 | [diff] [blame] | 299 | case IP_TTL: |
| 300 | if (cmsg->cmsg_len != CMSG_LEN(sizeof(int))) |
| 301 | return -EINVAL; |
| 302 | val = *(int *)CMSG_DATA(cmsg); |
| 303 | if (val < 1 || val > 255) |
| 304 | return -EINVAL; |
| 305 | ipc->ttl = val; |
| 306 | break; |
| 307 | case IP_TOS: |
Eric Dumazet | e895cdc | 2016-09-07 21:52:56 -0700 | [diff] [blame] | 308 | if (cmsg->cmsg_len == CMSG_LEN(sizeof(int))) |
| 309 | val = *(int *)CMSG_DATA(cmsg); |
| 310 | else if (cmsg->cmsg_len == CMSG_LEN(sizeof(u8))) |
| 311 | val = *(u8 *)CMSG_DATA(cmsg); |
| 312 | else |
Francesco Fusco | f02db31 | 2013-09-24 15:43:08 +0200 | [diff] [blame] | 313 | return -EINVAL; |
Francesco Fusco | f02db31 | 2013-09-24 15:43:08 +0200 | [diff] [blame] | 314 | if (val < 0 || val > 255) |
| 315 | return -EINVAL; |
| 316 | ipc->tos = val; |
| 317 | ipc->priority = rt_tos2priority(ipc->tos); |
| 318 | break; |
| 319 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 320 | default: |
| 321 | return -EINVAL; |
| 322 | } |
| 323 | } |
| 324 | return 0; |
| 325 | } |
| 326 | |
Eric Dumazet | 592fcb9 | 2010-06-09 16:21:07 +0000 | [diff] [blame] | 327 | static void ip_ra_destroy_rcu(struct rcu_head *head) |
Eric Dumazet | 6601850 | 2010-06-07 03:12:08 +0000 | [diff] [blame] | 328 | { |
Eric Dumazet | 592fcb9 | 2010-06-09 16:21:07 +0000 | [diff] [blame] | 329 | struct ip_ra_chain *ra = container_of(head, struct ip_ra_chain, rcu); |
| 330 | |
| 331 | sock_put(ra->saved_sk); |
| 332 | kfree(ra); |
Eric Dumazet | 6601850 | 2010-06-07 03:12:08 +0000 | [diff] [blame] | 333 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 334 | |
Eric Dumazet | 4d52cfb | 2009-06-02 00:42:16 -0700 | [diff] [blame] | 335 | int ip_ra_control(struct sock *sk, unsigned char on, |
| 336 | void (*destructor)(struct sock *)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 337 | { |
Eric Dumazet | 43a951e | 2010-10-25 03:32:44 +0000 | [diff] [blame] | 338 | struct ip_ra_chain *ra, *new_ra; |
| 339 | struct ip_ra_chain __rcu **rap; |
Kirill Tkhai | 5796ef7 | 2018-03-22 12:45:32 +0300 | [diff] [blame] | 340 | struct net *net = sock_net(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 341 | |
Eric Dumazet | c720c7e8 | 2009-10-15 06:30:45 +0000 | [diff] [blame] | 342 | if (sk->sk_type != SOCK_RAW || inet_sk(sk)->inet_num == IPPROTO_RAW) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 343 | return -EINVAL; |
| 344 | |
| 345 | new_ra = on ? kmalloc(sizeof(*new_ra), GFP_KERNEL) : NULL; |
| 346 | |
Kirill Tkhai | d9ff304 | 2018-03-22 12:45:40 +0300 | [diff] [blame] | 347 | mutex_lock(&net->ipv4.ra_mutex); |
Kirill Tkhai | 5796ef7 | 2018-03-22 12:45:32 +0300 | [diff] [blame] | 348 | for (rap = &net->ipv4.ra_chain; |
Kirill Tkhai | 76d3e15 | 2018-03-22 12:45:02 +0300 | [diff] [blame] | 349 | (ra = rcu_dereference_protected(*rap, |
Kirill Tkhai | d9ff304 | 2018-03-22 12:45:40 +0300 | [diff] [blame] | 350 | lockdep_is_held(&net->ipv4.ra_mutex))) != NULL; |
Eric Dumazet | 43a951e | 2010-10-25 03:32:44 +0000 | [diff] [blame] | 351 | rap = &ra->next) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 352 | if (ra->sk == sk) { |
| 353 | if (on) { |
Kirill Tkhai | d9ff304 | 2018-03-22 12:45:40 +0300 | [diff] [blame] | 354 | mutex_unlock(&net->ipv4.ra_mutex); |
Jesper Juhl | a51482b | 2005-11-08 09:41:34 -0800 | [diff] [blame] | 355 | kfree(new_ra); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 356 | return -EADDRINUSE; |
| 357 | } |
Eric Dumazet | 592fcb9 | 2010-06-09 16:21:07 +0000 | [diff] [blame] | 358 | /* dont let ip_call_ra_chain() use sk again */ |
| 359 | ra->sk = NULL; |
Eric Dumazet | 8e380f0 | 2014-09-09 08:11:41 -0700 | [diff] [blame] | 360 | RCU_INIT_POINTER(*rap, ra->next); |
Kirill Tkhai | d9ff304 | 2018-03-22 12:45:40 +0300 | [diff] [blame] | 361 | mutex_unlock(&net->ipv4.ra_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 362 | |
| 363 | if (ra->destructor) |
| 364 | ra->destructor(sk); |
Eric Dumazet | 592fcb9 | 2010-06-09 16:21:07 +0000 | [diff] [blame] | 365 | /* |
| 366 | * Delay sock_put(sk) and kfree(ra) after one rcu grace |
| 367 | * period. This guarantee ip_call_ra_chain() dont need |
| 368 | * to mess with socket refcounts. |
| 369 | */ |
| 370 | ra->saved_sk = sk; |
| 371 | call_rcu(&ra->rcu, ip_ra_destroy_rcu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 372 | return 0; |
| 373 | } |
| 374 | } |
Kirill Tkhai | 76d3e15 | 2018-03-22 12:45:02 +0300 | [diff] [blame] | 375 | if (!new_ra) { |
Kirill Tkhai | d9ff304 | 2018-03-22 12:45:40 +0300 | [diff] [blame] | 376 | mutex_unlock(&net->ipv4.ra_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 377 | return -ENOBUFS; |
Kirill Tkhai | 76d3e15 | 2018-03-22 12:45:02 +0300 | [diff] [blame] | 378 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 379 | new_ra->sk = sk; |
| 380 | new_ra->destructor = destructor; |
| 381 | |
Eric Dumazet | 8e380f0 | 2014-09-09 08:11:41 -0700 | [diff] [blame] | 382 | RCU_INIT_POINTER(new_ra->next, ra); |
Eric Dumazet | 6601850 | 2010-06-07 03:12:08 +0000 | [diff] [blame] | 383 | rcu_assign_pointer(*rap, new_ra); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 384 | sock_hold(sk); |
Kirill Tkhai | d9ff304 | 2018-03-22 12:45:40 +0300 | [diff] [blame] | 385 | mutex_unlock(&net->ipv4.ra_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 386 | |
| 387 | return 0; |
| 388 | } |
| 389 | |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 390 | void ip_icmp_error(struct sock *sk, struct sk_buff *skb, int err, |
Al Viro | 35986b3 | 2006-09-27 18:34:21 -0700 | [diff] [blame] | 391 | __be16 port, u32 info, u8 *payload) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 392 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 393 | struct sock_exterr_skb *serr; |
| 394 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 395 | skb = skb_clone(skb, GFP_ATOMIC); |
| 396 | if (!skb) |
| 397 | return; |
| 398 | |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 399 | serr = SKB_EXT_ERR(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 400 | serr->ee.ee_errno = err; |
| 401 | serr->ee.ee_origin = SO_EE_ORIGIN_ICMP; |
Arnaldo Carvalho de Melo | 88c7664 | 2007-03-13 14:43:18 -0300 | [diff] [blame] | 402 | serr->ee.ee_type = icmp_hdr(skb)->type; |
| 403 | serr->ee.ee_code = icmp_hdr(skb)->code; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 404 | serr->ee.ee_pad = 0; |
| 405 | serr->ee.ee_info = info; |
| 406 | serr->ee.ee_data = 0; |
Arnaldo Carvalho de Melo | 88c7664 | 2007-03-13 14:43:18 -0300 | [diff] [blame] | 407 | serr->addr_offset = (u8 *)&(((struct iphdr *)(icmp_hdr(skb) + 1))->daddr) - |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 408 | skb_network_header(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 409 | serr->port = port; |
| 410 | |
Ian Morris | 00db412 | 2015-04-03 09:17:27 +0100 | [diff] [blame] | 411 | if (skb_pull(skb, payload - skb->data)) { |
Arnaldo Carvalho de Melo | bd82393 | 2007-03-13 17:10:43 -0300 | [diff] [blame] | 412 | skb_reset_transport_header(skb); |
| 413 | if (sock_queue_err_skb(sk, skb) == 0) |
| 414 | return; |
| 415 | } |
| 416 | kfree_skb(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 417 | } |
| 418 | |
Al Viro | 0579016 | 2006-09-27 18:33:40 -0700 | [diff] [blame] | 419 | void ip_local_error(struct sock *sk, int err, __be32 daddr, __be16 port, u32 info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 420 | { |
| 421 | struct inet_sock *inet = inet_sk(sk); |
| 422 | struct sock_exterr_skb *serr; |
| 423 | struct iphdr *iph; |
| 424 | struct sk_buff *skb; |
| 425 | |
| 426 | if (!inet->recverr) |
| 427 | return; |
| 428 | |
| 429 | skb = alloc_skb(sizeof(struct iphdr), GFP_ATOMIC); |
| 430 | if (!skb) |
| 431 | return; |
| 432 | |
Arnaldo Carvalho de Melo | 2ca9e6f | 2007-03-10 19:15:25 -0300 | [diff] [blame] | 433 | skb_put(skb, sizeof(struct iphdr)); |
| 434 | skb_reset_network_header(skb); |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 435 | iph = ip_hdr(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 436 | iph->daddr = daddr; |
| 437 | |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 438 | serr = SKB_EXT_ERR(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 439 | serr->ee.ee_errno = err; |
| 440 | serr->ee.ee_origin = SO_EE_ORIGIN_LOCAL; |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 441 | serr->ee.ee_type = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 442 | serr->ee.ee_code = 0; |
| 443 | serr->ee.ee_pad = 0; |
| 444 | serr->ee.ee_info = info; |
| 445 | serr->ee.ee_data = 0; |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 446 | serr->addr_offset = (u8 *)&iph->daddr - skb_network_header(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 447 | serr->port = port; |
| 448 | |
Arnaldo Carvalho de Melo | 27a884d | 2007-04-19 20:29:13 -0700 | [diff] [blame] | 449 | __skb_pull(skb, skb_tail_pointer(skb) - skb->data); |
Arnaldo Carvalho de Melo | bd82393 | 2007-03-13 17:10:43 -0300 | [diff] [blame] | 450 | skb_reset_transport_header(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 451 | |
| 452 | if (sock_queue_err_skb(sk, skb)) |
| 453 | kfree_skb(skb); |
| 454 | } |
| 455 | |
Julian Anastasov | 34b99df | 2015-06-23 08:34:39 +0300 | [diff] [blame] | 456 | /* For some errors we have valid addr_offset even with zero payload and |
| 457 | * zero port. Also, addr_offset should be supported if port is set. |
| 458 | */ |
| 459 | static inline bool ipv4_datagram_support_addr(struct sock_exterr_skb *serr) |
| 460 | { |
| 461 | return serr->ee.ee_origin == SO_EE_ORIGIN_ICMP || |
| 462 | serr->ee.ee_origin == SO_EE_ORIGIN_LOCAL || serr->port; |
| 463 | } |
| 464 | |
Willem de Bruijn | c247f05 | 2015-03-07 20:33:22 -0500 | [diff] [blame] | 465 | /* IPv4 supports cmsg on all imcp errors and some timestamps |
| 466 | * |
| 467 | * Timestamp code paths do not initialize the fields expected by cmsg: |
| 468 | * the PKTINFO fields in skb->cb[]. Fill those in here. |
| 469 | */ |
| 470 | static bool ipv4_datagram_support_cmsg(const struct sock *sk, |
| 471 | struct sk_buff *skb, |
| 472 | int ee_origin) |
Willem de Bruijn | 829ae9d | 2014-11-30 22:22:34 -0500 | [diff] [blame] | 473 | { |
Willem de Bruijn | c247f05 | 2015-03-07 20:33:22 -0500 | [diff] [blame] | 474 | struct in_pktinfo *info; |
Willem de Bruijn | 829ae9d | 2014-11-30 22:22:34 -0500 | [diff] [blame] | 475 | |
Willem de Bruijn | c247f05 | 2015-03-07 20:33:22 -0500 | [diff] [blame] | 476 | if (ee_origin == SO_EE_ORIGIN_ICMP) |
| 477 | return true; |
| 478 | |
| 479 | if (ee_origin == SO_EE_ORIGIN_LOCAL) |
| 480 | return false; |
| 481 | |
| 482 | /* Support IP_PKTINFO on tstamp packets if requested, to correlate |
Willem de Bruijn | 1862d62 | 2017-04-12 19:24:35 -0400 | [diff] [blame] | 483 | * timestamp with egress dev. Not possible for packets without iif |
Willem de Bruijn | c247f05 | 2015-03-07 20:33:22 -0500 | [diff] [blame] | 484 | * or without payload (SOF_TIMESTAMPING_OPT_TSONLY). |
| 485 | */ |
Willem de Bruijn | 1862d62 | 2017-04-12 19:24:35 -0400 | [diff] [blame] | 486 | info = PKTINFO_SKB_CB(skb); |
| 487 | if (!(sk->sk_tsflags & SOF_TIMESTAMPING_OPT_CMSG) || |
| 488 | !info->ipi_ifindex) |
Willem de Bruijn | 829ae9d | 2014-11-30 22:22:34 -0500 | [diff] [blame] | 489 | return false; |
| 490 | |
| 491 | info->ipi_spec_dst.s_addr = ip_hdr(skb)->saddr; |
Willem de Bruijn | 829ae9d | 2014-11-30 22:22:34 -0500 | [diff] [blame] | 492 | return true; |
| 493 | } |
| 494 | |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 495 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 496 | * Handle MSG_ERRQUEUE |
| 497 | */ |
Hannes Frederic Sowa | 85fbaa7 | 2013-11-23 00:46:12 +0100 | [diff] [blame] | 498 | int ip_recv_error(struct sock *sk, struct msghdr *msg, int len, int *addr_len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 499 | { |
| 500 | struct sock_exterr_skb *serr; |
Willem de Bruijn | 364a9e9 | 2014-08-31 21:30:27 -0400 | [diff] [blame] | 501 | struct sk_buff *skb; |
Steffen Hurrle | 342dfc3 | 2014-01-17 22:53:15 +0100 | [diff] [blame] | 502 | DECLARE_SOCKADDR(struct sockaddr_in *, sin, msg->msg_name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 503 | struct { |
| 504 | struct sock_extended_err ee; |
| 505 | struct sockaddr_in offender; |
| 506 | } errhdr; |
| 507 | int err; |
| 508 | int copied; |
| 509 | |
| 510 | err = -EAGAIN; |
Willem de Bruijn | 364a9e9 | 2014-08-31 21:30:27 -0400 | [diff] [blame] | 511 | skb = sock_dequeue_err_skb(sk); |
Ian Morris | 51456b2 | 2015-04-03 09:17:26 +0100 | [diff] [blame] | 512 | if (!skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 513 | goto out; |
| 514 | |
| 515 | copied = skb->len; |
| 516 | if (copied > len) { |
| 517 | msg->msg_flags |= MSG_TRUNC; |
| 518 | copied = len; |
| 519 | } |
David S. Miller | 51f3d02 | 2014-11-05 16:46:40 -0500 | [diff] [blame] | 520 | err = skb_copy_datagram_msg(skb, 0, msg, copied); |
Eric Dumazet | 960a262 | 2016-04-21 22:27:32 -0700 | [diff] [blame] | 521 | if (unlikely(err)) { |
| 522 | kfree_skb(skb); |
| 523 | return err; |
| 524 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 525 | sock_recv_timestamp(msg, sk, skb); |
| 526 | |
| 527 | serr = SKB_EXT_ERR(skb); |
| 528 | |
Julian Anastasov | 34b99df | 2015-06-23 08:34:39 +0300 | [diff] [blame] | 529 | if (sin && ipv4_datagram_support_addr(serr)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 530 | sin->sin_family = AF_INET; |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 531 | sin->sin_addr.s_addr = *(__be32 *)(skb_network_header(skb) + |
| 532 | serr->addr_offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 533 | sin->sin_port = serr->port; |
| 534 | memset(&sin->sin_zero, 0, sizeof(sin->sin_zero)); |
Hannes Frederic Sowa | 85fbaa7 | 2013-11-23 00:46:12 +0100 | [diff] [blame] | 535 | *addr_len = sizeof(*sin); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 536 | } |
| 537 | |
| 538 | memcpy(&errhdr.ee, &serr->ee, sizeof(struct sock_extended_err)); |
| 539 | sin = &errhdr.offender; |
Willem de Bruijn | f812116 | 2015-01-15 13:18:40 -0500 | [diff] [blame] | 540 | memset(sin, 0, sizeof(*sin)); |
Willem de Bruijn | 829ae9d | 2014-11-30 22:22:34 -0500 | [diff] [blame] | 541 | |
Willem de Bruijn | c247f05 | 2015-03-07 20:33:22 -0500 | [diff] [blame] | 542 | if (ipv4_datagram_support_cmsg(sk, skb, serr->ee.ee_origin)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 543 | sin->sin_family = AF_INET; |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 544 | sin->sin_addr.s_addr = ip_hdr(skb)->saddr; |
Willem de Bruijn | f812116 | 2015-01-15 13:18:40 -0500 | [diff] [blame] | 545 | if (inet_sk(sk)->cmsg_flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Eric Dumazet | 960a262 | 2016-04-21 22:27:32 -0700 | [diff] [blame] | 556 | consume_skb(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 557 | out: |
| 558 | return err; |
| 559 | } |
| 560 | |
| 561 | |
| 562 | /* |
Eric Dumazet | 4d52cfb | 2009-06-02 00:42:16 -0700 | [diff] [blame] | 563 | * Socket option code for IP. This is the end of the line after any |
| 564 | * TCP,UDP etc options on an IP socket. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 565 | */ |
Marcelo Ricardo Leitner | baf606d | 2015-03-18 14:50:42 -0300 | [diff] [blame] | 566 | static bool setsockopt_needs_rtnl(int optname) |
| 567 | { |
| 568 | switch (optname) { |
| 569 | case IP_ADD_MEMBERSHIP: |
| 570 | case IP_ADD_SOURCE_MEMBERSHIP: |
Marcelo Ricardo Leitner | 54ff9ef | 2015-03-18 14:50:43 -0300 | [diff] [blame] | 571 | case IP_BLOCK_SOURCE: |
Marcelo Ricardo Leitner | baf606d | 2015-03-18 14:50:42 -0300 | [diff] [blame] | 572 | case IP_DROP_MEMBERSHIP: |
Marcelo Ricardo Leitner | 54ff9ef | 2015-03-18 14:50:43 -0300 | [diff] [blame] | 573 | case IP_DROP_SOURCE_MEMBERSHIP: |
| 574 | case IP_MSFILTER: |
| 575 | case IP_UNBLOCK_SOURCE: |
| 576 | case MCAST_BLOCK_SOURCE: |
| 577 | case MCAST_MSFILTER: |
Marcelo Ricardo Leitner | baf606d | 2015-03-18 14:50:42 -0300 | [diff] [blame] | 578 | case MCAST_JOIN_GROUP: |
Marcelo Ricardo Leitner | 54ff9ef | 2015-03-18 14:50:43 -0300 | [diff] [blame] | 579 | case MCAST_JOIN_SOURCE_GROUP: |
Marcelo Ricardo Leitner | baf606d | 2015-03-18 14:50:42 -0300 | [diff] [blame] | 580 | case MCAST_LEAVE_GROUP: |
Marcelo Ricardo Leitner | 54ff9ef | 2015-03-18 14:50:43 -0300 | [diff] [blame] | 581 | case MCAST_LEAVE_SOURCE_GROUP: |
| 582 | case MCAST_UNBLOCK_SOURCE: |
Marcelo Ricardo Leitner | baf606d | 2015-03-18 14:50:42 -0300 | [diff] [blame] | 583 | return true; |
| 584 | } |
| 585 | return false; |
| 586 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 587 | |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 588 | static int do_ip_setsockopt(struct sock *sk, int level, |
David S. Miller | b705884 | 2009-09-30 16:12:20 -0700 | [diff] [blame] | 589 | int optname, char __user *optval, unsigned int optlen) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 590 | { |
| 591 | struct inet_sock *inet = inet_sk(sk); |
Nikolay Borisov | 166b6b2 | 2016-02-08 23:29:22 +0200 | [diff] [blame] | 592 | struct net *net = sock_net(sk); |
Jianjun Kong | 09cb105 | 2008-11-03 00:27:11 -0800 | [diff] [blame] | 593 | int val = 0, err; |
Marcelo Ricardo Leitner | baf606d | 2015-03-18 14:50:42 -0300 | [diff] [blame] | 594 | bool needs_rtnl = setsockopt_needs_rtnl(optname); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 595 | |
Xi Wang | 0c9f79b | 2012-11-11 11:20:01 +0000 | [diff] [blame] | 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: |
Eric Dumazet | 90c337d | 2015-06-06 21:17:57 -0700 | [diff] [blame] | 613 | case IP_BIND_ADDRESS_NO_PORT: |
Xi Wang | 0c9f79b | 2012-11-11 11:20:01 +0000 | [diff] [blame] | 614 | case IP_UNICAST_IF: |
| 615 | case IP_MULTICAST_TTL: |
| 616 | case IP_MULTICAST_ALL: |
| 617 | case IP_MULTICAST_LOOP: |
| 618 | case IP_RECVORIGDSTADDR: |
Tom Herbert | ad6f939 | 2015-01-05 13:56:17 -0800 | [diff] [blame] | 619 | case IP_CHECKSUM: |
Willem de Bruijn | 70ecc24 | 2016-11-02 11:02:16 -0400 | [diff] [blame] | 620 | case IP_RECVFRAGSIZE: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Kirill Tkhai | 0526947 | 2018-03-22 12:45:12 +0300 | [diff] [blame] | 635 | if (optname == IP_ROUTER_ALERT) |
| 636 | return ip_ra_control(sk, val ? 1 : 0, NULL); |
Pavel Emelyanov | 6a9fb94 | 2007-11-05 21:32:31 -0800 | [diff] [blame] | 637 | if (ip_mroute_opt(optname)) |
Jianjun Kong | 09cb105 | 2008-11-03 00:27:11 -0800 | [diff] [blame] | 638 | return ip_mroute_setsockopt(sk, optname, optval, optlen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 639 | |
| 640 | err = 0; |
Marcelo Ricardo Leitner | baf606d | 2015-03-18 14:50:42 -0300 | [diff] [blame] | 641 | if (needs_rtnl) |
| 642 | rtnl_lock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 643 | lock_sock(sk); |
| 644 | |
| 645 | switch (optname) { |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 646 | case IP_OPTIONS: |
| 647 | { |
Eric Dumazet | f6d8bd0 | 2011-04-21 09:45:37 +0000 | [diff] [blame] | 648 | struct ip_options_rcu *old, *opt = NULL; |
| 649 | |
roel kluin | 65a1c4f | 2009-10-23 05:59:21 +0000 | [diff] [blame] | 650 | if (optlen > 40) |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 651 | goto e_inval; |
YOSHIFUJI Hideaki | 3b1e0a6 | 2008-03-26 02:26:21 +0900 | [diff] [blame] | 652 | err = ip_options_get_from_user(sock_net(sk), &opt, |
Denis V. Lunev | cb84663 | 2008-03-24 15:31:00 -0700 | [diff] [blame] | 653 | optval, optlen); |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 654 | if (err) |
| 655 | break; |
Eric Dumazet | f6d8bd0 | 2011-04-21 09:45:37 +0000 | [diff] [blame] | 656 | old = rcu_dereference_protected(inet->inet_opt, |
Hannes Frederic Sowa | 1e1d04e | 2016-04-05 17:10:15 +0200 | [diff] [blame] | 657 | lockdep_sock_is_held(sk)); |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 658 | if (inet->is_icsk) { |
| 659 | struct inet_connection_sock *icsk = inet_csk(sk); |
Eric Dumazet | dfd56b8 | 2011-12-10 09:48:31 +0000 | [diff] [blame] | 660 | #if IS_ENABLED(CONFIG_IPV6) |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 661 | if (sk->sk_family == PF_INET || |
| 662 | (!((1 << sk->sk_state) & |
| 663 | (TCPF_LISTEN | TCPF_CLOSE)) && |
Eric Dumazet | c720c7e8 | 2009-10-15 06:30:45 +0000 | [diff] [blame] | 664 | inet->inet_daddr != LOOPBACK4_IPV6)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 665 | #endif |
Eric Dumazet | f6d8bd0 | 2011-04-21 09:45:37 +0000 | [diff] [blame] | 666 | if (old) |
| 667 | icsk->icsk_ext_hdr_len -= old->opt.optlen; |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 668 | if (opt) |
Eric Dumazet | f6d8bd0 | 2011-04-21 09:45:37 +0000 | [diff] [blame] | 669 | icsk->icsk_ext_hdr_len += opt->opt.optlen; |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 670 | icsk->icsk_sync_mss(sk, icsk->icsk_pmtu_cookie); |
Eric Dumazet | dfd56b8 | 2011-12-10 09:48:31 +0000 | [diff] [blame] | 671 | #if IS_ENABLED(CONFIG_IPV6) |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 672 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 673 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 674 | } |
Eric Dumazet | f6d8bd0 | 2011-04-21 09:45:37 +0000 | [diff] [blame] | 675 | rcu_assign_pointer(inet->inet_opt, opt); |
| 676 | if (old) |
Paul E. McKenney | 605b4af | 2012-01-06 17:08:33 -0800 | [diff] [blame] | 677 | kfree_rcu(old, rcu); |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 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; |
Balazs Scheidler | e8b2dfe | 2008-11-16 19:32:39 -0800 | [diff] [blame] | 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; |
Tom Herbert | ad6f939 | 2015-01-05 13:56:17 -0800 | [diff] [blame] | 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; |
Willem de Bruijn | 70ecc24 | 2016-11-02 11:02:16 -0400 | [diff] [blame] | 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; |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 743 | case IP_TOS: /* This sets both TOS and Precedence */ |
| 744 | if (sk->sk_type == SOCK_STREAM) { |
Maciej Żenczykowski | 2c67e9a | 2011-10-22 00:07:47 -0400 | [diff] [blame] | 745 | val &= ~INET_ECN_MASK; |
| 746 | val |= inet->tos & INET_ECN_MASK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 747 | } |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 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: |
Eric Dumazet | 4d52cfb | 2009-06-02 00:42:16 -0700 | [diff] [blame] | 755 | if (optlen < 1) |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 756 | goto e_inval; |
Cong Wang | c9be4a5 | 2013-01-07 21:17:00 +0000 | [diff] [blame] | 757 | if (val != -1 && (val < 1 || val > 255)) |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 758 | goto e_inval; |
| 759 | inet->uc_ttl = val; |
| 760 | break; |
| 761 | case IP_HDRINCL: |
| 762 | if (sk->sk_type != SOCK_RAW) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 763 | err = -ENOPROTOOPT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 764 | break; |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 765 | } |
| 766 | inet->hdrincl = val ? 1 : 0; |
| 767 | break; |
Jiri Olsa | 7b2ff18 | 2010-06-15 01:07:31 +0000 | [diff] [blame] | 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; |
Eric Dumazet | 90c337d | 2015-06-06 21:17:57 -0700 | [diff] [blame] | 775 | case IP_BIND_ADDRESS_NO_PORT: |
| 776 | inet->bind_address_no_port = val ? 1 : 0; |
| 777 | break; |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 778 | case IP_MTU_DISCOVER: |
Hannes Frederic Sowa | 1b34657 | 2014-02-26 01:20:42 +0100 | [diff] [blame] | 779 | if (val < IP_PMTUDISC_DONT || val > IP_PMTUDISC_OMIT) |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 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; |
Eric Dumazet | 4d52cfb | 2009-06-02 00:42:16 -0700 | [diff] [blame] | 791 | if (optlen < 1) |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 792 | goto e_inval; |
Jianjun Kong | 09cb105 | 2008-11-03 00:27:11 -0800 | [diff] [blame] | 793 | if (val == -1) |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 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: |
Eric Dumazet | 4d52cfb | 2009-06-02 00:42:16 -0700 | [diff] [blame] | 800 | if (optlen < 1) |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 801 | goto e_inval; |
| 802 | inet->mc_loop = !!val; |
| 803 | break; |
Erich E. Hoover | 76e2105 | 2012-02-08 09:11:07 +0000 | [diff] [blame] | 804 | case IP_UNICAST_IF: |
| 805 | { |
| 806 | struct net_device *dev = NULL; |
| 807 | int ifindex; |
David Ahern | 9515a2e | 2018-01-24 19:37:38 -0800 | [diff] [blame] | 808 | int midx; |
Erich E. Hoover | 76e2105 | 2012-02-08 09:11:07 +0000 | [diff] [blame] | 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; |
David Ahern | 9515a2e | 2018-01-24 19:37:38 -0800 | [diff] [blame] | 824 | |
| 825 | midx = l3mdev_master_ifindex(dev); |
Erich E. Hoover | 76e2105 | 2012-02-08 09:11:07 +0000 | [diff] [blame] | 826 | dev_put(dev); |
| 827 | |
| 828 | err = -EINVAL; |
David Ahern | 9515a2e | 2018-01-24 19:37:38 -0800 | [diff] [blame] | 829 | if (sk->sk_bound_dev_if && |
| 830 | (!midx || midx != sk->sk_bound_dev_if)) |
Erich E. Hoover | 76e2105 | 2012-02-08 09:11:07 +0000 | [diff] [blame] | 831 | break; |
| 832 | |
| 833 | inet->uc_index = ifindex; |
| 834 | err = 0; |
| 835 | break; |
| 836 | } |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 837 | case IP_MULTICAST_IF: |
| 838 | { |
| 839 | struct ip_mreqn mreq; |
| 840 | struct net_device *dev = NULL; |
David Ahern | 7bb387c | 2016-12-29 15:39:37 -0800 | [diff] [blame] | 841 | int midx; |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 842 | |
| 843 | if (sk->sk_type == SOCK_STREAM) |
| 844 | goto e_inval; |
| 845 | /* |
| 846 | * Check the arguments are allowable |
| 847 | */ |
| 848 | |
Shan Wei | 0915921 | 2009-09-22 15:41:10 +0000 | [diff] [blame] | 849 | if (optlen < sizeof(struct in_addr)) |
| 850 | goto e_inval; |
| 851 | |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 852 | err = -EFAULT; |
| 853 | if (optlen >= sizeof(struct ip_mreqn)) { |
Jianjun Kong | 09cb105 | 2008-11-03 00:27:11 -0800 | [diff] [blame] | 854 | if (copy_from_user(&mreq, optval, sizeof(mreq))) |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 855 | break; |
| 856 | } else { |
| 857 | memset(&mreq, 0, sizeof(mreq)); |
Jiri Pirko | 3a084dd | 2012-05-03 22:37:45 +0000 | [diff] [blame] | 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 | } |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 867 | } |
| 868 | |
| 869 | if (!mreq.imr_ifindex) { |
Al Viro | e6f1ceb | 2008-03-17 22:44:53 -0700 | [diff] [blame] | 870 | if (mreq.imr_address.s_addr == htonl(INADDR_ANY)) { |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 871 | inet->mc_index = 0; |
| 872 | inet->mc_addr = 0; |
| 873 | err = 0; |
| 874 | break; |
| 875 | } |
YOSHIFUJI Hideaki | 3b1e0a6 | 2008-03-26 02:26:21 +0900 | [diff] [blame] | 876 | dev = ip_dev_find(sock_net(sk), mreq.imr_address.s_addr); |
Eric Dumazet | 55b8050 | 2009-10-19 06:41:58 +0000 | [diff] [blame] | 877 | if (dev) |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 878 | mreq.imr_ifindex = dev->ifindex; |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 879 | } else |
Eric Dumazet | 55b8050 | 2009-10-19 06:41:58 +0000 | [diff] [blame] | 880 | dev = dev_get_by_index(sock_net(sk), mreq.imr_ifindex); |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 881 | |
| 882 | |
| 883 | err = -EADDRNOTAVAIL; |
| 884 | if (!dev) |
| 885 | break; |
David Ahern | 7bb387c | 2016-12-29 15:39:37 -0800 | [diff] [blame] | 886 | |
| 887 | midx = l3mdev_master_ifindex(dev); |
| 888 | |
Eric Dumazet | 55b8050 | 2009-10-19 06:41:58 +0000 | [diff] [blame] | 889 | dev_put(dev); |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 890 | |
| 891 | err = -EINVAL; |
| 892 | if (sk->sk_bound_dev_if && |
David Ahern | 7bb387c | 2016-12-29 15:39:37 -0800 | [diff] [blame] | 893 | mreq.imr_ifindex != sk->sk_bound_dev_if && |
| 894 | (!midx || midx != sk->sk_bound_dev_if)) |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 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 | |
Flavio Leitner | a96fb49 | 2007-08-24 22:16:39 -0700 | [diff] [blame] | 908 | err = -EPROTO; |
| 909 | if (inet_sk(sk)->is_icsk) |
| 910 | break; |
| 911 | |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 912 | if (optlen < sizeof(struct ip_mreq)) |
| 913 | goto e_inval; |
| 914 | err = -EFAULT; |
| 915 | if (optlen >= sizeof(struct ip_mreqn)) { |
Jianjun Kong | 09cb105 | 2008-11-03 00:27:11 -0800 | [diff] [blame] | 916 | if (copy_from_user(&mreq, optval, sizeof(mreq))) |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 917 | break; |
| 918 | } else { |
| 919 | memset(&mreq, 0, sizeof(mreq)); |
Jianjun Kong | 09cb105 | 2008-11-03 00:27:11 -0800 | [diff] [blame] | 920 | if (copy_from_user(&mreq, optval, sizeof(struct ip_mreq))) |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 921 | break; |
| 922 | } |
| 923 | |
| 924 | if (optname == IP_ADD_MEMBERSHIP) |
Marcelo Ricardo Leitner | 54ff9ef | 2015-03-18 14:50:43 -0300 | [diff] [blame] | 925 | err = ip_mc_join_group(sk, &mreq); |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 926 | else |
Marcelo Ricardo Leitner | 54ff9ef | 2015-03-18 14:50:43 -0300 | [diff] [blame] | 927 | err = ip_mc_leave_group(sk, &mreq); |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 928 | break; |
| 929 | } |
| 930 | case IP_MSFILTER: |
| 931 | { |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 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 | } |
Al Viro | a2c841d | 2017-05-13 18:26:06 -0400 | [diff] [blame] | 940 | msf = memdup_user(optval, optlen); |
| 941 | if (IS_ERR(msf)) { |
| 942 | err = PTR_ERR(msf); |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 943 | break; |
| 944 | } |
| 945 | /* numsrc >= (1G-4) overflow in 32 bits */ |
| 946 | if (msf->imsf_numsrc >= 0x3ffffffcU || |
Nikolay Borisov | 166b6b2 | 2016-02-08 23:29:22 +0200 | [diff] [blame] | 947 | msf->imsf_numsrc > net->ipv4.sysctl_igmp_max_msf) { |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 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; |
Hangbin Liu | 6e2059b | 2018-07-10 22:41:26 +0800 | [diff] [blame^] | 987 | err = ip_mc_join_group_ssm(sk, &mreq, MCAST_INCLUDE); |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 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) |
Marcelo Ricardo Leitner | 54ff9ef | 2015-03-18 14:50:43 -0300 | [diff] [blame] | 1019 | err = ip_mc_join_group(sk, &mreq); |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1020 | else |
Marcelo Ricardo Leitner | 54ff9ef | 2015-03-18 14:50:43 -0300 | [diff] [blame] | 1021 | err = ip_mc_leave_group(sk, &mreq); |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 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; |
Hangbin Liu | 6e2059b | 2018-07-10 22:41:26 +0800 | [diff] [blame^] | 1064 | err = ip_mc_join_group_ssm(sk, &mreq, MCAST_INCLUDE); |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 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 | { |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 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 | } |
Al Viro | a2c841d | 2017-05-13 18:26:06 -0400 | [diff] [blame] | 1091 | gsf = memdup_user(optval, optlen); |
| 1092 | if (IS_ERR(gsf)) { |
| 1093 | err = PTR_ERR(gsf); |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1094 | break; |
| 1095 | } |
Eric Dumazet | 4d52cfb | 2009-06-02 00:42:16 -0700 | [diff] [blame] | 1096 | |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1097 | /* numsrc >= (4G-140)/128 overflow in 32 bits */ |
| 1098 | if (gsf->gf_numsrc >= 0x1ffffff || |
Nikolay Borisov | 166b6b2 | 2016-02-08 23:29:22 +0200 | [diff] [blame] | 1099 | gsf->gf_numsrc > net->ipv4.sysctl_igmp_max_msf) { |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 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); |
Jianjun Kong | 09cb105 | 2008-11-03 00:27:11 -0800 | [diff] [blame] | 1108 | msf = kmalloc(msize, GFP_KERNEL); |
Stephen Hemminger | cfcabdc | 2007-10-09 01:59:42 -0700 | [diff] [blame] | 1109 | if (!msf) { |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 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; |
Eric Dumazet | 4d52cfb | 2009-06-02 00:42:16 -0700 | [diff] [blame] | 1124 | for (i = 0; i < gsf->gf_numsrc; ++i) { |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 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); |
Eric Dumazet | 4d52cfb | 2009-06-02 00:42:16 -0700 | [diff] [blame] | 1135 | mc_msf_out: |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1136 | kfree(msf); |
| 1137 | kfree(gsf); |
| 1138 | break; |
| 1139 | } |
Nivedita Singhvi | f771bef | 2009-05-28 07:00:46 +0000 | [diff] [blame] | 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; |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1147 | |
| 1148 | case IP_FREEBIND: |
Eric Dumazet | 4d52cfb | 2009-06-02 00:42:16 -0700 | [diff] [blame] | 1149 | if (optlen < 1) |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1150 | goto e_inval; |
| 1151 | inet->freebind = !!val; |
| 1152 | break; |
| 1153 | |
| 1154 | case IP_IPSEC_POLICY: |
| 1155 | case IP_XFRM_POLICY: |
| 1156 | err = -EPERM; |
Eric W. Biederman | 52e804c | 2012-11-16 03:03:05 +0000 | [diff] [blame] | 1157 | if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN)) |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1158 | break; |
| 1159 | err = xfrm_user_policy(sk, optname, optval, optlen); |
| 1160 | break; |
| 1161 | |
KOVACS Krisztian | f5715ae | 2008-10-01 07:30:02 -0700 | [diff] [blame] | 1162 | case IP_TRANSPARENT: |
Eric W. Biederman | 52e804c | 2012-11-16 03:03:05 +0000 | [diff] [blame] | 1163 | if (!!val && !ns_capable(sock_net(sk)->user_ns, CAP_NET_RAW) && |
| 1164 | !ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN)) { |
KOVACS Krisztian | f5715ae | 2008-10-01 07:30:02 -0700 | [diff] [blame] | 1165 | err = -EPERM; |
| 1166 | break; |
| 1167 | } |
| 1168 | if (optlen < 1) |
| 1169 | goto e_inval; |
| 1170 | inet->transparent = !!val; |
| 1171 | break; |
| 1172 | |
Stephen Hemminger | d218d11 | 2010-01-11 16:28:01 -0800 | [diff] [blame] | 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 | |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1181 | default: |
| 1182 | err = -ENOPROTOOPT; |
| 1183 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1184 | } |
| 1185 | release_sock(sk); |
Marcelo Ricardo Leitner | baf606d | 2015-03-18 14:50:42 -0300 | [diff] [blame] | 1186 | if (needs_rtnl) |
| 1187 | rtnl_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1188 | return err; |
| 1189 | |
| 1190 | e_inval: |
| 1191 | release_sock(sk); |
Marcelo Ricardo Leitner | baf606d | 2015-03-18 14:50:42 -0300 | [diff] [blame] | 1192 | if (needs_rtnl) |
| 1193 | rtnl_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1194 | return -EINVAL; |
| 1195 | } |
| 1196 | |
Eric Dumazet | f84af32 | 2010-04-28 15:31:51 -0700 | [diff] [blame] | 1197 | /** |
Willem de Bruijn | 829ae9d | 2014-11-30 22:22:34 -0500 | [diff] [blame] | 1198 | * ipv4_pktinfo_prepare - transfer some info from rtable to skb |
Eric Dumazet | f84af32 | 2010-04-28 15:31:51 -0700 | [diff] [blame] | 1199 | * @sk: socket |
| 1200 | * @skb: buffer |
| 1201 | * |
David S. Miller | 35ebf65 | 2012-06-28 03:59:11 -0700 | [diff] [blame] | 1202 | * To support IP_CMSG_PKTINFO option, we store rt_iif and specific |
| 1203 | * destination in skb->cb[] before dst drop. |
stephen hemminger | 8e3bff9 | 2013-12-08 12:15:44 -0800 | [diff] [blame] | 1204 | * This way, receiver doesn't make cache line misses to read rtable. |
Eric Dumazet | f84af32 | 2010-04-28 15:31:51 -0700 | [diff] [blame] | 1205 | */ |
Shawn Bohrer | fbf8866 | 2013-10-07 11:01:40 -0500 | [diff] [blame] | 1206 | void ipv4_pktinfo_prepare(const struct sock *sk, struct sk_buff *skb) |
Eric Dumazet | f84af32 | 2010-04-28 15:31:51 -0700 | [diff] [blame] | 1207 | { |
Eric Dumazet | d826eb1 | 2011-11-09 07:24:35 +0000 | [diff] [blame] | 1208 | struct in_pktinfo *pktinfo = PKTINFO_SKB_CB(skb); |
Hannes Frederic Sowa | 4b261c75 | 2014-01-20 03:43:08 +0100 | [diff] [blame] | 1209 | bool prepare = (inet_sk(sk)->cmsg_flags & IP_CMSG_PKTINFO) || |
| 1210 | ipv6_sk_rxinfo(sk); |
Eric Dumazet | d826eb1 | 2011-11-09 07:24:35 +0000 | [diff] [blame] | 1211 | |
Hannes Frederic Sowa | 4b261c75 | 2014-01-20 03:43:08 +0100 | [diff] [blame] | 1212 | if (prepare && skb_rtable(skb)) { |
David Ahern | 0b922b7 | 2016-05-10 11:19:51 -0700 | [diff] [blame] | 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 |
Wei Zhang | f0c16ba | 2016-12-29 16:45:04 +0800 | [diff] [blame] | 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). |
David Ahern | 0b922b7 | 2016-05-10 11:19:51 -0700 | [diff] [blame] | 1221 | */ |
David Ahern | cbea8f0 | 2017-09-13 17:11:37 -0700 | [diff] [blame] | 1222 | struct rtable *rt = skb_rtable(skb); |
| 1223 | bool l3slave = ipv4_l3mdev_skb(IPCB(skb)->flags); |
| 1224 | |
| 1225 | if (pktinfo->ipi_ifindex == LOOPBACK_IFINDEX) |
Wei Zhang | f0c16ba | 2016-12-29 16:45:04 +0800 | [diff] [blame] | 1226 | pktinfo->ipi_ifindex = inet_iif(skb); |
David Ahern | cbea8f0 | 2017-09-13 17:11:37 -0700 | [diff] [blame] | 1227 | else if (l3slave && rt && rt->rt_iif) |
| 1228 | pktinfo->ipi_ifindex = rt->rt_iif; |
Wei Zhang | f0c16ba | 2016-12-29 16:45:04 +0800 | [diff] [blame] | 1229 | |
David S. Miller | 35ebf65 | 2012-06-28 03:59:11 -0700 | [diff] [blame] | 1230 | pktinfo->ipi_spec_dst.s_addr = fib_compute_spec_dst(skb); |
Eric Dumazet | d826eb1 | 2011-11-09 07:24:35 +0000 | [diff] [blame] | 1231 | } else { |
| 1232 | pktinfo->ipi_ifindex = 0; |
| 1233 | pktinfo->ipi_spec_dst.s_addr = 0; |
| 1234 | } |
Paolo Abeni | 61a1030 | 2017-08-03 18:07:07 +0200 | [diff] [blame] | 1235 | skb_dst_drop(skb); |
Eric Dumazet | f84af32 | 2010-04-28 15:31:51 -0700 | [diff] [blame] | 1236 | } |
Eric Dumazet | f84af32 | 2010-04-28 15:31:51 -0700 | [diff] [blame] | 1237 | |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 1238 | int ip_setsockopt(struct sock *sk, int level, |
David S. Miller | b705884 | 2009-09-30 16:12:20 -0700 | [diff] [blame] | 1239 | int optname, char __user *optval, unsigned int optlen) |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 1240 | { |
| 1241 | int err; |
| 1242 | |
| 1243 | if (level != SOL_IP) |
| 1244 | return -ENOPROTOOPT; |
| 1245 | |
| 1246 | err = do_ip_setsockopt(sk, level, optname, optval, optlen); |
Alexei Starovoitov | d2ba09c | 2018-05-21 19:22:30 -0700 | [diff] [blame] | 1247 | #ifdef CONFIG_BPFILTER |
| 1248 | if (optname >= BPFILTER_IPT_SO_SET_REPLACE && |
| 1249 | optname < BPFILTER_IPT_SET_MAX) |
| 1250 | err = bpfilter_ip_set_sockopt(sk, optname, optval, optlen); |
| 1251 | #endif |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 1252 | #ifdef CONFIG_NETFILTER |
| 1253 | /* we need to exclude all possible ENOPROTOOPTs except default case */ |
| 1254 | if (err == -ENOPROTOOPT && optname != IP_HDRINCL && |
Pavel Emelyanov | 6a9fb94 | 2007-11-05 21:32:31 -0800 | [diff] [blame] | 1255 | optname != IP_IPSEC_POLICY && |
| 1256 | optname != IP_XFRM_POLICY && |
Paolo Abeni | 3f34cfa | 2018-01-30 19:01:40 +0100 | [diff] [blame] | 1257 | !ip_mroute_opt(optname)) |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 1258 | err = nf_setsockopt(sk, PF_INET, optname, optval, optlen); |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 1259 | #endif |
| 1260 | return err; |
| 1261 | } |
Eric Dumazet | 4d52cfb | 2009-06-02 00:42:16 -0700 | [diff] [blame] | 1262 | EXPORT_SYMBOL(ip_setsockopt); |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 1263 | |
| 1264 | #ifdef CONFIG_COMPAT |
Arnaldo Carvalho de Melo | 543d9cf | 2006-03-20 22:48:35 -0800 | [diff] [blame] | 1265 | int compat_ip_setsockopt(struct sock *sk, int level, int optname, |
David S. Miller | b705884 | 2009-09-30 16:12:20 -0700 | [diff] [blame] | 1266 | char __user *optval, unsigned int optlen) |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 1267 | { |
| 1268 | int err; |
| 1269 | |
| 1270 | if (level != SOL_IP) |
| 1271 | return -ENOPROTOOPT; |
| 1272 | |
David L Stevens | dae5029 | 2008-04-27 01:06:07 -0700 | [diff] [blame] | 1273 | if (optname >= MCAST_JOIN_GROUP && optname <= MCAST_MSFILTER) |
| 1274 | return compat_mc_setsockopt(sk, level, optname, optval, optlen, |
| 1275 | ip_setsockopt); |
| 1276 | |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 1277 | err = do_ip_setsockopt(sk, level, optname, optval, optlen); |
| 1278 | #ifdef CONFIG_NETFILTER |
| 1279 | /* we need to exclude all possible ENOPROTOOPTs except default case */ |
| 1280 | if (err == -ENOPROTOOPT && optname != IP_HDRINCL && |
Pavel Emelyanov | 6a9fb94 | 2007-11-05 21:32:31 -0800 | [diff] [blame] | 1281 | optname != IP_IPSEC_POLICY && |
| 1282 | optname != IP_XFRM_POLICY && |
Paolo Abeni | 3f34cfa | 2018-01-30 19:01:40 +0100 | [diff] [blame] | 1283 | !ip_mroute_opt(optname)) |
| 1284 | err = compat_nf_setsockopt(sk, PF_INET, optname, optval, |
| 1285 | optlen); |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 1286 | #endif |
| 1287 | return err; |
| 1288 | } |
Arnaldo Carvalho de Melo | 543d9cf | 2006-03-20 22:48:35 -0800 | [diff] [blame] | 1289 | EXPORT_SYMBOL(compat_ip_setsockopt); |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 1290 | #endif |
| 1291 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1292 | /* |
Eric Dumazet | 4d52cfb | 2009-06-02 00:42:16 -0700 | [diff] [blame] | 1293 | * Get the options. Note for future reference. The GET of IP options gets |
| 1294 | * the _received_ ones. The set sets the _sent_ ones. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1295 | */ |
| 1296 | |
WANG Cong | 87e9f03 | 2015-11-03 15:41:16 -0800 | [diff] [blame] | 1297 | static bool getsockopt_needs_rtnl(int optname) |
| 1298 | { |
| 1299 | switch (optname) { |
| 1300 | case IP_MSFILTER: |
| 1301 | case MCAST_MSFILTER: |
| 1302 | return true; |
| 1303 | } |
| 1304 | return false; |
| 1305 | } |
| 1306 | |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 1307 | static int do_ip_getsockopt(struct sock *sk, int level, int optname, |
Eric Dumazet | 95c9617 | 2012-04-15 05:58:06 +0000 | [diff] [blame] | 1308 | char __user *optval, int __user *optlen, unsigned int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1309 | { |
| 1310 | struct inet_sock *inet = inet_sk(sk); |
WANG Cong | 87e9f03 | 2015-11-03 15:41:16 -0800 | [diff] [blame] | 1311 | bool needs_rtnl = getsockopt_needs_rtnl(optname); |
| 1312 | int val, err = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1313 | int len; |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 1314 | |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1315 | if (level != SOL_IP) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1316 | return -EOPNOTSUPP; |
| 1317 | |
Pavel Emelyanov | 6a9fb94 | 2007-11-05 21:32:31 -0800 | [diff] [blame] | 1318 | if (ip_mroute_opt(optname)) |
Jianjun Kong | 09cb105 | 2008-11-03 00:27:11 -0800 | [diff] [blame] | 1319 | return ip_mroute_getsockopt(sk, optname, optval, optlen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1320 | |
Jianjun Kong | 09cb105 | 2008-11-03 00:27:11 -0800 | [diff] [blame] | 1321 | if (get_user(len, optlen)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1322 | return -EFAULT; |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1323 | if (len < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1324 | return -EINVAL; |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 1325 | |
WANG Cong | 87e9f03 | 2015-11-03 15:41:16 -0800 | [diff] [blame] | 1326 | if (needs_rtnl) |
| 1327 | rtnl_lock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1328 | lock_sock(sk); |
| 1329 | |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1330 | switch (optname) { |
| 1331 | case IP_OPTIONS: |
| 1332 | { |
| 1333 | unsigned char optbuf[sizeof(struct ip_options)+40]; |
Eric Dumazet | f6d8bd0 | 2011-04-21 09:45:37 +0000 | [diff] [blame] | 1334 | struct ip_options *opt = (struct ip_options *)optbuf; |
| 1335 | struct ip_options_rcu *inet_opt; |
| 1336 | |
| 1337 | inet_opt = rcu_dereference_protected(inet->inet_opt, |
Hannes Frederic Sowa | 1e1d04e | 2016-04-05 17:10:15 +0200 | [diff] [blame] | 1338 | lockdep_sock_is_held(sk)); |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1339 | opt->optlen = 0; |
Eric Dumazet | f6d8bd0 | 2011-04-21 09:45:37 +0000 | [diff] [blame] | 1340 | if (inet_opt) |
| 1341 | memcpy(optbuf, &inet_opt->opt, |
| 1342 | sizeof(struct ip_options) + |
| 1343 | inet_opt->opt.optlen); |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1344 | release_sock(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1345 | |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1346 | if (opt->optlen == 0) |
| 1347 | return put_user(0, optlen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1348 | |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1349 | ip_options_undo(opt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1350 | |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1351 | len = min_t(unsigned int, len, opt->optlen); |
| 1352 | if (put_user(len, optlen)) |
| 1353 | return -EFAULT; |
| 1354 | if (copy_to_user(optval, opt->__data, len)) |
| 1355 | return -EFAULT; |
| 1356 | return 0; |
| 1357 | } |
| 1358 | case IP_PKTINFO: |
| 1359 | val = (inet->cmsg_flags & IP_CMSG_PKTINFO) != 0; |
| 1360 | break; |
| 1361 | case IP_RECVTTL: |
| 1362 | val = (inet->cmsg_flags & IP_CMSG_TTL) != 0; |
| 1363 | break; |
| 1364 | case IP_RECVTOS: |
| 1365 | val = (inet->cmsg_flags & IP_CMSG_TOS) != 0; |
| 1366 | break; |
| 1367 | case IP_RECVOPTS: |
| 1368 | val = (inet->cmsg_flags & IP_CMSG_RECVOPTS) != 0; |
| 1369 | break; |
| 1370 | case IP_RETOPTS: |
| 1371 | val = (inet->cmsg_flags & IP_CMSG_RETOPTS) != 0; |
| 1372 | break; |
| 1373 | case IP_PASSSEC: |
| 1374 | val = (inet->cmsg_flags & IP_CMSG_PASSSEC) != 0; |
| 1375 | break; |
Balazs Scheidler | e8b2dfe | 2008-11-16 19:32:39 -0800 | [diff] [blame] | 1376 | case IP_RECVORIGDSTADDR: |
| 1377 | val = (inet->cmsg_flags & IP_CMSG_ORIGDSTADDR) != 0; |
| 1378 | break; |
Tom Herbert | ad6f939 | 2015-01-05 13:56:17 -0800 | [diff] [blame] | 1379 | case IP_CHECKSUM: |
| 1380 | val = (inet->cmsg_flags & IP_CMSG_CHECKSUM) != 0; |
| 1381 | break; |
Willem de Bruijn | 70ecc24 | 2016-11-02 11:02:16 -0400 | [diff] [blame] | 1382 | case IP_RECVFRAGSIZE: |
| 1383 | val = (inet->cmsg_flags & IP_CMSG_RECVFRAGSIZE) != 0; |
| 1384 | break; |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1385 | case IP_TOS: |
| 1386 | val = inet->tos; |
| 1387 | break; |
| 1388 | case IP_TTL: |
Nikolay Borisov | fa50d97 | 2016-02-15 12:11:27 +0200 | [diff] [blame] | 1389 | { |
| 1390 | struct net *net = sock_net(sk); |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1391 | val = (inet->uc_ttl == -1 ? |
Nikolay Borisov | fa50d97 | 2016-02-15 12:11:27 +0200 | [diff] [blame] | 1392 | net->ipv4.sysctl_ip_default_ttl : |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1393 | inet->uc_ttl); |
| 1394 | break; |
Nikolay Borisov | fa50d97 | 2016-02-15 12:11:27 +0200 | [diff] [blame] | 1395 | } |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1396 | case IP_HDRINCL: |
| 1397 | val = inet->hdrincl; |
| 1398 | break; |
Michael Kerrisk | a89b476 | 2010-09-10 20:26:56 +0000 | [diff] [blame] | 1399 | case IP_NODEFRAG: |
| 1400 | val = inet->nodefrag; |
| 1401 | break; |
Eric Dumazet | 90c337d | 2015-06-06 21:17:57 -0700 | [diff] [blame] | 1402 | case IP_BIND_ADDRESS_NO_PORT: |
| 1403 | val = inet->bind_address_no_port; |
| 1404 | break; |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1405 | case IP_MTU_DISCOVER: |
| 1406 | val = inet->pmtudisc; |
| 1407 | break; |
| 1408 | case IP_MTU: |
| 1409 | { |
| 1410 | struct dst_entry *dst; |
| 1411 | val = 0; |
| 1412 | dst = sk_dst_get(sk); |
| 1413 | if (dst) { |
| 1414 | val = dst_mtu(dst); |
| 1415 | dst_release(dst); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1416 | } |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1417 | if (!val) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1418 | release_sock(sk); |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1419 | return -ENOTCONN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1420 | } |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1421 | break; |
| 1422 | } |
| 1423 | case IP_RECVERR: |
| 1424 | val = inet->recverr; |
| 1425 | break; |
| 1426 | case IP_MULTICAST_TTL: |
| 1427 | val = inet->mc_ttl; |
| 1428 | break; |
| 1429 | case IP_MULTICAST_LOOP: |
| 1430 | val = inet->mc_loop; |
| 1431 | break; |
Erich E. Hoover | 76e2105 | 2012-02-08 09:11:07 +0000 | [diff] [blame] | 1432 | case IP_UNICAST_IF: |
| 1433 | val = (__force int)htonl((__u32) inet->uc_index); |
| 1434 | break; |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1435 | case IP_MULTICAST_IF: |
| 1436 | { |
| 1437 | struct in_addr addr; |
| 1438 | len = min_t(unsigned int, len, sizeof(struct in_addr)); |
| 1439 | addr.s_addr = inet->mc_addr; |
| 1440 | release_sock(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1441 | |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1442 | if (put_user(len, optlen)) |
| 1443 | return -EFAULT; |
| 1444 | if (copy_to_user(optval, &addr, len)) |
| 1445 | return -EFAULT; |
| 1446 | return 0; |
| 1447 | } |
| 1448 | case IP_MSFILTER: |
| 1449 | { |
| 1450 | struct ip_msfilter msf; |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1451 | |
| 1452 | if (len < IP_MSFILTER_SIZE(0)) { |
WANG Cong | 87e9f03 | 2015-11-03 15:41:16 -0800 | [diff] [blame] | 1453 | err = -EINVAL; |
| 1454 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1455 | } |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1456 | if (copy_from_user(&msf, optval, IP_MSFILTER_SIZE(0))) { |
WANG Cong | 87e9f03 | 2015-11-03 15:41:16 -0800 | [diff] [blame] | 1457 | err = -EFAULT; |
| 1458 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1459 | } |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1460 | err = ip_mc_msfget(sk, &msf, |
| 1461 | (struct ip_msfilter __user *)optval, optlen); |
WANG Cong | 87e9f03 | 2015-11-03 15:41:16 -0800 | [diff] [blame] | 1462 | goto out; |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1463 | } |
| 1464 | case MCAST_MSFILTER: |
| 1465 | { |
| 1466 | struct group_filter gsf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1467 | |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1468 | if (len < GROUP_FILTER_SIZE(0)) { |
WANG Cong | 87e9f03 | 2015-11-03 15:41:16 -0800 | [diff] [blame] | 1469 | err = -EINVAL; |
| 1470 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1471 | } |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1472 | if (copy_from_user(&gsf, optval, GROUP_FILTER_SIZE(0))) { |
WANG Cong | 87e9f03 | 2015-11-03 15:41:16 -0800 | [diff] [blame] | 1473 | err = -EFAULT; |
| 1474 | goto out; |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1475 | } |
| 1476 | err = ip_mc_gsfget(sk, &gsf, |
Eric Dumazet | 4d52cfb | 2009-06-02 00:42:16 -0700 | [diff] [blame] | 1477 | (struct group_filter __user *)optval, |
| 1478 | optlen); |
WANG Cong | 87e9f03 | 2015-11-03 15:41:16 -0800 | [diff] [blame] | 1479 | goto out; |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1480 | } |
Nivedita Singhvi | f771bef | 2009-05-28 07:00:46 +0000 | [diff] [blame] | 1481 | case IP_MULTICAST_ALL: |
| 1482 | val = inet->mc_all; |
| 1483 | break; |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1484 | case IP_PKTOPTIONS: |
| 1485 | { |
| 1486 | struct msghdr msg; |
| 1487 | |
| 1488 | release_sock(sk); |
| 1489 | |
| 1490 | if (sk->sk_type != SOCK_STREAM) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1491 | return -ENOPROTOOPT; |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1492 | |
Karoly Kemeny | c54a5e0 | 2014-07-27 12:29:07 +0200 | [diff] [blame] | 1493 | msg.msg_control = (__force void *) optval; |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1494 | msg.msg_controllen = len; |
Daniel Baluta | dd23198e | 2011-08-07 22:31:07 -0700 | [diff] [blame] | 1495 | msg.msg_flags = flags; |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1496 | |
| 1497 | if (inet->cmsg_flags & IP_CMSG_PKTINFO) { |
| 1498 | struct in_pktinfo info; |
| 1499 | |
Eric Dumazet | c720c7e8 | 2009-10-15 06:30:45 +0000 | [diff] [blame] | 1500 | info.ipi_addr.s_addr = inet->inet_rcv_saddr; |
| 1501 | info.ipi_spec_dst.s_addr = inet->inet_rcv_saddr; |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1502 | info.ipi_ifindex = inet->mc_index; |
| 1503 | put_cmsg(&msg, SOL_IP, IP_PKTINFO, sizeof(info), &info); |
| 1504 | } |
| 1505 | if (inet->cmsg_flags & IP_CMSG_TTL) { |
| 1506 | int hlim = inet->mc_ttl; |
| 1507 | put_cmsg(&msg, SOL_IP, IP_TTL, sizeof(hlim), &hlim); |
| 1508 | } |
Jiri Benc | 4c507d2 | 2012-02-09 09:35:49 +0000 | [diff] [blame] | 1509 | if (inet->cmsg_flags & IP_CMSG_TOS) { |
| 1510 | int tos = inet->rcv_tos; |
| 1511 | put_cmsg(&msg, SOL_IP, IP_TOS, sizeof(tos), &tos); |
| 1512 | } |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1513 | len -= msg.msg_controllen; |
| 1514 | return put_user(len, optlen); |
| 1515 | } |
| 1516 | case IP_FREEBIND: |
| 1517 | val = inet->freebind; |
| 1518 | break; |
KOVACS Krisztian | f5715ae | 2008-10-01 07:30:02 -0700 | [diff] [blame] | 1519 | case IP_TRANSPARENT: |
| 1520 | val = inet->transparent; |
| 1521 | break; |
Stephen Hemminger | d218d11 | 2010-01-11 16:28:01 -0800 | [diff] [blame] | 1522 | case IP_MINTTL: |
| 1523 | val = inet->min_ttl; |
| 1524 | break; |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1525 | default: |
| 1526 | release_sock(sk); |
| 1527 | return -ENOPROTOOPT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1528 | } |
| 1529 | release_sock(sk); |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 1530 | |
Eric Dumazet | 4d52cfb | 2009-06-02 00:42:16 -0700 | [diff] [blame] | 1531 | if (len < sizeof(int) && len > 0 && val >= 0 && val <= 255) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1532 | unsigned char ucval = (unsigned char)val; |
| 1533 | len = 1; |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1534 | if (put_user(len, optlen)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1535 | return -EFAULT; |
Jianjun Kong | 09cb105 | 2008-11-03 00:27:11 -0800 | [diff] [blame] | 1536 | if (copy_to_user(optval, &ucval, 1)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1537 | return -EFAULT; |
| 1538 | } else { |
| 1539 | len = min_t(unsigned int, sizeof(int), len); |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1540 | if (put_user(len, optlen)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1541 | return -EFAULT; |
Jianjun Kong | 09cb105 | 2008-11-03 00:27:11 -0800 | [diff] [blame] | 1542 | if (copy_to_user(optval, &val, len)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1543 | return -EFAULT; |
| 1544 | } |
| 1545 | return 0; |
WANG Cong | 87e9f03 | 2015-11-03 15:41:16 -0800 | [diff] [blame] | 1546 | |
| 1547 | out: |
| 1548 | release_sock(sk); |
| 1549 | if (needs_rtnl) |
| 1550 | rtnl_unlock(); |
| 1551 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1552 | } |
| 1553 | |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 1554 | int ip_getsockopt(struct sock *sk, int level, |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1555 | int optname, char __user *optval, int __user *optlen) |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 1556 | { |
| 1557 | int err; |
| 1558 | |
Daniel Baluta | dd23198e | 2011-08-07 22:31:07 -0700 | [diff] [blame] | 1559 | err = do_ip_getsockopt(sk, level, optname, optval, optlen, 0); |
Alexei Starovoitov | d2ba09c | 2018-05-21 19:22:30 -0700 | [diff] [blame] | 1560 | #ifdef CONFIG_BPFILTER |
| 1561 | if (optname >= BPFILTER_IPT_SO_GET_INFO && |
| 1562 | optname < BPFILTER_IPT_GET_MAX) |
| 1563 | err = bpfilter_ip_get_sockopt(sk, optname, optval, optlen); |
| 1564 | #endif |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 1565 | #ifdef CONFIG_NETFILTER |
| 1566 | /* we need to exclude all possible ENOPROTOOPTs except default case */ |
Pavel Emelyanov | 6a9fb94 | 2007-11-05 21:32:31 -0800 | [diff] [blame] | 1567 | if (err == -ENOPROTOOPT && optname != IP_PKTOPTIONS && |
| 1568 | !ip_mroute_opt(optname)) { |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 1569 | int len; |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 1570 | |
Jianjun Kong | 09cb105 | 2008-11-03 00:27:11 -0800 | [diff] [blame] | 1571 | if (get_user(len, optlen)) |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 1572 | return -EFAULT; |
| 1573 | |
Paolo Abeni | 01ea306 | 2018-02-08 12:19:00 +0100 | [diff] [blame] | 1574 | err = nf_getsockopt(sk, PF_INET, optname, optval, &len); |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 1575 | if (err >= 0) |
| 1576 | err = put_user(len, optlen); |
| 1577 | return err; |
| 1578 | } |
| 1579 | #endif |
| 1580 | return err; |
| 1581 | } |
Eric Dumazet | 4d52cfb | 2009-06-02 00:42:16 -0700 | [diff] [blame] | 1582 | EXPORT_SYMBOL(ip_getsockopt); |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 1583 | |
| 1584 | #ifdef CONFIG_COMPAT |
Arnaldo Carvalho de Melo | 543d9cf | 2006-03-20 22:48:35 -0800 | [diff] [blame] | 1585 | int compat_ip_getsockopt(struct sock *sk, int level, int optname, |
| 1586 | char __user *optval, int __user *optlen) |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 1587 | { |
David L Stevens | 42908c6 | 2008-04-29 03:23:22 -0700 | [diff] [blame] | 1588 | int err; |
| 1589 | |
| 1590 | if (optname == MCAST_MSFILTER) |
| 1591 | return compat_mc_getsockopt(sk, level, optname, optval, optlen, |
| 1592 | ip_getsockopt); |
| 1593 | |
Daniel Baluta | dd23198e | 2011-08-07 22:31:07 -0700 | [diff] [blame] | 1594 | err = do_ip_getsockopt(sk, level, optname, optval, optlen, |
| 1595 | MSG_CMSG_COMPAT); |
David L Stevens | 42908c6 | 2008-04-29 03:23:22 -0700 | [diff] [blame] | 1596 | |
Alexei Starovoitov | d2ba09c | 2018-05-21 19:22:30 -0700 | [diff] [blame] | 1597 | #ifdef CONFIG_BPFILTER |
| 1598 | if (optname >= BPFILTER_IPT_SO_GET_INFO && |
| 1599 | optname < BPFILTER_IPT_GET_MAX) |
| 1600 | err = bpfilter_ip_get_sockopt(sk, optname, optval, optlen); |
| 1601 | #endif |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 1602 | #ifdef CONFIG_NETFILTER |
| 1603 | /* we need to exclude all possible ENOPROTOOPTs except default case */ |
Pavel Emelyanov | 6a9fb94 | 2007-11-05 21:32:31 -0800 | [diff] [blame] | 1604 | if (err == -ENOPROTOOPT && optname != IP_PKTOPTIONS && |
| 1605 | !ip_mroute_opt(optname)) { |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 1606 | int len; |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 1607 | |
Arnaldo Carvalho de Melo | 543d9cf | 2006-03-20 22:48:35 -0800 | [diff] [blame] | 1608 | if (get_user(len, optlen)) |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 1609 | return -EFAULT; |
| 1610 | |
Arnaldo Carvalho de Melo | 543d9cf | 2006-03-20 22:48:35 -0800 | [diff] [blame] | 1611 | err = compat_nf_getsockopt(sk, PF_INET, optname, optval, &len); |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 1612 | if (err >= 0) |
| 1613 | err = put_user(len, optlen); |
| 1614 | return err; |
| 1615 | } |
| 1616 | #endif |
| 1617 | return err; |
| 1618 | } |
Arnaldo Carvalho de Melo | 543d9cf | 2006-03-20 22:48:35 -0800 | [diff] [blame] | 1619 | EXPORT_SYMBOL(compat_ip_getsockopt); |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 1620 | #endif |