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