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