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