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