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