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