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