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