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