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 | } |
| 354 | if (new_ra == NULL) { |
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 | |
Arnaldo Carvalho de Melo | bd82393 | 2007-03-13 17:10:43 -0300 | [diff] [blame] | 390 | if (skb_pull(skb, payload - skb->data) != NULL) { |
| 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 | |
Willem de Bruijn | 829ae9d | 2014-11-30 22:22:34 -0500 | [diff] [blame] | 435 | static bool ipv4_pktinfo_prepare_errqueue(const struct sock *sk, |
| 436 | const struct sk_buff *skb, |
| 437 | int ee_origin) |
| 438 | { |
| 439 | struct in_pktinfo *info = PKTINFO_SKB_CB(skb); |
| 440 | |
| 441 | if ((ee_origin != SO_EE_ORIGIN_TIMESTAMPING) || |
| 442 | (!(sk->sk_tsflags & SOF_TIMESTAMPING_OPT_CMSG)) || |
| 443 | (!skb->dev)) |
| 444 | return false; |
| 445 | |
| 446 | info->ipi_spec_dst.s_addr = ip_hdr(skb)->saddr; |
| 447 | info->ipi_ifindex = skb->dev->ifindex; |
| 448 | return true; |
| 449 | } |
| 450 | |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 451 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 452 | * Handle MSG_ERRQUEUE |
| 453 | */ |
Hannes Frederic Sowa | 85fbaa7 | 2013-11-23 00:46:12 +0100 | [diff] [blame] | 454 | 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] | 455 | { |
| 456 | struct sock_exterr_skb *serr; |
Willem de Bruijn | 364a9e9 | 2014-08-31 21:30:27 -0400 | [diff] [blame] | 457 | struct sk_buff *skb; |
Steffen Hurrle | 342dfc3 | 2014-01-17 22:53:15 +0100 | [diff] [blame] | 458 | DECLARE_SOCKADDR(struct sockaddr_in *, sin, msg->msg_name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 459 | struct { |
| 460 | struct sock_extended_err ee; |
| 461 | struct sockaddr_in offender; |
| 462 | } errhdr; |
| 463 | int err; |
| 464 | int copied; |
| 465 | |
Willem de Bruijn | 7ce875e | 2014-11-30 22:22:33 -0500 | [diff] [blame] | 466 | WARN_ON_ONCE(sk->sk_family == AF_INET6); |
| 467 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 468 | err = -EAGAIN; |
Willem de Bruijn | 364a9e9 | 2014-08-31 21:30:27 -0400 | [diff] [blame] | 469 | skb = sock_dequeue_err_skb(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 470 | if (skb == NULL) |
| 471 | goto out; |
| 472 | |
| 473 | copied = skb->len; |
| 474 | if (copied > len) { |
| 475 | msg->msg_flags |= MSG_TRUNC; |
| 476 | copied = len; |
| 477 | } |
David S. Miller | 51f3d02 | 2014-11-05 16:46:40 -0500 | [diff] [blame] | 478 | err = skb_copy_datagram_msg(skb, 0, msg, copied); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 479 | if (err) |
| 480 | goto out_free_skb; |
| 481 | |
| 482 | sock_recv_timestamp(msg, sk, skb); |
| 483 | |
| 484 | serr = SKB_EXT_ERR(skb); |
| 485 | |
Willem de Bruijn | 49ca0d8 | 2015-01-30 13:29:31 -0500 | [diff] [blame] | 486 | if (sin && skb->len) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 487 | sin->sin_family = AF_INET; |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 488 | sin->sin_addr.s_addr = *(__be32 *)(skb_network_header(skb) + |
| 489 | serr->addr_offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 490 | sin->sin_port = serr->port; |
| 491 | memset(&sin->sin_zero, 0, sizeof(sin->sin_zero)); |
Hannes Frederic Sowa | 85fbaa7 | 2013-11-23 00:46:12 +0100 | [diff] [blame] | 492 | *addr_len = sizeof(*sin); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 493 | } |
| 494 | |
| 495 | memcpy(&errhdr.ee, &serr->ee, sizeof(struct sock_extended_err)); |
| 496 | sin = &errhdr.offender; |
Willem de Bruijn | f812116 | 2015-01-15 13:18:40 -0500 | [diff] [blame] | 497 | memset(sin, 0, sizeof(*sin)); |
Willem de Bruijn | 829ae9d | 2014-11-30 22:22:34 -0500 | [diff] [blame] | 498 | |
Willem de Bruijn | 49ca0d8 | 2015-01-30 13:29:31 -0500 | [diff] [blame] | 499 | if (skb->len && |
| 500 | (serr->ee.ee_origin == SO_EE_ORIGIN_ICMP || |
| 501 | ipv4_pktinfo_prepare_errqueue(sk, skb, serr->ee.ee_origin))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 502 | sin->sin_family = AF_INET; |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 503 | sin->sin_addr.s_addr = ip_hdr(skb)->saddr; |
Willem de Bruijn | f812116 | 2015-01-15 13:18:40 -0500 | [diff] [blame] | 504 | if (inet_sk(sk)->cmsg_flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 505 | ip_cmsg_recv(msg, skb); |
| 506 | } |
| 507 | |
| 508 | put_cmsg(msg, SOL_IP, IP_RECVERR, sizeof(errhdr), &errhdr); |
| 509 | |
| 510 | /* Now we could try to dump offended packet options */ |
| 511 | |
| 512 | msg->msg_flags |= MSG_ERRQUEUE; |
| 513 | err = copied; |
| 514 | |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 515 | out_free_skb: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 516 | kfree_skb(skb); |
| 517 | out: |
| 518 | return err; |
| 519 | } |
| 520 | |
| 521 | |
| 522 | /* |
Eric Dumazet | 4d52cfb | 2009-06-02 00:42:16 -0700 | [diff] [blame] | 523 | * Socket option code for IP. This is the end of the line after any |
| 524 | * TCP,UDP etc options on an IP socket. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 525 | */ |
| 526 | |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 527 | static int do_ip_setsockopt(struct sock *sk, int level, |
David S. Miller | b705884 | 2009-09-30 16:12:20 -0700 | [diff] [blame] | 528 | int optname, char __user *optval, unsigned int optlen) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 529 | { |
| 530 | struct inet_sock *inet = inet_sk(sk); |
Jianjun Kong | 09cb105 | 2008-11-03 00:27:11 -0800 | [diff] [blame] | 531 | int val = 0, err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 532 | |
Xi Wang | 0c9f79b | 2012-11-11 11:20:01 +0000 | [diff] [blame] | 533 | switch (optname) { |
| 534 | case IP_PKTINFO: |
| 535 | case IP_RECVTTL: |
| 536 | case IP_RECVOPTS: |
| 537 | case IP_RECVTOS: |
| 538 | case IP_RETOPTS: |
| 539 | case IP_TOS: |
| 540 | case IP_TTL: |
| 541 | case IP_HDRINCL: |
| 542 | case IP_MTU_DISCOVER: |
| 543 | case IP_RECVERR: |
| 544 | case IP_ROUTER_ALERT: |
| 545 | case IP_FREEBIND: |
| 546 | case IP_PASSSEC: |
| 547 | case IP_TRANSPARENT: |
| 548 | case IP_MINTTL: |
| 549 | case IP_NODEFRAG: |
| 550 | case IP_UNICAST_IF: |
| 551 | case IP_MULTICAST_TTL: |
| 552 | case IP_MULTICAST_ALL: |
| 553 | case IP_MULTICAST_LOOP: |
| 554 | case IP_RECVORIGDSTADDR: |
Tom Herbert | ad6f939 | 2015-01-05 13:56:17 -0800 | [diff] [blame] | 555 | case IP_CHECKSUM: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 556 | if (optlen >= sizeof(int)) { |
| 557 | if (get_user(val, (int __user *) optval)) |
| 558 | return -EFAULT; |
| 559 | } else if (optlen >= sizeof(char)) { |
| 560 | unsigned char ucval; |
| 561 | |
| 562 | if (get_user(ucval, (unsigned char __user *) optval)) |
| 563 | return -EFAULT; |
| 564 | val = (int) ucval; |
| 565 | } |
| 566 | } |
| 567 | |
| 568 | /* If optlen==0, it is equivalent to val == 0 */ |
| 569 | |
Pavel Emelyanov | 6a9fb94 | 2007-11-05 21:32:31 -0800 | [diff] [blame] | 570 | if (ip_mroute_opt(optname)) |
Jianjun Kong | 09cb105 | 2008-11-03 00:27:11 -0800 | [diff] [blame] | 571 | return ip_mroute_setsockopt(sk, optname, optval, optlen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 572 | |
| 573 | err = 0; |
| 574 | lock_sock(sk); |
| 575 | |
| 576 | switch (optname) { |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 577 | case IP_OPTIONS: |
| 578 | { |
Eric Dumazet | f6d8bd0 | 2011-04-21 09:45:37 +0000 | [diff] [blame] | 579 | struct ip_options_rcu *old, *opt = NULL; |
| 580 | |
roel kluin | 65a1c4f | 2009-10-23 05:59:21 +0000 | [diff] [blame] | 581 | if (optlen > 40) |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 582 | goto e_inval; |
YOSHIFUJI Hideaki | 3b1e0a6 | 2008-03-26 02:26:21 +0900 | [diff] [blame] | 583 | err = ip_options_get_from_user(sock_net(sk), &opt, |
Denis V. Lunev | cb84663 | 2008-03-24 15:31:00 -0700 | [diff] [blame] | 584 | optval, optlen); |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 585 | if (err) |
| 586 | break; |
Eric Dumazet | f6d8bd0 | 2011-04-21 09:45:37 +0000 | [diff] [blame] | 587 | old = rcu_dereference_protected(inet->inet_opt, |
| 588 | sock_owned_by_user(sk)); |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 589 | if (inet->is_icsk) { |
| 590 | struct inet_connection_sock *icsk = inet_csk(sk); |
Eric Dumazet | dfd56b8 | 2011-12-10 09:48:31 +0000 | [diff] [blame] | 591 | #if IS_ENABLED(CONFIG_IPV6) |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 592 | if (sk->sk_family == PF_INET || |
| 593 | (!((1 << sk->sk_state) & |
| 594 | (TCPF_LISTEN | TCPF_CLOSE)) && |
Eric Dumazet | c720c7e | 2009-10-15 06:30:45 +0000 | [diff] [blame] | 595 | inet->inet_daddr != LOOPBACK4_IPV6)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 596 | #endif |
Eric Dumazet | f6d8bd0 | 2011-04-21 09:45:37 +0000 | [diff] [blame] | 597 | if (old) |
| 598 | icsk->icsk_ext_hdr_len -= old->opt.optlen; |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 599 | if (opt) |
Eric Dumazet | f6d8bd0 | 2011-04-21 09:45:37 +0000 | [diff] [blame] | 600 | icsk->icsk_ext_hdr_len += opt->opt.optlen; |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 601 | icsk->icsk_sync_mss(sk, icsk->icsk_pmtu_cookie); |
Eric Dumazet | dfd56b8 | 2011-12-10 09:48:31 +0000 | [diff] [blame] | 602 | #if IS_ENABLED(CONFIG_IPV6) |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 603 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 604 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 605 | } |
Eric Dumazet | f6d8bd0 | 2011-04-21 09:45:37 +0000 | [diff] [blame] | 606 | rcu_assign_pointer(inet->inet_opt, opt); |
| 607 | if (old) |
Paul E. McKenney | 605b4af | 2012-01-06 17:08:33 -0800 | [diff] [blame] | 608 | kfree_rcu(old, rcu); |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 609 | break; |
| 610 | } |
| 611 | case IP_PKTINFO: |
| 612 | if (val) |
| 613 | inet->cmsg_flags |= IP_CMSG_PKTINFO; |
| 614 | else |
| 615 | inet->cmsg_flags &= ~IP_CMSG_PKTINFO; |
| 616 | break; |
| 617 | case IP_RECVTTL: |
| 618 | if (val) |
| 619 | inet->cmsg_flags |= IP_CMSG_TTL; |
| 620 | else |
| 621 | inet->cmsg_flags &= ~IP_CMSG_TTL; |
| 622 | break; |
| 623 | case IP_RECVTOS: |
| 624 | if (val) |
| 625 | inet->cmsg_flags |= IP_CMSG_TOS; |
| 626 | else |
| 627 | inet->cmsg_flags &= ~IP_CMSG_TOS; |
| 628 | break; |
| 629 | case IP_RECVOPTS: |
| 630 | if (val) |
| 631 | inet->cmsg_flags |= IP_CMSG_RECVOPTS; |
| 632 | else |
| 633 | inet->cmsg_flags &= ~IP_CMSG_RECVOPTS; |
| 634 | break; |
| 635 | case IP_RETOPTS: |
| 636 | if (val) |
| 637 | inet->cmsg_flags |= IP_CMSG_RETOPTS; |
| 638 | else |
| 639 | inet->cmsg_flags &= ~IP_CMSG_RETOPTS; |
| 640 | break; |
| 641 | case IP_PASSSEC: |
| 642 | if (val) |
| 643 | inet->cmsg_flags |= IP_CMSG_PASSSEC; |
| 644 | else |
| 645 | inet->cmsg_flags &= ~IP_CMSG_PASSSEC; |
| 646 | break; |
Balazs Scheidler | e8b2dfe | 2008-11-16 19:32:39 -0800 | [diff] [blame] | 647 | case IP_RECVORIGDSTADDR: |
| 648 | if (val) |
| 649 | inet->cmsg_flags |= IP_CMSG_ORIGDSTADDR; |
| 650 | else |
| 651 | inet->cmsg_flags &= ~IP_CMSG_ORIGDSTADDR; |
| 652 | break; |
Tom Herbert | ad6f939 | 2015-01-05 13:56:17 -0800 | [diff] [blame] | 653 | case IP_CHECKSUM: |
| 654 | if (val) { |
| 655 | if (!(inet->cmsg_flags & IP_CMSG_CHECKSUM)) { |
| 656 | inet_inc_convert_csum(sk); |
| 657 | inet->cmsg_flags |= IP_CMSG_CHECKSUM; |
| 658 | } |
| 659 | } else { |
| 660 | if (inet->cmsg_flags & IP_CMSG_CHECKSUM) { |
| 661 | inet_dec_convert_csum(sk); |
| 662 | inet->cmsg_flags &= ~IP_CMSG_CHECKSUM; |
| 663 | } |
| 664 | } |
| 665 | break; |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 666 | case IP_TOS: /* This sets both TOS and Precedence */ |
| 667 | if (sk->sk_type == SOCK_STREAM) { |
Maciej Żenczykowski | 2c67e9a | 2011-10-22 00:07:47 -0400 | [diff] [blame] | 668 | val &= ~INET_ECN_MASK; |
| 669 | val |= inet->tos & INET_ECN_MASK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 670 | } |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 671 | if (inet->tos != val) { |
| 672 | inet->tos = val; |
| 673 | sk->sk_priority = rt_tos2priority(val); |
| 674 | sk_dst_reset(sk); |
| 675 | } |
| 676 | break; |
| 677 | case IP_TTL: |
Eric Dumazet | 4d52cfb | 2009-06-02 00:42:16 -0700 | [diff] [blame] | 678 | if (optlen < 1) |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 679 | goto e_inval; |
Cong Wang | c9be4a5 | 2013-01-07 21:17:00 +0000 | [diff] [blame] | 680 | if (val != -1 && (val < 1 || val > 255)) |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 681 | goto e_inval; |
| 682 | inet->uc_ttl = val; |
| 683 | break; |
| 684 | case IP_HDRINCL: |
| 685 | if (sk->sk_type != SOCK_RAW) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 686 | err = -ENOPROTOOPT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 687 | break; |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 688 | } |
| 689 | inet->hdrincl = val ? 1 : 0; |
| 690 | break; |
Jiri Olsa | 7b2ff18 | 2010-06-15 01:07:31 +0000 | [diff] [blame] | 691 | case IP_NODEFRAG: |
| 692 | if (sk->sk_type != SOCK_RAW) { |
| 693 | err = -ENOPROTOOPT; |
| 694 | break; |
| 695 | } |
| 696 | inet->nodefrag = val ? 1 : 0; |
| 697 | break; |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 698 | case IP_MTU_DISCOVER: |
Hannes Frederic Sowa | 1b34657 | 2014-02-26 01:20:42 +0100 | [diff] [blame] | 699 | if (val < IP_PMTUDISC_DONT || val > IP_PMTUDISC_OMIT) |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 700 | goto e_inval; |
| 701 | inet->pmtudisc = val; |
| 702 | break; |
| 703 | case IP_RECVERR: |
| 704 | inet->recverr = !!val; |
| 705 | if (!val) |
| 706 | skb_queue_purge(&sk->sk_error_queue); |
| 707 | break; |
| 708 | case IP_MULTICAST_TTL: |
| 709 | if (sk->sk_type == SOCK_STREAM) |
| 710 | goto e_inval; |
Eric Dumazet | 4d52cfb | 2009-06-02 00:42:16 -0700 | [diff] [blame] | 711 | if (optlen < 1) |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 712 | goto e_inval; |
Jianjun Kong | 09cb105 | 2008-11-03 00:27:11 -0800 | [diff] [blame] | 713 | if (val == -1) |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 714 | val = 1; |
| 715 | if (val < 0 || val > 255) |
| 716 | goto e_inval; |
| 717 | inet->mc_ttl = val; |
| 718 | break; |
| 719 | case IP_MULTICAST_LOOP: |
Eric Dumazet | 4d52cfb | 2009-06-02 00:42:16 -0700 | [diff] [blame] | 720 | if (optlen < 1) |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 721 | goto e_inval; |
| 722 | inet->mc_loop = !!val; |
| 723 | break; |
Erich E. Hoover | 76e2105 | 2012-02-08 09:11:07 +0000 | [diff] [blame] | 724 | case IP_UNICAST_IF: |
| 725 | { |
| 726 | struct net_device *dev = NULL; |
| 727 | int ifindex; |
| 728 | |
| 729 | if (optlen != sizeof(int)) |
| 730 | goto e_inval; |
| 731 | |
| 732 | ifindex = (__force int)ntohl((__force __be32)val); |
| 733 | if (ifindex == 0) { |
| 734 | inet->uc_index = 0; |
| 735 | err = 0; |
| 736 | break; |
| 737 | } |
| 738 | |
| 739 | dev = dev_get_by_index(sock_net(sk), ifindex); |
| 740 | err = -EADDRNOTAVAIL; |
| 741 | if (!dev) |
| 742 | break; |
| 743 | dev_put(dev); |
| 744 | |
| 745 | err = -EINVAL; |
| 746 | if (sk->sk_bound_dev_if) |
| 747 | break; |
| 748 | |
| 749 | inet->uc_index = ifindex; |
| 750 | err = 0; |
| 751 | break; |
| 752 | } |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 753 | case IP_MULTICAST_IF: |
| 754 | { |
| 755 | struct ip_mreqn mreq; |
| 756 | struct net_device *dev = NULL; |
| 757 | |
| 758 | if (sk->sk_type == SOCK_STREAM) |
| 759 | goto e_inval; |
| 760 | /* |
| 761 | * Check the arguments are allowable |
| 762 | */ |
| 763 | |
Shan Wei | 0915921 | 2009-09-22 15:41:10 +0000 | [diff] [blame] | 764 | if (optlen < sizeof(struct in_addr)) |
| 765 | goto e_inval; |
| 766 | |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 767 | err = -EFAULT; |
| 768 | if (optlen >= sizeof(struct ip_mreqn)) { |
Jianjun Kong | 09cb105 | 2008-11-03 00:27:11 -0800 | [diff] [blame] | 769 | if (copy_from_user(&mreq, optval, sizeof(mreq))) |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 770 | break; |
| 771 | } else { |
| 772 | memset(&mreq, 0, sizeof(mreq)); |
Jiri Pirko | 3a084dd | 2012-05-03 22:37:45 +0000 | [diff] [blame] | 773 | if (optlen >= sizeof(struct ip_mreq)) { |
| 774 | if (copy_from_user(&mreq, optval, |
| 775 | sizeof(struct ip_mreq))) |
| 776 | break; |
| 777 | } else if (optlen >= sizeof(struct in_addr)) { |
| 778 | if (copy_from_user(&mreq.imr_address, optval, |
| 779 | sizeof(struct in_addr))) |
| 780 | break; |
| 781 | } |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 782 | } |
| 783 | |
| 784 | if (!mreq.imr_ifindex) { |
Al Viro | e6f1ceb | 2008-03-17 22:44:53 -0700 | [diff] [blame] | 785 | if (mreq.imr_address.s_addr == htonl(INADDR_ANY)) { |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 786 | inet->mc_index = 0; |
| 787 | inet->mc_addr = 0; |
| 788 | err = 0; |
| 789 | break; |
| 790 | } |
YOSHIFUJI Hideaki | 3b1e0a6 | 2008-03-26 02:26:21 +0900 | [diff] [blame] | 791 | dev = ip_dev_find(sock_net(sk), mreq.imr_address.s_addr); |
Eric Dumazet | 55b8050 | 2009-10-19 06:41:58 +0000 | [diff] [blame] | 792 | if (dev) |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 793 | mreq.imr_ifindex = dev->ifindex; |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 794 | } else |
Eric Dumazet | 55b8050 | 2009-10-19 06:41:58 +0000 | [diff] [blame] | 795 | dev = dev_get_by_index(sock_net(sk), mreq.imr_ifindex); |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 796 | |
| 797 | |
| 798 | err = -EADDRNOTAVAIL; |
| 799 | if (!dev) |
| 800 | break; |
Eric Dumazet | 55b8050 | 2009-10-19 06:41:58 +0000 | [diff] [blame] | 801 | dev_put(dev); |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 802 | |
| 803 | err = -EINVAL; |
| 804 | if (sk->sk_bound_dev_if && |
| 805 | mreq.imr_ifindex != sk->sk_bound_dev_if) |
| 806 | break; |
| 807 | |
| 808 | inet->mc_index = mreq.imr_ifindex; |
| 809 | inet->mc_addr = mreq.imr_address.s_addr; |
| 810 | err = 0; |
| 811 | break; |
| 812 | } |
| 813 | |
| 814 | case IP_ADD_MEMBERSHIP: |
| 815 | case IP_DROP_MEMBERSHIP: |
| 816 | { |
| 817 | struct ip_mreqn mreq; |
| 818 | |
Flavio Leitner | a96fb49 | 2007-08-24 22:16:39 -0700 | [diff] [blame] | 819 | err = -EPROTO; |
| 820 | if (inet_sk(sk)->is_icsk) |
| 821 | break; |
| 822 | |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 823 | if (optlen < sizeof(struct ip_mreq)) |
| 824 | goto e_inval; |
| 825 | err = -EFAULT; |
| 826 | if (optlen >= sizeof(struct ip_mreqn)) { |
Jianjun Kong | 09cb105 | 2008-11-03 00:27:11 -0800 | [diff] [blame] | 827 | if (copy_from_user(&mreq, optval, sizeof(mreq))) |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 828 | break; |
| 829 | } else { |
| 830 | memset(&mreq, 0, sizeof(mreq)); |
Jianjun Kong | 09cb105 | 2008-11-03 00:27:11 -0800 | [diff] [blame] | 831 | if (copy_from_user(&mreq, optval, sizeof(struct ip_mreq))) |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 832 | break; |
| 833 | } |
| 834 | |
| 835 | if (optname == IP_ADD_MEMBERSHIP) |
| 836 | err = ip_mc_join_group(sk, &mreq); |
| 837 | else |
| 838 | err = ip_mc_leave_group(sk, &mreq); |
| 839 | break; |
| 840 | } |
| 841 | case IP_MSFILTER: |
| 842 | { |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 843 | struct ip_msfilter *msf; |
| 844 | |
| 845 | if (optlen < IP_MSFILTER_SIZE(0)) |
| 846 | goto e_inval; |
| 847 | if (optlen > sysctl_optmem_max) { |
| 848 | err = -ENOBUFS; |
| 849 | break; |
| 850 | } |
| 851 | msf = kmalloc(optlen, GFP_KERNEL); |
Stephen Hemminger | cfcabdc | 2007-10-09 01:59:42 -0700 | [diff] [blame] | 852 | if (!msf) { |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 853 | err = -ENOBUFS; |
| 854 | break; |
| 855 | } |
| 856 | err = -EFAULT; |
| 857 | if (copy_from_user(msf, optval, optlen)) { |
| 858 | kfree(msf); |
| 859 | break; |
| 860 | } |
| 861 | /* numsrc >= (1G-4) overflow in 32 bits */ |
| 862 | if (msf->imsf_numsrc >= 0x3ffffffcU || |
| 863 | msf->imsf_numsrc > sysctl_igmp_max_msf) { |
| 864 | kfree(msf); |
| 865 | err = -ENOBUFS; |
| 866 | break; |
| 867 | } |
| 868 | if (IP_MSFILTER_SIZE(msf->imsf_numsrc) > optlen) { |
| 869 | kfree(msf); |
| 870 | err = -EINVAL; |
| 871 | break; |
| 872 | } |
| 873 | err = ip_mc_msfilter(sk, msf, 0); |
| 874 | kfree(msf); |
| 875 | break; |
| 876 | } |
| 877 | case IP_BLOCK_SOURCE: |
| 878 | case IP_UNBLOCK_SOURCE: |
| 879 | case IP_ADD_SOURCE_MEMBERSHIP: |
| 880 | case IP_DROP_SOURCE_MEMBERSHIP: |
| 881 | { |
| 882 | struct ip_mreq_source mreqs; |
| 883 | int omode, add; |
| 884 | |
| 885 | if (optlen != sizeof(struct ip_mreq_source)) |
| 886 | goto e_inval; |
| 887 | if (copy_from_user(&mreqs, optval, sizeof(mreqs))) { |
| 888 | err = -EFAULT; |
| 889 | break; |
| 890 | } |
| 891 | if (optname == IP_BLOCK_SOURCE) { |
| 892 | omode = MCAST_EXCLUDE; |
| 893 | add = 1; |
| 894 | } else if (optname == IP_UNBLOCK_SOURCE) { |
| 895 | omode = MCAST_EXCLUDE; |
| 896 | add = 0; |
| 897 | } else if (optname == IP_ADD_SOURCE_MEMBERSHIP) { |
| 898 | struct ip_mreqn mreq; |
| 899 | |
| 900 | mreq.imr_multiaddr.s_addr = mreqs.imr_multiaddr; |
| 901 | mreq.imr_address.s_addr = mreqs.imr_interface; |
| 902 | mreq.imr_ifindex = 0; |
| 903 | err = ip_mc_join_group(sk, &mreq); |
| 904 | if (err && err != -EADDRINUSE) |
| 905 | break; |
| 906 | omode = MCAST_INCLUDE; |
| 907 | add = 1; |
| 908 | } else /* IP_DROP_SOURCE_MEMBERSHIP */ { |
| 909 | omode = MCAST_INCLUDE; |
| 910 | add = 0; |
| 911 | } |
| 912 | err = ip_mc_source(add, omode, sk, &mreqs, 0); |
| 913 | break; |
| 914 | } |
| 915 | case MCAST_JOIN_GROUP: |
| 916 | case MCAST_LEAVE_GROUP: |
| 917 | { |
| 918 | struct group_req greq; |
| 919 | struct sockaddr_in *psin; |
| 920 | struct ip_mreqn mreq; |
| 921 | |
| 922 | if (optlen < sizeof(struct group_req)) |
| 923 | goto e_inval; |
| 924 | err = -EFAULT; |
| 925 | if (copy_from_user(&greq, optval, sizeof(greq))) |
| 926 | break; |
| 927 | psin = (struct sockaddr_in *)&greq.gr_group; |
| 928 | if (psin->sin_family != AF_INET) |
| 929 | goto e_inval; |
| 930 | memset(&mreq, 0, sizeof(mreq)); |
| 931 | mreq.imr_multiaddr = psin->sin_addr; |
| 932 | mreq.imr_ifindex = greq.gr_interface; |
| 933 | |
| 934 | if (optname == MCAST_JOIN_GROUP) |
| 935 | err = ip_mc_join_group(sk, &mreq); |
| 936 | else |
| 937 | err = ip_mc_leave_group(sk, &mreq); |
| 938 | break; |
| 939 | } |
| 940 | case MCAST_JOIN_SOURCE_GROUP: |
| 941 | case MCAST_LEAVE_SOURCE_GROUP: |
| 942 | case MCAST_BLOCK_SOURCE: |
| 943 | case MCAST_UNBLOCK_SOURCE: |
| 944 | { |
| 945 | struct group_source_req greqs; |
| 946 | struct ip_mreq_source mreqs; |
| 947 | struct sockaddr_in *psin; |
| 948 | int omode, add; |
| 949 | |
| 950 | if (optlen != sizeof(struct group_source_req)) |
| 951 | goto e_inval; |
| 952 | if (copy_from_user(&greqs, optval, sizeof(greqs))) { |
| 953 | err = -EFAULT; |
| 954 | break; |
| 955 | } |
| 956 | if (greqs.gsr_group.ss_family != AF_INET || |
| 957 | greqs.gsr_source.ss_family != AF_INET) { |
| 958 | err = -EADDRNOTAVAIL; |
| 959 | break; |
| 960 | } |
| 961 | psin = (struct sockaddr_in *)&greqs.gsr_group; |
| 962 | mreqs.imr_multiaddr = psin->sin_addr.s_addr; |
| 963 | psin = (struct sockaddr_in *)&greqs.gsr_source; |
| 964 | mreqs.imr_sourceaddr = psin->sin_addr.s_addr; |
| 965 | mreqs.imr_interface = 0; /* use index for mc_source */ |
| 966 | |
| 967 | if (optname == MCAST_BLOCK_SOURCE) { |
| 968 | omode = MCAST_EXCLUDE; |
| 969 | add = 1; |
| 970 | } else if (optname == MCAST_UNBLOCK_SOURCE) { |
| 971 | omode = MCAST_EXCLUDE; |
| 972 | add = 0; |
| 973 | } else if (optname == MCAST_JOIN_SOURCE_GROUP) { |
| 974 | struct ip_mreqn mreq; |
| 975 | |
| 976 | psin = (struct sockaddr_in *)&greqs.gsr_group; |
| 977 | mreq.imr_multiaddr = psin->sin_addr; |
| 978 | mreq.imr_address.s_addr = 0; |
| 979 | mreq.imr_ifindex = greqs.gsr_interface; |
| 980 | err = ip_mc_join_group(sk, &mreq); |
| 981 | if (err && err != -EADDRINUSE) |
| 982 | break; |
| 983 | greqs.gsr_interface = mreq.imr_ifindex; |
| 984 | omode = MCAST_INCLUDE; |
| 985 | add = 1; |
| 986 | } else /* MCAST_LEAVE_SOURCE_GROUP */ { |
| 987 | omode = MCAST_INCLUDE; |
| 988 | add = 0; |
| 989 | } |
| 990 | err = ip_mc_source(add, omode, sk, &mreqs, |
| 991 | greqs.gsr_interface); |
| 992 | break; |
| 993 | } |
| 994 | case MCAST_MSFILTER: |
| 995 | { |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 996 | struct sockaddr_in *psin; |
| 997 | struct ip_msfilter *msf = NULL; |
| 998 | struct group_filter *gsf = NULL; |
| 999 | int msize, i, ifindex; |
| 1000 | |
| 1001 | if (optlen < GROUP_FILTER_SIZE(0)) |
| 1002 | goto e_inval; |
| 1003 | if (optlen > sysctl_optmem_max) { |
| 1004 | err = -ENOBUFS; |
| 1005 | break; |
| 1006 | } |
Jianjun Kong | 09cb105 | 2008-11-03 00:27:11 -0800 | [diff] [blame] | 1007 | gsf = kmalloc(optlen, GFP_KERNEL); |
Stephen Hemminger | cfcabdc | 2007-10-09 01:59:42 -0700 | [diff] [blame] | 1008 | if (!gsf) { |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1009 | err = -ENOBUFS; |
| 1010 | break; |
| 1011 | } |
| 1012 | err = -EFAULT; |
Eric Dumazet | 4d52cfb | 2009-06-02 00:42:16 -0700 | [diff] [blame] | 1013 | if (copy_from_user(gsf, optval, optlen)) |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1014 | goto mc_msf_out; |
Eric Dumazet | 4d52cfb | 2009-06-02 00:42:16 -0700 | [diff] [blame] | 1015 | |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1016 | /* numsrc >= (4G-140)/128 overflow in 32 bits */ |
| 1017 | if (gsf->gf_numsrc >= 0x1ffffff || |
| 1018 | gsf->gf_numsrc > sysctl_igmp_max_msf) { |
| 1019 | err = -ENOBUFS; |
| 1020 | goto mc_msf_out; |
| 1021 | } |
| 1022 | if (GROUP_FILTER_SIZE(gsf->gf_numsrc) > optlen) { |
| 1023 | err = -EINVAL; |
| 1024 | goto mc_msf_out; |
| 1025 | } |
| 1026 | msize = IP_MSFILTER_SIZE(gsf->gf_numsrc); |
Jianjun Kong | 09cb105 | 2008-11-03 00:27:11 -0800 | [diff] [blame] | 1027 | msf = kmalloc(msize, GFP_KERNEL); |
Stephen Hemminger | cfcabdc | 2007-10-09 01:59:42 -0700 | [diff] [blame] | 1028 | if (!msf) { |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1029 | err = -ENOBUFS; |
| 1030 | goto mc_msf_out; |
| 1031 | } |
| 1032 | ifindex = gsf->gf_interface; |
| 1033 | psin = (struct sockaddr_in *)&gsf->gf_group; |
| 1034 | if (psin->sin_family != AF_INET) { |
| 1035 | err = -EADDRNOTAVAIL; |
| 1036 | goto mc_msf_out; |
| 1037 | } |
| 1038 | msf->imsf_multiaddr = psin->sin_addr.s_addr; |
| 1039 | msf->imsf_interface = 0; |
| 1040 | msf->imsf_fmode = gsf->gf_fmode; |
| 1041 | msf->imsf_numsrc = gsf->gf_numsrc; |
| 1042 | err = -EADDRNOTAVAIL; |
Eric Dumazet | 4d52cfb | 2009-06-02 00:42:16 -0700 | [diff] [blame] | 1043 | for (i = 0; i < gsf->gf_numsrc; ++i) { |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1044 | psin = (struct sockaddr_in *)&gsf->gf_slist[i]; |
| 1045 | |
| 1046 | if (psin->sin_family != AF_INET) |
| 1047 | goto mc_msf_out; |
| 1048 | msf->imsf_slist[i] = psin->sin_addr.s_addr; |
| 1049 | } |
| 1050 | kfree(gsf); |
| 1051 | gsf = NULL; |
| 1052 | |
| 1053 | err = ip_mc_msfilter(sk, msf, ifindex); |
Eric Dumazet | 4d52cfb | 2009-06-02 00:42:16 -0700 | [diff] [blame] | 1054 | mc_msf_out: |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1055 | kfree(msf); |
| 1056 | kfree(gsf); |
| 1057 | break; |
| 1058 | } |
Nivedita Singhvi | f771bef | 2009-05-28 07:00:46 +0000 | [diff] [blame] | 1059 | case IP_MULTICAST_ALL: |
| 1060 | if (optlen < 1) |
| 1061 | goto e_inval; |
| 1062 | if (val != 0 && val != 1) |
| 1063 | goto e_inval; |
| 1064 | inet->mc_all = val; |
| 1065 | break; |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1066 | case IP_ROUTER_ALERT: |
| 1067 | err = ip_ra_control(sk, val ? 1 : 0, NULL); |
| 1068 | break; |
| 1069 | |
| 1070 | case IP_FREEBIND: |
Eric Dumazet | 4d52cfb | 2009-06-02 00:42:16 -0700 | [diff] [blame] | 1071 | if (optlen < 1) |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1072 | goto e_inval; |
| 1073 | inet->freebind = !!val; |
| 1074 | break; |
| 1075 | |
| 1076 | case IP_IPSEC_POLICY: |
| 1077 | case IP_XFRM_POLICY: |
| 1078 | err = -EPERM; |
Eric W. Biederman | 52e804c | 2012-11-16 03:03:05 +0000 | [diff] [blame] | 1079 | if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN)) |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1080 | break; |
| 1081 | err = xfrm_user_policy(sk, optname, optval, optlen); |
| 1082 | break; |
| 1083 | |
KOVACS Krisztian | f5715ae | 2008-10-01 07:30:02 -0700 | [diff] [blame] | 1084 | case IP_TRANSPARENT: |
Eric W. Biederman | 52e804c | 2012-11-16 03:03:05 +0000 | [diff] [blame] | 1085 | if (!!val && !ns_capable(sock_net(sk)->user_ns, CAP_NET_RAW) && |
| 1086 | !ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN)) { |
KOVACS Krisztian | f5715ae | 2008-10-01 07:30:02 -0700 | [diff] [blame] | 1087 | err = -EPERM; |
| 1088 | break; |
| 1089 | } |
| 1090 | if (optlen < 1) |
| 1091 | goto e_inval; |
| 1092 | inet->transparent = !!val; |
| 1093 | break; |
| 1094 | |
Stephen Hemminger | d218d11 | 2010-01-11 16:28:01 -0800 | [diff] [blame] | 1095 | case IP_MINTTL: |
| 1096 | if (optlen < 1) |
| 1097 | goto e_inval; |
| 1098 | if (val < 0 || val > 255) |
| 1099 | goto e_inval; |
| 1100 | inet->min_ttl = val; |
| 1101 | break; |
| 1102 | |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1103 | default: |
| 1104 | err = -ENOPROTOOPT; |
| 1105 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1106 | } |
| 1107 | release_sock(sk); |
| 1108 | return err; |
| 1109 | |
| 1110 | e_inval: |
| 1111 | release_sock(sk); |
| 1112 | return -EINVAL; |
| 1113 | } |
| 1114 | |
Eric Dumazet | f84af32 | 2010-04-28 15:31:51 -0700 | [diff] [blame] | 1115 | /** |
Willem de Bruijn | 829ae9d | 2014-11-30 22:22:34 -0500 | [diff] [blame] | 1116 | * ipv4_pktinfo_prepare - transfer some info from rtable to skb |
Eric Dumazet | f84af32 | 2010-04-28 15:31:51 -0700 | [diff] [blame] | 1117 | * @sk: socket |
| 1118 | * @skb: buffer |
| 1119 | * |
David S. Miller | 35ebf65 | 2012-06-28 03:59:11 -0700 | [diff] [blame] | 1120 | * To support IP_CMSG_PKTINFO option, we store rt_iif and specific |
| 1121 | * destination in skb->cb[] before dst drop. |
stephen hemminger | 8e3bff9 | 2013-12-08 12:15:44 -0800 | [diff] [blame] | 1122 | * This way, receiver doesn't make cache line misses to read rtable. |
Eric Dumazet | f84af32 | 2010-04-28 15:31:51 -0700 | [diff] [blame] | 1123 | */ |
Shawn Bohrer | fbf8866 | 2013-10-07 11:01:40 -0500 | [diff] [blame] | 1124 | void ipv4_pktinfo_prepare(const struct sock *sk, struct sk_buff *skb) |
Eric Dumazet | f84af32 | 2010-04-28 15:31:51 -0700 | [diff] [blame] | 1125 | { |
Eric Dumazet | d826eb1 | 2011-11-09 07:24:35 +0000 | [diff] [blame] | 1126 | struct in_pktinfo *pktinfo = PKTINFO_SKB_CB(skb); |
Hannes Frederic Sowa | 4b261c7 | 2014-01-20 03:43:08 +0100 | [diff] [blame] | 1127 | bool prepare = (inet_sk(sk)->cmsg_flags & IP_CMSG_PKTINFO) || |
| 1128 | ipv6_sk_rxinfo(sk); |
Eric Dumazet | d826eb1 | 2011-11-09 07:24:35 +0000 | [diff] [blame] | 1129 | |
Hannes Frederic Sowa | 4b261c7 | 2014-01-20 03:43:08 +0100 | [diff] [blame] | 1130 | if (prepare && skb_rtable(skb)) { |
David S. Miller | 92101b3 | 2012-07-23 16:29:00 -0700 | [diff] [blame] | 1131 | pktinfo->ipi_ifindex = inet_iif(skb); |
David S. Miller | 35ebf65 | 2012-06-28 03:59:11 -0700 | [diff] [blame] | 1132 | pktinfo->ipi_spec_dst.s_addr = fib_compute_spec_dst(skb); |
Eric Dumazet | d826eb1 | 2011-11-09 07:24:35 +0000 | [diff] [blame] | 1133 | } else { |
| 1134 | pktinfo->ipi_ifindex = 0; |
| 1135 | pktinfo->ipi_spec_dst.s_addr = 0; |
| 1136 | } |
| 1137 | skb_dst_drop(skb); |
Eric Dumazet | f84af32 | 2010-04-28 15:31:51 -0700 | [diff] [blame] | 1138 | } |
Eric Dumazet | f84af32 | 2010-04-28 15:31:51 -0700 | [diff] [blame] | 1139 | |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 1140 | int ip_setsockopt(struct sock *sk, int level, |
David S. Miller | b705884 | 2009-09-30 16:12:20 -0700 | [diff] [blame] | 1141 | int optname, char __user *optval, unsigned int optlen) |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 1142 | { |
| 1143 | int err; |
| 1144 | |
| 1145 | if (level != SOL_IP) |
| 1146 | return -ENOPROTOOPT; |
| 1147 | |
| 1148 | err = do_ip_setsockopt(sk, level, optname, optval, optlen); |
| 1149 | #ifdef CONFIG_NETFILTER |
| 1150 | /* we need to exclude all possible ENOPROTOOPTs except default case */ |
| 1151 | if (err == -ENOPROTOOPT && optname != IP_HDRINCL && |
Pavel Emelyanov | 6a9fb94 | 2007-11-05 21:32:31 -0800 | [diff] [blame] | 1152 | optname != IP_IPSEC_POLICY && |
| 1153 | optname != IP_XFRM_POLICY && |
| 1154 | !ip_mroute_opt(optname)) { |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 1155 | lock_sock(sk); |
| 1156 | err = nf_setsockopt(sk, PF_INET, optname, optval, optlen); |
| 1157 | release_sock(sk); |
| 1158 | } |
| 1159 | #endif |
| 1160 | return err; |
| 1161 | } |
Eric Dumazet | 4d52cfb | 2009-06-02 00:42:16 -0700 | [diff] [blame] | 1162 | EXPORT_SYMBOL(ip_setsockopt); |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 1163 | |
| 1164 | #ifdef CONFIG_COMPAT |
Arnaldo Carvalho de Melo | 543d9cf | 2006-03-20 22:48:35 -0800 | [diff] [blame] | 1165 | int compat_ip_setsockopt(struct sock *sk, int level, int optname, |
David S. Miller | b705884 | 2009-09-30 16:12:20 -0700 | [diff] [blame] | 1166 | char __user *optval, unsigned int optlen) |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 1167 | { |
| 1168 | int err; |
| 1169 | |
| 1170 | if (level != SOL_IP) |
| 1171 | return -ENOPROTOOPT; |
| 1172 | |
David L Stevens | dae5029 | 2008-04-27 01:06:07 -0700 | [diff] [blame] | 1173 | if (optname >= MCAST_JOIN_GROUP && optname <= MCAST_MSFILTER) |
| 1174 | return compat_mc_setsockopt(sk, level, optname, optval, optlen, |
| 1175 | ip_setsockopt); |
| 1176 | |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 1177 | err = do_ip_setsockopt(sk, level, optname, optval, optlen); |
| 1178 | #ifdef CONFIG_NETFILTER |
| 1179 | /* we need to exclude all possible ENOPROTOOPTs except default case */ |
| 1180 | if (err == -ENOPROTOOPT && optname != IP_HDRINCL && |
Pavel Emelyanov | 6a9fb94 | 2007-11-05 21:32:31 -0800 | [diff] [blame] | 1181 | optname != IP_IPSEC_POLICY && |
| 1182 | optname != IP_XFRM_POLICY && |
| 1183 | !ip_mroute_opt(optname)) { |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 1184 | lock_sock(sk); |
Arnaldo Carvalho de Melo | 543d9cf | 2006-03-20 22:48:35 -0800 | [diff] [blame] | 1185 | err = compat_nf_setsockopt(sk, PF_INET, optname, |
| 1186 | optval, optlen); |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 1187 | release_sock(sk); |
| 1188 | } |
| 1189 | #endif |
| 1190 | return err; |
| 1191 | } |
Arnaldo Carvalho de Melo | 543d9cf | 2006-03-20 22:48:35 -0800 | [diff] [blame] | 1192 | EXPORT_SYMBOL(compat_ip_setsockopt); |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 1193 | #endif |
| 1194 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1195 | /* |
Eric Dumazet | 4d52cfb | 2009-06-02 00:42:16 -0700 | [diff] [blame] | 1196 | * Get the options. Note for future reference. The GET of IP options gets |
| 1197 | * the _received_ ones. The set sets the _sent_ ones. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1198 | */ |
| 1199 | |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 1200 | static int do_ip_getsockopt(struct sock *sk, int level, int optname, |
Eric Dumazet | 95c9617 | 2012-04-15 05:58:06 +0000 | [diff] [blame] | 1201 | char __user *optval, int __user *optlen, unsigned int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1202 | { |
| 1203 | struct inet_sock *inet = inet_sk(sk); |
| 1204 | int val; |
| 1205 | int len; |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 1206 | |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1207 | if (level != SOL_IP) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1208 | return -EOPNOTSUPP; |
| 1209 | |
Pavel Emelyanov | 6a9fb94 | 2007-11-05 21:32:31 -0800 | [diff] [blame] | 1210 | if (ip_mroute_opt(optname)) |
Jianjun Kong | 09cb105 | 2008-11-03 00:27:11 -0800 | [diff] [blame] | 1211 | return ip_mroute_getsockopt(sk, optname, optval, optlen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1212 | |
Jianjun Kong | 09cb105 | 2008-11-03 00:27:11 -0800 | [diff] [blame] | 1213 | if (get_user(len, optlen)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1214 | return -EFAULT; |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1215 | if (len < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1216 | return -EINVAL; |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 1217 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1218 | lock_sock(sk); |
| 1219 | |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1220 | switch (optname) { |
| 1221 | case IP_OPTIONS: |
| 1222 | { |
| 1223 | unsigned char optbuf[sizeof(struct ip_options)+40]; |
Eric Dumazet | f6d8bd0 | 2011-04-21 09:45:37 +0000 | [diff] [blame] | 1224 | struct ip_options *opt = (struct ip_options *)optbuf; |
| 1225 | struct ip_options_rcu *inet_opt; |
| 1226 | |
| 1227 | inet_opt = rcu_dereference_protected(inet->inet_opt, |
| 1228 | sock_owned_by_user(sk)); |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1229 | opt->optlen = 0; |
Eric Dumazet | f6d8bd0 | 2011-04-21 09:45:37 +0000 | [diff] [blame] | 1230 | if (inet_opt) |
| 1231 | memcpy(optbuf, &inet_opt->opt, |
| 1232 | sizeof(struct ip_options) + |
| 1233 | inet_opt->opt.optlen); |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1234 | release_sock(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1235 | |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1236 | if (opt->optlen == 0) |
| 1237 | return put_user(0, optlen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1238 | |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1239 | ip_options_undo(opt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1240 | |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1241 | len = min_t(unsigned int, len, opt->optlen); |
| 1242 | if (put_user(len, optlen)) |
| 1243 | return -EFAULT; |
| 1244 | if (copy_to_user(optval, opt->__data, len)) |
| 1245 | return -EFAULT; |
| 1246 | return 0; |
| 1247 | } |
| 1248 | case IP_PKTINFO: |
| 1249 | val = (inet->cmsg_flags & IP_CMSG_PKTINFO) != 0; |
| 1250 | break; |
| 1251 | case IP_RECVTTL: |
| 1252 | val = (inet->cmsg_flags & IP_CMSG_TTL) != 0; |
| 1253 | break; |
| 1254 | case IP_RECVTOS: |
| 1255 | val = (inet->cmsg_flags & IP_CMSG_TOS) != 0; |
| 1256 | break; |
| 1257 | case IP_RECVOPTS: |
| 1258 | val = (inet->cmsg_flags & IP_CMSG_RECVOPTS) != 0; |
| 1259 | break; |
| 1260 | case IP_RETOPTS: |
| 1261 | val = (inet->cmsg_flags & IP_CMSG_RETOPTS) != 0; |
| 1262 | break; |
| 1263 | case IP_PASSSEC: |
| 1264 | val = (inet->cmsg_flags & IP_CMSG_PASSSEC) != 0; |
| 1265 | break; |
Balazs Scheidler | e8b2dfe | 2008-11-16 19:32:39 -0800 | [diff] [blame] | 1266 | case IP_RECVORIGDSTADDR: |
| 1267 | val = (inet->cmsg_flags & IP_CMSG_ORIGDSTADDR) != 0; |
| 1268 | break; |
Tom Herbert | ad6f939 | 2015-01-05 13:56:17 -0800 | [diff] [blame] | 1269 | case IP_CHECKSUM: |
| 1270 | val = (inet->cmsg_flags & IP_CMSG_CHECKSUM) != 0; |
| 1271 | break; |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1272 | case IP_TOS: |
| 1273 | val = inet->tos; |
| 1274 | break; |
| 1275 | case IP_TTL: |
| 1276 | val = (inet->uc_ttl == -1 ? |
| 1277 | sysctl_ip_default_ttl : |
| 1278 | inet->uc_ttl); |
| 1279 | break; |
| 1280 | case IP_HDRINCL: |
| 1281 | val = inet->hdrincl; |
| 1282 | break; |
Michael Kerrisk | a89b476 | 2010-09-10 20:26:56 +0000 | [diff] [blame] | 1283 | case IP_NODEFRAG: |
| 1284 | val = inet->nodefrag; |
| 1285 | break; |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1286 | case IP_MTU_DISCOVER: |
| 1287 | val = inet->pmtudisc; |
| 1288 | break; |
| 1289 | case IP_MTU: |
| 1290 | { |
| 1291 | struct dst_entry *dst; |
| 1292 | val = 0; |
| 1293 | dst = sk_dst_get(sk); |
| 1294 | if (dst) { |
| 1295 | val = dst_mtu(dst); |
| 1296 | dst_release(dst); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1297 | } |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1298 | if (!val) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1299 | release_sock(sk); |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1300 | return -ENOTCONN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1301 | } |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1302 | break; |
| 1303 | } |
| 1304 | case IP_RECVERR: |
| 1305 | val = inet->recverr; |
| 1306 | break; |
| 1307 | case IP_MULTICAST_TTL: |
| 1308 | val = inet->mc_ttl; |
| 1309 | break; |
| 1310 | case IP_MULTICAST_LOOP: |
| 1311 | val = inet->mc_loop; |
| 1312 | break; |
Erich E. Hoover | 76e2105 | 2012-02-08 09:11:07 +0000 | [diff] [blame] | 1313 | case IP_UNICAST_IF: |
| 1314 | val = (__force int)htonl((__u32) inet->uc_index); |
| 1315 | break; |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1316 | case IP_MULTICAST_IF: |
| 1317 | { |
| 1318 | struct in_addr addr; |
| 1319 | len = min_t(unsigned int, len, sizeof(struct in_addr)); |
| 1320 | addr.s_addr = inet->mc_addr; |
| 1321 | release_sock(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1322 | |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1323 | if (put_user(len, optlen)) |
| 1324 | return -EFAULT; |
| 1325 | if (copy_to_user(optval, &addr, len)) |
| 1326 | return -EFAULT; |
| 1327 | return 0; |
| 1328 | } |
| 1329 | case IP_MSFILTER: |
| 1330 | { |
| 1331 | struct ip_msfilter msf; |
| 1332 | int err; |
| 1333 | |
| 1334 | if (len < IP_MSFILTER_SIZE(0)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1335 | release_sock(sk); |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1336 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1337 | } |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1338 | if (copy_from_user(&msf, optval, IP_MSFILTER_SIZE(0))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1339 | release_sock(sk); |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1340 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1341 | } |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1342 | err = ip_mc_msfget(sk, &msf, |
| 1343 | (struct ip_msfilter __user *)optval, optlen); |
| 1344 | release_sock(sk); |
| 1345 | return err; |
| 1346 | } |
| 1347 | case MCAST_MSFILTER: |
| 1348 | { |
| 1349 | struct group_filter gsf; |
| 1350 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1351 | |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1352 | if (len < GROUP_FILTER_SIZE(0)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1353 | release_sock(sk); |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1354 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1355 | } |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1356 | if (copy_from_user(&gsf, optval, GROUP_FILTER_SIZE(0))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1357 | release_sock(sk); |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1358 | return -EFAULT; |
| 1359 | } |
| 1360 | err = ip_mc_gsfget(sk, &gsf, |
Eric Dumazet | 4d52cfb | 2009-06-02 00:42:16 -0700 | [diff] [blame] | 1361 | (struct group_filter __user *)optval, |
| 1362 | optlen); |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1363 | release_sock(sk); |
| 1364 | return err; |
| 1365 | } |
Nivedita Singhvi | f771bef | 2009-05-28 07:00:46 +0000 | [diff] [blame] | 1366 | case IP_MULTICAST_ALL: |
| 1367 | val = inet->mc_all; |
| 1368 | break; |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1369 | case IP_PKTOPTIONS: |
| 1370 | { |
| 1371 | struct msghdr msg; |
| 1372 | |
| 1373 | release_sock(sk); |
| 1374 | |
| 1375 | if (sk->sk_type != SOCK_STREAM) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1376 | return -ENOPROTOOPT; |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1377 | |
Karoly Kemeny | c54a5e0 | 2014-07-27 12:29:07 +0200 | [diff] [blame] | 1378 | msg.msg_control = (__force void *) optval; |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1379 | msg.msg_controllen = len; |
Daniel Baluta | dd23198e | 2011-08-07 22:31:07 -0700 | [diff] [blame] | 1380 | msg.msg_flags = flags; |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1381 | |
| 1382 | if (inet->cmsg_flags & IP_CMSG_PKTINFO) { |
| 1383 | struct in_pktinfo info; |
| 1384 | |
Eric Dumazet | c720c7e | 2009-10-15 06:30:45 +0000 | [diff] [blame] | 1385 | info.ipi_addr.s_addr = inet->inet_rcv_saddr; |
| 1386 | info.ipi_spec_dst.s_addr = inet->inet_rcv_saddr; |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1387 | info.ipi_ifindex = inet->mc_index; |
| 1388 | put_cmsg(&msg, SOL_IP, IP_PKTINFO, sizeof(info), &info); |
| 1389 | } |
| 1390 | if (inet->cmsg_flags & IP_CMSG_TTL) { |
| 1391 | int hlim = inet->mc_ttl; |
| 1392 | put_cmsg(&msg, SOL_IP, IP_TTL, sizeof(hlim), &hlim); |
| 1393 | } |
Jiri Benc | 4c507d2 | 2012-02-09 09:35:49 +0000 | [diff] [blame] | 1394 | if (inet->cmsg_flags & IP_CMSG_TOS) { |
| 1395 | int tos = inet->rcv_tos; |
| 1396 | put_cmsg(&msg, SOL_IP, IP_TOS, sizeof(tos), &tos); |
| 1397 | } |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1398 | len -= msg.msg_controllen; |
| 1399 | return put_user(len, optlen); |
| 1400 | } |
| 1401 | case IP_FREEBIND: |
| 1402 | val = inet->freebind; |
| 1403 | break; |
KOVACS Krisztian | f5715ae | 2008-10-01 07:30:02 -0700 | [diff] [blame] | 1404 | case IP_TRANSPARENT: |
| 1405 | val = inet->transparent; |
| 1406 | break; |
Stephen Hemminger | d218d11 | 2010-01-11 16:28:01 -0800 | [diff] [blame] | 1407 | case IP_MINTTL: |
| 1408 | val = inet->min_ttl; |
| 1409 | break; |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1410 | default: |
| 1411 | release_sock(sk); |
| 1412 | return -ENOPROTOOPT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1413 | } |
| 1414 | release_sock(sk); |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 1415 | |
Eric Dumazet | 4d52cfb | 2009-06-02 00:42:16 -0700 | [diff] [blame] | 1416 | if (len < sizeof(int) && len > 0 && val >= 0 && val <= 255) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1417 | unsigned char ucval = (unsigned char)val; |
| 1418 | len = 1; |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1419 | if (put_user(len, optlen)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1420 | return -EFAULT; |
Jianjun Kong | 09cb105 | 2008-11-03 00:27:11 -0800 | [diff] [blame] | 1421 | if (copy_to_user(optval, &ucval, 1)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1422 | return -EFAULT; |
| 1423 | } else { |
| 1424 | len = min_t(unsigned int, sizeof(int), len); |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1425 | if (put_user(len, optlen)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1426 | return -EFAULT; |
Jianjun Kong | 09cb105 | 2008-11-03 00:27:11 -0800 | [diff] [blame] | 1427 | if (copy_to_user(optval, &val, len)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1428 | return -EFAULT; |
| 1429 | } |
| 1430 | return 0; |
| 1431 | } |
| 1432 | |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 1433 | int ip_getsockopt(struct sock *sk, int level, |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1434 | int optname, char __user *optval, int __user *optlen) |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 1435 | { |
| 1436 | int err; |
| 1437 | |
Daniel Baluta | dd23198e | 2011-08-07 22:31:07 -0700 | [diff] [blame] | 1438 | err = do_ip_getsockopt(sk, level, optname, optval, optlen, 0); |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 1439 | #ifdef CONFIG_NETFILTER |
| 1440 | /* we need to exclude all possible ENOPROTOOPTs except default case */ |
Pavel Emelyanov | 6a9fb94 | 2007-11-05 21:32:31 -0800 | [diff] [blame] | 1441 | if (err == -ENOPROTOOPT && optname != IP_PKTOPTIONS && |
| 1442 | !ip_mroute_opt(optname)) { |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 1443 | int len; |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 1444 | |
Jianjun Kong | 09cb105 | 2008-11-03 00:27:11 -0800 | [diff] [blame] | 1445 | if (get_user(len, optlen)) |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 1446 | return -EFAULT; |
| 1447 | |
| 1448 | lock_sock(sk); |
| 1449 | err = nf_getsockopt(sk, PF_INET, optname, optval, |
| 1450 | &len); |
| 1451 | release_sock(sk); |
| 1452 | if (err >= 0) |
| 1453 | err = put_user(len, optlen); |
| 1454 | return err; |
| 1455 | } |
| 1456 | #endif |
| 1457 | return err; |
| 1458 | } |
Eric Dumazet | 4d52cfb | 2009-06-02 00:42:16 -0700 | [diff] [blame] | 1459 | EXPORT_SYMBOL(ip_getsockopt); |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 1460 | |
| 1461 | #ifdef CONFIG_COMPAT |
Arnaldo Carvalho de Melo | 543d9cf | 2006-03-20 22:48:35 -0800 | [diff] [blame] | 1462 | int compat_ip_getsockopt(struct sock *sk, int level, int optname, |
| 1463 | char __user *optval, int __user *optlen) |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 1464 | { |
David L Stevens | 42908c6 | 2008-04-29 03:23:22 -0700 | [diff] [blame] | 1465 | int err; |
| 1466 | |
| 1467 | if (optname == MCAST_MSFILTER) |
| 1468 | return compat_mc_getsockopt(sk, level, optname, optval, optlen, |
| 1469 | ip_getsockopt); |
| 1470 | |
Daniel Baluta | dd23198e | 2011-08-07 22:31:07 -0700 | [diff] [blame] | 1471 | err = do_ip_getsockopt(sk, level, optname, optval, optlen, |
| 1472 | MSG_CMSG_COMPAT); |
David L Stevens | 42908c6 | 2008-04-29 03:23:22 -0700 | [diff] [blame] | 1473 | |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 1474 | #ifdef CONFIG_NETFILTER |
| 1475 | /* we need to exclude all possible ENOPROTOOPTs except default case */ |
Pavel Emelyanov | 6a9fb94 | 2007-11-05 21:32:31 -0800 | [diff] [blame] | 1476 | if (err == -ENOPROTOOPT && optname != IP_PKTOPTIONS && |
| 1477 | !ip_mroute_opt(optname)) { |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 1478 | int len; |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 1479 | |
Arnaldo Carvalho de Melo | 543d9cf | 2006-03-20 22:48:35 -0800 | [diff] [blame] | 1480 | if (get_user(len, optlen)) |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 1481 | return -EFAULT; |
| 1482 | |
| 1483 | lock_sock(sk); |
Arnaldo Carvalho de Melo | 543d9cf | 2006-03-20 22:48:35 -0800 | [diff] [blame] | 1484 | err = compat_nf_getsockopt(sk, PF_INET, optname, optval, &len); |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 1485 | release_sock(sk); |
| 1486 | if (err >= 0) |
| 1487 | err = put_user(len, optlen); |
| 1488 | return err; |
| 1489 | } |
| 1490 | #endif |
| 1491 | return err; |
| 1492 | } |
Arnaldo Carvalho de Melo | 543d9cf | 2006-03-20 22:48:35 -0800 | [diff] [blame] | 1493 | EXPORT_SYMBOL(compat_ip_getsockopt); |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 1494 | #endif |