blob: 9daf2177dc005c339c536c618fedc11259c178bc [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 Hideakie905a9e2007-02-09 23:24:47 +09007 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * 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 Hideakie905a9e2007-02-09 23:24:47 +090013 * Alan Cox : Fixed a couple of oopses in Martin's
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 * TOS tweaks.
15 * Mike McLagan : Routing by source
16 */
17
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/module.h>
19#include <linux/types.h>
20#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/skbuff.h>
22#include <linux/ip.h>
23#include <linux/icmp.h>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020024#include <linux/inetdevice.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/netdevice.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090026#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <net/sock.h>
28#include <net/ip.h>
29#include <net/icmp.h>
Arnaldo Carvalho de Melod83d8462005-12-13 23:26:10 -080030#include <net/tcp_states.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#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 Żenczykowski2c67e9a2011-10-22 00:07:47 -040036#include <net/inet_ecn.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <net/route.h>
38#include <net/xfrm.h>
David L Stevensdae50292008-04-27 01:06:07 -070039#include <net/compat.h>
Eric Dumazetdfd56b82011-12-10 09:48:31 +000040#if IS_ENABLED(CONFIG_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <net/transp_v6.h>
42#endif
David S. Miller35ebf652012-06-28 03:59:11 -070043#include <net/ip_fib.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
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 Zhang2c7946a2006-03-20 22:41:23 -080053#define IP_CMSG_PASSSEC 32
Balazs Scheidlere8b2dfe2008-11-16 19:32:39 -080054#define IP_CMSG_ORIGDSTADDR 64
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
56/*
57 * SOL_IP control messages.
58 */
59
60static void ip_cmsg_recv_pktinfo(struct msghdr *msg, struct sk_buff *skb)
61{
Eric Dumazetd826eb12011-11-09 07:24:35 +000062 struct in_pktinfo info = *PKTINFO_SKB_CB(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -070064 info.ipi_addr.s_addr = ip_hdr(skb)->daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
66 put_cmsg(msg, SOL_IP, IP_PKTINFO, sizeof(info), &info);
67}
68
69static void ip_cmsg_recv_ttl(struct msghdr *msg, struct sk_buff *skb)
70{
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -070071 int ttl = ip_hdr(skb)->ttl;
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 put_cmsg(msg, SOL_IP, IP_TTL, sizeof(int), &ttl);
73}
74
75static void ip_cmsg_recv_tos(struct msghdr *msg, struct sk_buff *skb)
76{
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -070077 put_cmsg(msg, SOL_IP, IP_TOS, 1, &ip_hdr(skb)->tos);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078}
79
80static 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 Meloeddc9ec2007-04-20 22:47:35 -070085 put_cmsg(msg, SOL_IP, IP_RECVOPTS, IPCB(skb)->opt.optlen,
86 ip_hdr(skb) + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087}
88
89
90static void ip_cmsg_recv_retopts(struct msghdr *msg, struct sk_buff *skb)
91{
92 unsigned char optbuf[sizeof(struct ip_options) + 40];
Daniel Baluta5e73ea12012-04-15 01:34:41 +000093 struct ip_options *opt = (struct ip_options *)optbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
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 Zhang2c7946a2006-03-20 22:41:23 -0800107static void ip_cmsg_recv_security(struct msghdr *msg, struct sk_buff *skb)
108{
109 char *secdata;
Catherine Zhangdc49c1f2006-08-02 14:12:06 -0700110 u32 seclen, secid;
Catherine Zhang2c7946a2006-03-20 22:41:23 -0800111 int err;
112
Catherine Zhangdc49c1f2006-08-02 14:12:06 -0700113 err = security_socket_getpeersec_dgram(NULL, skb, &secid);
114 if (err)
115 return;
116
117 err = security_secid_to_secctx(secid, &secdata, &seclen);
Catherine Zhang2c7946a2006-03-20 22:41:23 -0800118 if (err)
119 return;
120
121 put_cmsg(msg, SOL_IP, SCM_SECURITY, seclen, secdata);
Catherine Zhangdc49c1f2006-08-02 14:12:06 -0700122 security_release_secctx(secdata, seclen);
Catherine Zhang2c7946a2006-03-20 22:41:23 -0800123}
124
Harvey Harrison21d1a162008-11-20 01:54:27 -0800125static void ip_cmsg_recv_dstaddr(struct msghdr *msg, struct sk_buff *skb)
Balazs Scheidlere8b2dfe2008-11-16 19:32:39 -0800126{
127 struct sockaddr_in sin;
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000128 const struct iphdr *iph = ip_hdr(skb);
Harvey Harrison21d1a162008-11-20 01:54:27 -0800129 __be16 *ports = (__be16 *)skb_transport_header(skb);
Balazs Scheidlere8b2dfe2008-11-16 19:32:39 -0800130
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 Torvalds1da177e2005-04-16 15:20:36 -0700146
147void ip_cmsg_recv(struct msghdr *msg, struct sk_buff *skb)
148{
149 struct inet_sock *inet = inet_sk(skb->sk);
Eric Dumazet95c96172012-04-15 05:58:06 +0000150 unsigned int flags = inet->cmsg_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
152 /* Ordered by supposed usage frequency */
153 if (flags & 1)
154 ip_cmsg_recv_pktinfo(msg, skb);
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700155 if ((flags >>= 1) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 return;
157
158 if (flags & 1)
159 ip_cmsg_recv_ttl(msg, skb);
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700160 if ((flags >>= 1) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 return;
162
163 if (flags & 1)
164 ip_cmsg_recv_tos(msg, skb);
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700165 if ((flags >>= 1) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 return;
167
168 if (flags & 1)
169 ip_cmsg_recv_opts(msg, skb);
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700170 if ((flags >>= 1) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 return;
172
173 if (flags & 1)
174 ip_cmsg_recv_retopts(msg, skb);
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700175 if ((flags >>= 1) == 0)
Catherine Zhang2c7946a2006-03-20 22:41:23 -0800176 return;
177
178 if (flags & 1)
179 ip_cmsg_recv_security(msg, skb);
Balazs Scheidlere8b2dfe2008-11-16 19:32:39 -0800180
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700181 if ((flags >>= 1) == 0)
Balazs Scheidlere8b2dfe2008-11-16 19:32:39 -0800182 return;
183 if (flags & 1)
184 ip_cmsg_recv_dstaddr(msg, skb);
185
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186}
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700187EXPORT_SYMBOL(ip_cmsg_recv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
Hannes Frederic Sowac8e6ad02014-02-18 21:38:08 +0100189int ip_cmsg_send(struct net *net, struct msghdr *msg, struct ipcm_cookie *ipc,
190 bool allow_ipv6)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191{
Francesco Fuscof02db312013-09-24 15:43:08 +0200192 int err, val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 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 Dumazet5337b5b2014-11-10 17:54:25 -0800198#if IS_ENABLED(CONFIG_IPV6)
Hannes Frederic Sowac8e6ad02014-02-18 21:38:08 +0100199 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 Torvalds1da177e2005-04-16 15:20:36 -0700214 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 Dumazet4d52cfb2009-06-02 00:42:16 -0700219 err = ip_options_get(net, &ipc->opt, CMSG_DATA(cmsg),
220 err < 40 ? err : 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 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 Fuscof02db312013-09-24 15:43:08 +0200234 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 Torvalds1da177e2005-04-16 15:20:36 -0700252 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 Dumazet43a951e2010-10-25 03:32:44 +0000270struct ip_ra_chain __rcu *ip_ra_chain;
Eric Dumazet66018502010-06-07 03:12:08 +0000271static DEFINE_SPINLOCK(ip_ra_lock);
272
Eric Dumazet592fcb92010-06-09 16:21:07 +0000273
274static void ip_ra_destroy_rcu(struct rcu_head *head)
Eric Dumazet66018502010-06-07 03:12:08 +0000275{
Eric Dumazet592fcb92010-06-09 16:21:07 +0000276 struct ip_ra_chain *ra = container_of(head, struct ip_ra_chain, rcu);
277
278 sock_put(ra->saved_sk);
279 kfree(ra);
Eric Dumazet66018502010-06-07 03:12:08 +0000280}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700282int ip_ra_control(struct sock *sk, unsigned char on,
283 void (*destructor)(struct sock *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284{
Eric Dumazet43a951e2010-10-25 03:32:44 +0000285 struct ip_ra_chain *ra, *new_ra;
286 struct ip_ra_chain __rcu **rap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
Eric Dumazetc720c7e82009-10-15 06:30:45 +0000288 if (sk->sk_type != SOCK_RAW || inet_sk(sk)->inet_num == IPPROTO_RAW)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 return -EINVAL;
290
291 new_ra = on ? kmalloc(sizeof(*new_ra), GFP_KERNEL) : NULL;
292
Eric Dumazet66018502010-06-07 03:12:08 +0000293 spin_lock_bh(&ip_ra_lock);
Eric Dumazet43a951e2010-10-25 03:32:44 +0000294 for (rap = &ip_ra_chain;
295 (ra = rcu_dereference_protected(*rap,
296 lockdep_is_held(&ip_ra_lock))) != NULL;
297 rap = &ra->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 if (ra->sk == sk) {
299 if (on) {
Eric Dumazet66018502010-06-07 03:12:08 +0000300 spin_unlock_bh(&ip_ra_lock);
Jesper Juhla51482b2005-11-08 09:41:34 -0800301 kfree(new_ra);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 return -EADDRINUSE;
303 }
Eric Dumazet592fcb92010-06-09 16:21:07 +0000304 /* dont let ip_call_ra_chain() use sk again */
305 ra->sk = NULL;
Eric Dumazet8e380f02014-09-09 08:11:41 -0700306 RCU_INIT_POINTER(*rap, ra->next);
Eric Dumazet66018502010-06-07 03:12:08 +0000307 spin_unlock_bh(&ip_ra_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
309 if (ra->destructor)
310 ra->destructor(sk);
Eric Dumazet592fcb92010-06-09 16:21:07 +0000311 /*
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 Torvalds1da177e2005-04-16 15:20:36 -0700318 return 0;
319 }
320 }
321 if (new_ra == NULL) {
Eric Dumazet66018502010-06-07 03:12:08 +0000322 spin_unlock_bh(&ip_ra_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 return -ENOBUFS;
324 }
325 new_ra->sk = sk;
326 new_ra->destructor = destructor;
327
Eric Dumazet8e380f02014-09-09 08:11:41 -0700328 RCU_INIT_POINTER(new_ra->next, ra);
Eric Dumazet66018502010-06-07 03:12:08 +0000329 rcu_assign_pointer(*rap, new_ra);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 sock_hold(sk);
Eric Dumazet66018502010-06-07 03:12:08 +0000331 spin_unlock_bh(&ip_ra_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
333 return 0;
334}
335
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900336void ip_icmp_error(struct sock *sk, struct sk_buff *skb, int err,
Al Viro35986b32006-09-27 18:34:21 -0700337 __be16 port, u32 info, u8 *payload)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 struct sock_exterr_skb *serr;
340
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 skb = skb_clone(skb, GFP_ATOMIC);
342 if (!skb)
343 return;
344
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900345 serr = SKB_EXT_ERR(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 serr->ee.ee_errno = err;
347 serr->ee.ee_origin = SO_EE_ORIGIN_ICMP;
Arnaldo Carvalho de Melo88c76642007-03-13 14:43:18 -0300348 serr->ee.ee_type = icmp_hdr(skb)->type;
349 serr->ee.ee_code = icmp_hdr(skb)->code;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 serr->ee.ee_pad = 0;
351 serr->ee.ee_info = info;
352 serr->ee.ee_data = 0;
Arnaldo Carvalho de Melo88c76642007-03-13 14:43:18 -0300353 serr->addr_offset = (u8 *)&(((struct iphdr *)(icmp_hdr(skb) + 1))->daddr) -
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700354 skb_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 serr->port = port;
356
Arnaldo Carvalho de Melobd823932007-03-13 17:10:43 -0300357 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 Torvalds1da177e2005-04-16 15:20:36 -0700363}
364
Al Viro05790162006-09-27 18:33:40 -0700365void ip_local_error(struct sock *sk, int err, __be32 daddr, __be16 port, u32 info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366{
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 Melo2ca9e6f2007-03-10 19:15:25 -0300379 skb_put(skb, sizeof(struct iphdr));
380 skb_reset_network_header(skb);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700381 iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 iph->daddr = daddr;
383
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900384 serr = SKB_EXT_ERR(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 serr->ee.ee_errno = err;
386 serr->ee.ee_origin = SO_EE_ORIGIN_LOCAL;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900387 serr->ee.ee_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 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 Melod56f90a2007-04-10 20:50:43 -0700392 serr->addr_offset = (u8 *)&iph->daddr - skb_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 serr->port = port;
394
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700395 __skb_pull(skb, skb_tail_pointer(skb) - skb->data);
Arnaldo Carvalho de Melobd823932007-03-13 17:10:43 -0300396 skb_reset_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
398 if (sock_queue_err_skb(sk, skb))
399 kfree_skb(skb);
400}
401
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900402/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 * Handle MSG_ERRQUEUE
404 */
Hannes Frederic Sowa85fbaa72013-11-23 00:46:12 +0100405int ip_recv_error(struct sock *sk, struct msghdr *msg, int len, int *addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406{
407 struct sock_exterr_skb *serr;
Willem de Bruijn364a9e92014-08-31 21:30:27 -0400408 struct sk_buff *skb;
Steffen Hurrle342dfc32014-01-17 22:53:15 +0100409 DECLARE_SOCKADDR(struct sockaddr_in *, sin, msg->msg_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 struct {
411 struct sock_extended_err ee;
412 struct sockaddr_in offender;
413 } errhdr;
414 int err;
415 int copied;
416
417 err = -EAGAIN;
Willem de Bruijn364a9e92014-08-31 21:30:27 -0400418 skb = sock_dequeue_err_skb(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 if (skb == NULL)
420 goto out;
421
422 copied = skb->len;
423 if (copied > len) {
424 msg->msg_flags |= MSG_TRUNC;
425 copied = len;
426 }
427 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
428 if (err)
429 goto out_free_skb;
430
431 sock_recv_timestamp(msg, sk, skb);
432
433 serr = SKB_EXT_ERR(skb);
434
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 if (sin) {
436 sin->sin_family = AF_INET;
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700437 sin->sin_addr.s_addr = *(__be32 *)(skb_network_header(skb) +
438 serr->addr_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 sin->sin_port = serr->port;
440 memset(&sin->sin_zero, 0, sizeof(sin->sin_zero));
Hannes Frederic Sowa85fbaa72013-11-23 00:46:12 +0100441 *addr_len = sizeof(*sin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 }
443
444 memcpy(&errhdr.ee, &serr->ee, sizeof(struct sock_extended_err));
445 sin = &errhdr.offender;
446 sin->sin_family = AF_UNSPEC;
447 if (serr->ee.ee_origin == SO_EE_ORIGIN_ICMP) {
448 struct inet_sock *inet = inet_sk(sk);
449
450 sin->sin_family = AF_INET;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700451 sin->sin_addr.s_addr = ip_hdr(skb)->saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 sin->sin_port = 0;
453 memset(&sin->sin_zero, 0, sizeof(sin->sin_zero));
454 if (inet->cmsg_flags)
455 ip_cmsg_recv(msg, skb);
456 }
457
458 put_cmsg(msg, SOL_IP, IP_RECVERR, sizeof(errhdr), &errhdr);
459
460 /* Now we could try to dump offended packet options */
461
462 msg->msg_flags |= MSG_ERRQUEUE;
463 err = copied;
464
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900465out_free_skb:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 kfree_skb(skb);
467out:
468 return err;
469}
470
471
472/*
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700473 * Socket option code for IP. This is the end of the line after any
474 * TCP,UDP etc options on an IP socket.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 */
476
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800477static int do_ip_setsockopt(struct sock *sk, int level,
David S. Millerb7058842009-09-30 16:12:20 -0700478 int optname, char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479{
480 struct inet_sock *inet = inet_sk(sk);
Jianjun Kong09cb1052008-11-03 00:27:11 -0800481 int val = 0, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482
Xi Wang0c9f79b2012-11-11 11:20:01 +0000483 switch (optname) {
484 case IP_PKTINFO:
485 case IP_RECVTTL:
486 case IP_RECVOPTS:
487 case IP_RECVTOS:
488 case IP_RETOPTS:
489 case IP_TOS:
490 case IP_TTL:
491 case IP_HDRINCL:
492 case IP_MTU_DISCOVER:
493 case IP_RECVERR:
494 case IP_ROUTER_ALERT:
495 case IP_FREEBIND:
496 case IP_PASSSEC:
497 case IP_TRANSPARENT:
498 case IP_MINTTL:
499 case IP_NODEFRAG:
500 case IP_UNICAST_IF:
501 case IP_MULTICAST_TTL:
502 case IP_MULTICAST_ALL:
503 case IP_MULTICAST_LOOP:
504 case IP_RECVORIGDSTADDR:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 if (optlen >= sizeof(int)) {
506 if (get_user(val, (int __user *) optval))
507 return -EFAULT;
508 } else if (optlen >= sizeof(char)) {
509 unsigned char ucval;
510
511 if (get_user(ucval, (unsigned char __user *) optval))
512 return -EFAULT;
513 val = (int) ucval;
514 }
515 }
516
517 /* If optlen==0, it is equivalent to val == 0 */
518
Pavel Emelyanov6a9fb942007-11-05 21:32:31 -0800519 if (ip_mroute_opt(optname))
Jianjun Kong09cb1052008-11-03 00:27:11 -0800520 return ip_mroute_setsockopt(sk, optname, optval, optlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
522 err = 0;
523 lock_sock(sk);
524
525 switch (optname) {
Stephen Hemminger132adf52007-03-08 20:44:43 -0800526 case IP_OPTIONS:
527 {
Eric Dumazetf6d8bd02011-04-21 09:45:37 +0000528 struct ip_options_rcu *old, *opt = NULL;
529
roel kluin65a1c4f2009-10-23 05:59:21 +0000530 if (optlen > 40)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800531 goto e_inval;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900532 err = ip_options_get_from_user(sock_net(sk), &opt,
Denis V. Lunevcb846632008-03-24 15:31:00 -0700533 optval, optlen);
Stephen Hemminger132adf52007-03-08 20:44:43 -0800534 if (err)
535 break;
Eric Dumazetf6d8bd02011-04-21 09:45:37 +0000536 old = rcu_dereference_protected(inet->inet_opt,
537 sock_owned_by_user(sk));
Stephen Hemminger132adf52007-03-08 20:44:43 -0800538 if (inet->is_icsk) {
539 struct inet_connection_sock *icsk = inet_csk(sk);
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000540#if IS_ENABLED(CONFIG_IPV6)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800541 if (sk->sk_family == PF_INET ||
542 (!((1 << sk->sk_state) &
543 (TCPF_LISTEN | TCPF_CLOSE)) &&
Eric Dumazetc720c7e82009-10-15 06:30:45 +0000544 inet->inet_daddr != LOOPBACK4_IPV6)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545#endif
Eric Dumazetf6d8bd02011-04-21 09:45:37 +0000546 if (old)
547 icsk->icsk_ext_hdr_len -= old->opt.optlen;
Stephen Hemminger132adf52007-03-08 20:44:43 -0800548 if (opt)
Eric Dumazetf6d8bd02011-04-21 09:45:37 +0000549 icsk->icsk_ext_hdr_len += opt->opt.optlen;
Stephen Hemminger132adf52007-03-08 20:44:43 -0800550 icsk->icsk_sync_mss(sk, icsk->icsk_pmtu_cookie);
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000551#if IS_ENABLED(CONFIG_IPV6)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800552 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 }
Eric Dumazetf6d8bd02011-04-21 09:45:37 +0000555 rcu_assign_pointer(inet->inet_opt, opt);
556 if (old)
Paul E. McKenney605b4af2012-01-06 17:08:33 -0800557 kfree_rcu(old, rcu);
Stephen Hemminger132adf52007-03-08 20:44:43 -0800558 break;
559 }
560 case IP_PKTINFO:
561 if (val)
562 inet->cmsg_flags |= IP_CMSG_PKTINFO;
563 else
564 inet->cmsg_flags &= ~IP_CMSG_PKTINFO;
565 break;
566 case IP_RECVTTL:
567 if (val)
568 inet->cmsg_flags |= IP_CMSG_TTL;
569 else
570 inet->cmsg_flags &= ~IP_CMSG_TTL;
571 break;
572 case IP_RECVTOS:
573 if (val)
574 inet->cmsg_flags |= IP_CMSG_TOS;
575 else
576 inet->cmsg_flags &= ~IP_CMSG_TOS;
577 break;
578 case IP_RECVOPTS:
579 if (val)
580 inet->cmsg_flags |= IP_CMSG_RECVOPTS;
581 else
582 inet->cmsg_flags &= ~IP_CMSG_RECVOPTS;
583 break;
584 case IP_RETOPTS:
585 if (val)
586 inet->cmsg_flags |= IP_CMSG_RETOPTS;
587 else
588 inet->cmsg_flags &= ~IP_CMSG_RETOPTS;
589 break;
590 case IP_PASSSEC:
591 if (val)
592 inet->cmsg_flags |= IP_CMSG_PASSSEC;
593 else
594 inet->cmsg_flags &= ~IP_CMSG_PASSSEC;
595 break;
Balazs Scheidlere8b2dfe2008-11-16 19:32:39 -0800596 case IP_RECVORIGDSTADDR:
597 if (val)
598 inet->cmsg_flags |= IP_CMSG_ORIGDSTADDR;
599 else
600 inet->cmsg_flags &= ~IP_CMSG_ORIGDSTADDR;
601 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -0800602 case IP_TOS: /* This sets both TOS and Precedence */
603 if (sk->sk_type == SOCK_STREAM) {
Maciej Żenczykowski2c67e9a2011-10-22 00:07:47 -0400604 val &= ~INET_ECN_MASK;
605 val |= inet->tos & INET_ECN_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 }
Stephen Hemminger132adf52007-03-08 20:44:43 -0800607 if (inet->tos != val) {
608 inet->tos = val;
609 sk->sk_priority = rt_tos2priority(val);
610 sk_dst_reset(sk);
611 }
612 break;
613 case IP_TTL:
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700614 if (optlen < 1)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800615 goto e_inval;
Cong Wangc9be4a52013-01-07 21:17:00 +0000616 if (val != -1 && (val < 1 || val > 255))
Stephen Hemminger132adf52007-03-08 20:44:43 -0800617 goto e_inval;
618 inet->uc_ttl = val;
619 break;
620 case IP_HDRINCL:
621 if (sk->sk_type != SOCK_RAW) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 err = -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -0800624 }
625 inet->hdrincl = val ? 1 : 0;
626 break;
Jiri Olsa7b2ff182010-06-15 01:07:31 +0000627 case IP_NODEFRAG:
628 if (sk->sk_type != SOCK_RAW) {
629 err = -ENOPROTOOPT;
630 break;
631 }
632 inet->nodefrag = val ? 1 : 0;
633 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -0800634 case IP_MTU_DISCOVER:
Hannes Frederic Sowa1b346572014-02-26 01:20:42 +0100635 if (val < IP_PMTUDISC_DONT || val > IP_PMTUDISC_OMIT)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800636 goto e_inval;
637 inet->pmtudisc = val;
638 break;
639 case IP_RECVERR:
640 inet->recverr = !!val;
641 if (!val)
642 skb_queue_purge(&sk->sk_error_queue);
643 break;
644 case IP_MULTICAST_TTL:
645 if (sk->sk_type == SOCK_STREAM)
646 goto e_inval;
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700647 if (optlen < 1)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800648 goto e_inval;
Jianjun Kong09cb1052008-11-03 00:27:11 -0800649 if (val == -1)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800650 val = 1;
651 if (val < 0 || val > 255)
652 goto e_inval;
653 inet->mc_ttl = val;
654 break;
655 case IP_MULTICAST_LOOP:
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700656 if (optlen < 1)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800657 goto e_inval;
658 inet->mc_loop = !!val;
659 break;
Erich E. Hoover76e21052012-02-08 09:11:07 +0000660 case IP_UNICAST_IF:
661 {
662 struct net_device *dev = NULL;
663 int ifindex;
664
665 if (optlen != sizeof(int))
666 goto e_inval;
667
668 ifindex = (__force int)ntohl((__force __be32)val);
669 if (ifindex == 0) {
670 inet->uc_index = 0;
671 err = 0;
672 break;
673 }
674
675 dev = dev_get_by_index(sock_net(sk), ifindex);
676 err = -EADDRNOTAVAIL;
677 if (!dev)
678 break;
679 dev_put(dev);
680
681 err = -EINVAL;
682 if (sk->sk_bound_dev_if)
683 break;
684
685 inet->uc_index = ifindex;
686 err = 0;
687 break;
688 }
Stephen Hemminger132adf52007-03-08 20:44:43 -0800689 case IP_MULTICAST_IF:
690 {
691 struct ip_mreqn mreq;
692 struct net_device *dev = NULL;
693
694 if (sk->sk_type == SOCK_STREAM)
695 goto e_inval;
696 /*
697 * Check the arguments are allowable
698 */
699
Shan Wei09159212009-09-22 15:41:10 +0000700 if (optlen < sizeof(struct in_addr))
701 goto e_inval;
702
Stephen Hemminger132adf52007-03-08 20:44:43 -0800703 err = -EFAULT;
704 if (optlen >= sizeof(struct ip_mreqn)) {
Jianjun Kong09cb1052008-11-03 00:27:11 -0800705 if (copy_from_user(&mreq, optval, sizeof(mreq)))
Stephen Hemminger132adf52007-03-08 20:44:43 -0800706 break;
707 } else {
708 memset(&mreq, 0, sizeof(mreq));
Jiri Pirko3a084dd2012-05-03 22:37:45 +0000709 if (optlen >= sizeof(struct ip_mreq)) {
710 if (copy_from_user(&mreq, optval,
711 sizeof(struct ip_mreq)))
712 break;
713 } else if (optlen >= sizeof(struct in_addr)) {
714 if (copy_from_user(&mreq.imr_address, optval,
715 sizeof(struct in_addr)))
716 break;
717 }
Stephen Hemminger132adf52007-03-08 20:44:43 -0800718 }
719
720 if (!mreq.imr_ifindex) {
Al Viroe6f1ceb2008-03-17 22:44:53 -0700721 if (mreq.imr_address.s_addr == htonl(INADDR_ANY)) {
Stephen Hemminger132adf52007-03-08 20:44:43 -0800722 inet->mc_index = 0;
723 inet->mc_addr = 0;
724 err = 0;
725 break;
726 }
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900727 dev = ip_dev_find(sock_net(sk), mreq.imr_address.s_addr);
Eric Dumazet55b80502009-10-19 06:41:58 +0000728 if (dev)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800729 mreq.imr_ifindex = dev->ifindex;
Stephen Hemminger132adf52007-03-08 20:44:43 -0800730 } else
Eric Dumazet55b80502009-10-19 06:41:58 +0000731 dev = dev_get_by_index(sock_net(sk), mreq.imr_ifindex);
Stephen Hemminger132adf52007-03-08 20:44:43 -0800732
733
734 err = -EADDRNOTAVAIL;
735 if (!dev)
736 break;
Eric Dumazet55b80502009-10-19 06:41:58 +0000737 dev_put(dev);
Stephen Hemminger132adf52007-03-08 20:44:43 -0800738
739 err = -EINVAL;
740 if (sk->sk_bound_dev_if &&
741 mreq.imr_ifindex != sk->sk_bound_dev_if)
742 break;
743
744 inet->mc_index = mreq.imr_ifindex;
745 inet->mc_addr = mreq.imr_address.s_addr;
746 err = 0;
747 break;
748 }
749
750 case IP_ADD_MEMBERSHIP:
751 case IP_DROP_MEMBERSHIP:
752 {
753 struct ip_mreqn mreq;
754
Flavio Leitnera96fb492007-08-24 22:16:39 -0700755 err = -EPROTO;
756 if (inet_sk(sk)->is_icsk)
757 break;
758
Stephen Hemminger132adf52007-03-08 20:44:43 -0800759 if (optlen < sizeof(struct ip_mreq))
760 goto e_inval;
761 err = -EFAULT;
762 if (optlen >= sizeof(struct ip_mreqn)) {
Jianjun Kong09cb1052008-11-03 00:27:11 -0800763 if (copy_from_user(&mreq, optval, sizeof(mreq)))
Stephen Hemminger132adf52007-03-08 20:44:43 -0800764 break;
765 } else {
766 memset(&mreq, 0, sizeof(mreq));
Jianjun Kong09cb1052008-11-03 00:27:11 -0800767 if (copy_from_user(&mreq, optval, sizeof(struct ip_mreq)))
Stephen Hemminger132adf52007-03-08 20:44:43 -0800768 break;
769 }
770
771 if (optname == IP_ADD_MEMBERSHIP)
772 err = ip_mc_join_group(sk, &mreq);
773 else
774 err = ip_mc_leave_group(sk, &mreq);
775 break;
776 }
777 case IP_MSFILTER:
778 {
Stephen Hemminger132adf52007-03-08 20:44:43 -0800779 struct ip_msfilter *msf;
780
781 if (optlen < IP_MSFILTER_SIZE(0))
782 goto e_inval;
783 if (optlen > sysctl_optmem_max) {
784 err = -ENOBUFS;
785 break;
786 }
787 msf = kmalloc(optlen, GFP_KERNEL);
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -0700788 if (!msf) {
Stephen Hemminger132adf52007-03-08 20:44:43 -0800789 err = -ENOBUFS;
790 break;
791 }
792 err = -EFAULT;
793 if (copy_from_user(msf, optval, optlen)) {
794 kfree(msf);
795 break;
796 }
797 /* numsrc >= (1G-4) overflow in 32 bits */
798 if (msf->imsf_numsrc >= 0x3ffffffcU ||
799 msf->imsf_numsrc > sysctl_igmp_max_msf) {
800 kfree(msf);
801 err = -ENOBUFS;
802 break;
803 }
804 if (IP_MSFILTER_SIZE(msf->imsf_numsrc) > optlen) {
805 kfree(msf);
806 err = -EINVAL;
807 break;
808 }
809 err = ip_mc_msfilter(sk, msf, 0);
810 kfree(msf);
811 break;
812 }
813 case IP_BLOCK_SOURCE:
814 case IP_UNBLOCK_SOURCE:
815 case IP_ADD_SOURCE_MEMBERSHIP:
816 case IP_DROP_SOURCE_MEMBERSHIP:
817 {
818 struct ip_mreq_source mreqs;
819 int omode, add;
820
821 if (optlen != sizeof(struct ip_mreq_source))
822 goto e_inval;
823 if (copy_from_user(&mreqs, optval, sizeof(mreqs))) {
824 err = -EFAULT;
825 break;
826 }
827 if (optname == IP_BLOCK_SOURCE) {
828 omode = MCAST_EXCLUDE;
829 add = 1;
830 } else if (optname == IP_UNBLOCK_SOURCE) {
831 omode = MCAST_EXCLUDE;
832 add = 0;
833 } else if (optname == IP_ADD_SOURCE_MEMBERSHIP) {
834 struct ip_mreqn mreq;
835
836 mreq.imr_multiaddr.s_addr = mreqs.imr_multiaddr;
837 mreq.imr_address.s_addr = mreqs.imr_interface;
838 mreq.imr_ifindex = 0;
839 err = ip_mc_join_group(sk, &mreq);
840 if (err && err != -EADDRINUSE)
841 break;
842 omode = MCAST_INCLUDE;
843 add = 1;
844 } else /* IP_DROP_SOURCE_MEMBERSHIP */ {
845 omode = MCAST_INCLUDE;
846 add = 0;
847 }
848 err = ip_mc_source(add, omode, sk, &mreqs, 0);
849 break;
850 }
851 case MCAST_JOIN_GROUP:
852 case MCAST_LEAVE_GROUP:
853 {
854 struct group_req greq;
855 struct sockaddr_in *psin;
856 struct ip_mreqn mreq;
857
858 if (optlen < sizeof(struct group_req))
859 goto e_inval;
860 err = -EFAULT;
861 if (copy_from_user(&greq, optval, sizeof(greq)))
862 break;
863 psin = (struct sockaddr_in *)&greq.gr_group;
864 if (psin->sin_family != AF_INET)
865 goto e_inval;
866 memset(&mreq, 0, sizeof(mreq));
867 mreq.imr_multiaddr = psin->sin_addr;
868 mreq.imr_ifindex = greq.gr_interface;
869
870 if (optname == MCAST_JOIN_GROUP)
871 err = ip_mc_join_group(sk, &mreq);
872 else
873 err = ip_mc_leave_group(sk, &mreq);
874 break;
875 }
876 case MCAST_JOIN_SOURCE_GROUP:
877 case MCAST_LEAVE_SOURCE_GROUP:
878 case MCAST_BLOCK_SOURCE:
879 case MCAST_UNBLOCK_SOURCE:
880 {
881 struct group_source_req greqs;
882 struct ip_mreq_source mreqs;
883 struct sockaddr_in *psin;
884 int omode, add;
885
886 if (optlen != sizeof(struct group_source_req))
887 goto e_inval;
888 if (copy_from_user(&greqs, optval, sizeof(greqs))) {
889 err = -EFAULT;
890 break;
891 }
892 if (greqs.gsr_group.ss_family != AF_INET ||
893 greqs.gsr_source.ss_family != AF_INET) {
894 err = -EADDRNOTAVAIL;
895 break;
896 }
897 psin = (struct sockaddr_in *)&greqs.gsr_group;
898 mreqs.imr_multiaddr = psin->sin_addr.s_addr;
899 psin = (struct sockaddr_in *)&greqs.gsr_source;
900 mreqs.imr_sourceaddr = psin->sin_addr.s_addr;
901 mreqs.imr_interface = 0; /* use index for mc_source */
902
903 if (optname == MCAST_BLOCK_SOURCE) {
904 omode = MCAST_EXCLUDE;
905 add = 1;
906 } else if (optname == MCAST_UNBLOCK_SOURCE) {
907 omode = MCAST_EXCLUDE;
908 add = 0;
909 } else if (optname == MCAST_JOIN_SOURCE_GROUP) {
910 struct ip_mreqn mreq;
911
912 psin = (struct sockaddr_in *)&greqs.gsr_group;
913 mreq.imr_multiaddr = psin->sin_addr;
914 mreq.imr_address.s_addr = 0;
915 mreq.imr_ifindex = greqs.gsr_interface;
916 err = ip_mc_join_group(sk, &mreq);
917 if (err && err != -EADDRINUSE)
918 break;
919 greqs.gsr_interface = mreq.imr_ifindex;
920 omode = MCAST_INCLUDE;
921 add = 1;
922 } else /* MCAST_LEAVE_SOURCE_GROUP */ {
923 omode = MCAST_INCLUDE;
924 add = 0;
925 }
926 err = ip_mc_source(add, omode, sk, &mreqs,
927 greqs.gsr_interface);
928 break;
929 }
930 case MCAST_MSFILTER:
931 {
Stephen Hemminger132adf52007-03-08 20:44:43 -0800932 struct sockaddr_in *psin;
933 struct ip_msfilter *msf = NULL;
934 struct group_filter *gsf = NULL;
935 int msize, i, ifindex;
936
937 if (optlen < GROUP_FILTER_SIZE(0))
938 goto e_inval;
939 if (optlen > sysctl_optmem_max) {
940 err = -ENOBUFS;
941 break;
942 }
Jianjun Kong09cb1052008-11-03 00:27:11 -0800943 gsf = kmalloc(optlen, GFP_KERNEL);
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -0700944 if (!gsf) {
Stephen Hemminger132adf52007-03-08 20:44:43 -0800945 err = -ENOBUFS;
946 break;
947 }
948 err = -EFAULT;
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700949 if (copy_from_user(gsf, optval, optlen))
Stephen Hemminger132adf52007-03-08 20:44:43 -0800950 goto mc_msf_out;
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700951
Stephen Hemminger132adf52007-03-08 20:44:43 -0800952 /* numsrc >= (4G-140)/128 overflow in 32 bits */
953 if (gsf->gf_numsrc >= 0x1ffffff ||
954 gsf->gf_numsrc > sysctl_igmp_max_msf) {
955 err = -ENOBUFS;
956 goto mc_msf_out;
957 }
958 if (GROUP_FILTER_SIZE(gsf->gf_numsrc) > optlen) {
959 err = -EINVAL;
960 goto mc_msf_out;
961 }
962 msize = IP_MSFILTER_SIZE(gsf->gf_numsrc);
Jianjun Kong09cb1052008-11-03 00:27:11 -0800963 msf = kmalloc(msize, GFP_KERNEL);
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -0700964 if (!msf) {
Stephen Hemminger132adf52007-03-08 20:44:43 -0800965 err = -ENOBUFS;
966 goto mc_msf_out;
967 }
968 ifindex = gsf->gf_interface;
969 psin = (struct sockaddr_in *)&gsf->gf_group;
970 if (psin->sin_family != AF_INET) {
971 err = -EADDRNOTAVAIL;
972 goto mc_msf_out;
973 }
974 msf->imsf_multiaddr = psin->sin_addr.s_addr;
975 msf->imsf_interface = 0;
976 msf->imsf_fmode = gsf->gf_fmode;
977 msf->imsf_numsrc = gsf->gf_numsrc;
978 err = -EADDRNOTAVAIL;
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700979 for (i = 0; i < gsf->gf_numsrc; ++i) {
Stephen Hemminger132adf52007-03-08 20:44:43 -0800980 psin = (struct sockaddr_in *)&gsf->gf_slist[i];
981
982 if (psin->sin_family != AF_INET)
983 goto mc_msf_out;
984 msf->imsf_slist[i] = psin->sin_addr.s_addr;
985 }
986 kfree(gsf);
987 gsf = NULL;
988
989 err = ip_mc_msfilter(sk, msf, ifindex);
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700990mc_msf_out:
Stephen Hemminger132adf52007-03-08 20:44:43 -0800991 kfree(msf);
992 kfree(gsf);
993 break;
994 }
Nivedita Singhvif771bef2009-05-28 07:00:46 +0000995 case IP_MULTICAST_ALL:
996 if (optlen < 1)
997 goto e_inval;
998 if (val != 0 && val != 1)
999 goto e_inval;
1000 inet->mc_all = val;
1001 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001002 case IP_ROUTER_ALERT:
1003 err = ip_ra_control(sk, val ? 1 : 0, NULL);
1004 break;
1005
1006 case IP_FREEBIND:
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001007 if (optlen < 1)
Stephen Hemminger132adf52007-03-08 20:44:43 -08001008 goto e_inval;
1009 inet->freebind = !!val;
1010 break;
1011
1012 case IP_IPSEC_POLICY:
1013 case IP_XFRM_POLICY:
1014 err = -EPERM;
Eric W. Biederman52e804c2012-11-16 03:03:05 +00001015 if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001016 break;
1017 err = xfrm_user_policy(sk, optname, optval, optlen);
1018 break;
1019
KOVACS Krisztianf5715ae2008-10-01 07:30:02 -07001020 case IP_TRANSPARENT:
Eric W. Biederman52e804c2012-11-16 03:03:05 +00001021 if (!!val && !ns_capable(sock_net(sk)->user_ns, CAP_NET_RAW) &&
1022 !ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN)) {
KOVACS Krisztianf5715ae2008-10-01 07:30:02 -07001023 err = -EPERM;
1024 break;
1025 }
1026 if (optlen < 1)
1027 goto e_inval;
1028 inet->transparent = !!val;
1029 break;
1030
Stephen Hemmingerd218d112010-01-11 16:28:01 -08001031 case IP_MINTTL:
1032 if (optlen < 1)
1033 goto e_inval;
1034 if (val < 0 || val > 255)
1035 goto e_inval;
1036 inet->min_ttl = val;
1037 break;
1038
Stephen Hemminger132adf52007-03-08 20:44:43 -08001039 default:
1040 err = -ENOPROTOOPT;
1041 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 }
1043 release_sock(sk);
1044 return err;
1045
1046e_inval:
1047 release_sock(sk);
1048 return -EINVAL;
1049}
1050
Eric Dumazetf84af322010-04-28 15:31:51 -07001051/**
Eric Dumazetd826eb12011-11-09 07:24:35 +00001052 * ipv4_pktinfo_prepare - transfert some info from rtable to skb
Eric Dumazetf84af322010-04-28 15:31:51 -07001053 * @sk: socket
1054 * @skb: buffer
1055 *
David S. Miller35ebf652012-06-28 03:59:11 -07001056 * To support IP_CMSG_PKTINFO option, we store rt_iif and specific
1057 * destination in skb->cb[] before dst drop.
stephen hemminger8e3bff92013-12-08 12:15:44 -08001058 * This way, receiver doesn't make cache line misses to read rtable.
Eric Dumazetf84af322010-04-28 15:31:51 -07001059 */
Shawn Bohrerfbf88662013-10-07 11:01:40 -05001060void ipv4_pktinfo_prepare(const struct sock *sk, struct sk_buff *skb)
Eric Dumazetf84af322010-04-28 15:31:51 -07001061{
Eric Dumazetd826eb12011-11-09 07:24:35 +00001062 struct in_pktinfo *pktinfo = PKTINFO_SKB_CB(skb);
Hannes Frederic Sowa4b261c752014-01-20 03:43:08 +01001063 bool prepare = (inet_sk(sk)->cmsg_flags & IP_CMSG_PKTINFO) ||
1064 ipv6_sk_rxinfo(sk);
Eric Dumazetd826eb12011-11-09 07:24:35 +00001065
Hannes Frederic Sowa4b261c752014-01-20 03:43:08 +01001066 if (prepare && skb_rtable(skb)) {
David S. Miller92101b32012-07-23 16:29:00 -07001067 pktinfo->ipi_ifindex = inet_iif(skb);
David S. Miller35ebf652012-06-28 03:59:11 -07001068 pktinfo->ipi_spec_dst.s_addr = fib_compute_spec_dst(skb);
Eric Dumazetd826eb12011-11-09 07:24:35 +00001069 } else {
1070 pktinfo->ipi_ifindex = 0;
1071 pktinfo->ipi_spec_dst.s_addr = 0;
1072 }
1073 skb_dst_drop(skb);
Eric Dumazetf84af322010-04-28 15:31:51 -07001074}
Eric Dumazetf84af322010-04-28 15:31:51 -07001075
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001076int ip_setsockopt(struct sock *sk, int level,
David S. Millerb7058842009-09-30 16:12:20 -07001077 int optname, char __user *optval, unsigned int optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001078{
1079 int err;
1080
1081 if (level != SOL_IP)
1082 return -ENOPROTOOPT;
1083
1084 err = do_ip_setsockopt(sk, level, optname, optval, optlen);
1085#ifdef CONFIG_NETFILTER
1086 /* we need to exclude all possible ENOPROTOOPTs except default case */
1087 if (err == -ENOPROTOOPT && optname != IP_HDRINCL &&
Pavel Emelyanov6a9fb942007-11-05 21:32:31 -08001088 optname != IP_IPSEC_POLICY &&
1089 optname != IP_XFRM_POLICY &&
1090 !ip_mroute_opt(optname)) {
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001091 lock_sock(sk);
1092 err = nf_setsockopt(sk, PF_INET, optname, optval, optlen);
1093 release_sock(sk);
1094 }
1095#endif
1096 return err;
1097}
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001098EXPORT_SYMBOL(ip_setsockopt);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001099
1100#ifdef CONFIG_COMPAT
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001101int compat_ip_setsockopt(struct sock *sk, int level, int optname,
David S. Millerb7058842009-09-30 16:12:20 -07001102 char __user *optval, unsigned int optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001103{
1104 int err;
1105
1106 if (level != SOL_IP)
1107 return -ENOPROTOOPT;
1108
David L Stevensdae50292008-04-27 01:06:07 -07001109 if (optname >= MCAST_JOIN_GROUP && optname <= MCAST_MSFILTER)
1110 return compat_mc_setsockopt(sk, level, optname, optval, optlen,
1111 ip_setsockopt);
1112
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001113 err = do_ip_setsockopt(sk, level, optname, optval, optlen);
1114#ifdef CONFIG_NETFILTER
1115 /* we need to exclude all possible ENOPROTOOPTs except default case */
1116 if (err == -ENOPROTOOPT && optname != IP_HDRINCL &&
Pavel Emelyanov6a9fb942007-11-05 21:32:31 -08001117 optname != IP_IPSEC_POLICY &&
1118 optname != IP_XFRM_POLICY &&
1119 !ip_mroute_opt(optname)) {
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001120 lock_sock(sk);
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001121 err = compat_nf_setsockopt(sk, PF_INET, optname,
1122 optval, optlen);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001123 release_sock(sk);
1124 }
1125#endif
1126 return err;
1127}
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001128EXPORT_SYMBOL(compat_ip_setsockopt);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001129#endif
1130
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131/*
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001132 * Get the options. Note for future reference. The GET of IP options gets
1133 * the _received_ ones. The set sets the _sent_ ones.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 */
1135
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001136static int do_ip_getsockopt(struct sock *sk, int level, int optname,
Eric Dumazet95c96172012-04-15 05:58:06 +00001137 char __user *optval, int __user *optlen, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138{
1139 struct inet_sock *inet = inet_sk(sk);
1140 int val;
1141 int len;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001142
Stephen Hemminger132adf52007-03-08 20:44:43 -08001143 if (level != SOL_IP)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 return -EOPNOTSUPP;
1145
Pavel Emelyanov6a9fb942007-11-05 21:32:31 -08001146 if (ip_mroute_opt(optname))
Jianjun Kong09cb1052008-11-03 00:27:11 -08001147 return ip_mroute_getsockopt(sk, optname, optval, optlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148
Jianjun Kong09cb1052008-11-03 00:27:11 -08001149 if (get_user(len, optlen))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 return -EFAULT;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001151 if (len < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 return -EINVAL;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001153
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 lock_sock(sk);
1155
Stephen Hemminger132adf52007-03-08 20:44:43 -08001156 switch (optname) {
1157 case IP_OPTIONS:
1158 {
1159 unsigned char optbuf[sizeof(struct ip_options)+40];
Eric Dumazetf6d8bd02011-04-21 09:45:37 +00001160 struct ip_options *opt = (struct ip_options *)optbuf;
1161 struct ip_options_rcu *inet_opt;
1162
1163 inet_opt = rcu_dereference_protected(inet->inet_opt,
1164 sock_owned_by_user(sk));
Stephen Hemminger132adf52007-03-08 20:44:43 -08001165 opt->optlen = 0;
Eric Dumazetf6d8bd02011-04-21 09:45:37 +00001166 if (inet_opt)
1167 memcpy(optbuf, &inet_opt->opt,
1168 sizeof(struct ip_options) +
1169 inet_opt->opt.optlen);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001170 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171
Stephen Hemminger132adf52007-03-08 20:44:43 -08001172 if (opt->optlen == 0)
1173 return put_user(0, optlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174
Stephen Hemminger132adf52007-03-08 20:44:43 -08001175 ip_options_undo(opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176
Stephen Hemminger132adf52007-03-08 20:44:43 -08001177 len = min_t(unsigned int, len, opt->optlen);
1178 if (put_user(len, optlen))
1179 return -EFAULT;
1180 if (copy_to_user(optval, opt->__data, len))
1181 return -EFAULT;
1182 return 0;
1183 }
1184 case IP_PKTINFO:
1185 val = (inet->cmsg_flags & IP_CMSG_PKTINFO) != 0;
1186 break;
1187 case IP_RECVTTL:
1188 val = (inet->cmsg_flags & IP_CMSG_TTL) != 0;
1189 break;
1190 case IP_RECVTOS:
1191 val = (inet->cmsg_flags & IP_CMSG_TOS) != 0;
1192 break;
1193 case IP_RECVOPTS:
1194 val = (inet->cmsg_flags & IP_CMSG_RECVOPTS) != 0;
1195 break;
1196 case IP_RETOPTS:
1197 val = (inet->cmsg_flags & IP_CMSG_RETOPTS) != 0;
1198 break;
1199 case IP_PASSSEC:
1200 val = (inet->cmsg_flags & IP_CMSG_PASSSEC) != 0;
1201 break;
Balazs Scheidlere8b2dfe2008-11-16 19:32:39 -08001202 case IP_RECVORIGDSTADDR:
1203 val = (inet->cmsg_flags & IP_CMSG_ORIGDSTADDR) != 0;
1204 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001205 case IP_TOS:
1206 val = inet->tos;
1207 break;
1208 case IP_TTL:
1209 val = (inet->uc_ttl == -1 ?
1210 sysctl_ip_default_ttl :
1211 inet->uc_ttl);
1212 break;
1213 case IP_HDRINCL:
1214 val = inet->hdrincl;
1215 break;
Michael Kerriska89b4762010-09-10 20:26:56 +00001216 case IP_NODEFRAG:
1217 val = inet->nodefrag;
1218 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001219 case IP_MTU_DISCOVER:
1220 val = inet->pmtudisc;
1221 break;
1222 case IP_MTU:
1223 {
1224 struct dst_entry *dst;
1225 val = 0;
1226 dst = sk_dst_get(sk);
1227 if (dst) {
1228 val = dst_mtu(dst);
1229 dst_release(dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001231 if (!val) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 release_sock(sk);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001233 return -ENOTCONN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001235 break;
1236 }
1237 case IP_RECVERR:
1238 val = inet->recverr;
1239 break;
1240 case IP_MULTICAST_TTL:
1241 val = inet->mc_ttl;
1242 break;
1243 case IP_MULTICAST_LOOP:
1244 val = inet->mc_loop;
1245 break;
Erich E. Hoover76e21052012-02-08 09:11:07 +00001246 case IP_UNICAST_IF:
1247 val = (__force int)htonl((__u32) inet->uc_index);
1248 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001249 case IP_MULTICAST_IF:
1250 {
1251 struct in_addr addr;
1252 len = min_t(unsigned int, len, sizeof(struct in_addr));
1253 addr.s_addr = inet->mc_addr;
1254 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255
Stephen Hemminger132adf52007-03-08 20:44:43 -08001256 if (put_user(len, optlen))
1257 return -EFAULT;
1258 if (copy_to_user(optval, &addr, len))
1259 return -EFAULT;
1260 return 0;
1261 }
1262 case IP_MSFILTER:
1263 {
1264 struct ip_msfilter msf;
1265 int err;
1266
1267 if (len < IP_MSFILTER_SIZE(0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 release_sock(sk);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001269 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001271 if (copy_from_user(&msf, optval, IP_MSFILTER_SIZE(0))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 release_sock(sk);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001273 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001275 err = ip_mc_msfget(sk, &msf,
1276 (struct ip_msfilter __user *)optval, optlen);
1277 release_sock(sk);
1278 return err;
1279 }
1280 case MCAST_MSFILTER:
1281 {
1282 struct group_filter gsf;
1283 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284
Stephen Hemminger132adf52007-03-08 20:44:43 -08001285 if (len < GROUP_FILTER_SIZE(0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 release_sock(sk);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001287 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001289 if (copy_from_user(&gsf, optval, GROUP_FILTER_SIZE(0))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 release_sock(sk);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001291 return -EFAULT;
1292 }
1293 err = ip_mc_gsfget(sk, &gsf,
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001294 (struct group_filter __user *)optval,
1295 optlen);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001296 release_sock(sk);
1297 return err;
1298 }
Nivedita Singhvif771bef2009-05-28 07:00:46 +00001299 case IP_MULTICAST_ALL:
1300 val = inet->mc_all;
1301 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001302 case IP_PKTOPTIONS:
1303 {
1304 struct msghdr msg;
1305
1306 release_sock(sk);
1307
1308 if (sk->sk_type != SOCK_STREAM)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 return -ENOPROTOOPT;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001310
Karoly Kemenyc54a5e02014-07-27 12:29:07 +02001311 msg.msg_control = (__force void *) optval;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001312 msg.msg_controllen = len;
Daniel Balutadd23198e2011-08-07 22:31:07 -07001313 msg.msg_flags = flags;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001314
1315 if (inet->cmsg_flags & IP_CMSG_PKTINFO) {
1316 struct in_pktinfo info;
1317
Eric Dumazetc720c7e82009-10-15 06:30:45 +00001318 info.ipi_addr.s_addr = inet->inet_rcv_saddr;
1319 info.ipi_spec_dst.s_addr = inet->inet_rcv_saddr;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001320 info.ipi_ifindex = inet->mc_index;
1321 put_cmsg(&msg, SOL_IP, IP_PKTINFO, sizeof(info), &info);
1322 }
1323 if (inet->cmsg_flags & IP_CMSG_TTL) {
1324 int hlim = inet->mc_ttl;
1325 put_cmsg(&msg, SOL_IP, IP_TTL, sizeof(hlim), &hlim);
1326 }
Jiri Benc4c507d22012-02-09 09:35:49 +00001327 if (inet->cmsg_flags & IP_CMSG_TOS) {
1328 int tos = inet->rcv_tos;
1329 put_cmsg(&msg, SOL_IP, IP_TOS, sizeof(tos), &tos);
1330 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001331 len -= msg.msg_controllen;
1332 return put_user(len, optlen);
1333 }
1334 case IP_FREEBIND:
1335 val = inet->freebind;
1336 break;
KOVACS Krisztianf5715ae2008-10-01 07:30:02 -07001337 case IP_TRANSPARENT:
1338 val = inet->transparent;
1339 break;
Stephen Hemmingerd218d112010-01-11 16:28:01 -08001340 case IP_MINTTL:
1341 val = inet->min_ttl;
1342 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001343 default:
1344 release_sock(sk);
1345 return -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 }
1347 release_sock(sk);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001348
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001349 if (len < sizeof(int) && len > 0 && val >= 0 && val <= 255) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 unsigned char ucval = (unsigned char)val;
1351 len = 1;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001352 if (put_user(len, optlen))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 return -EFAULT;
Jianjun Kong09cb1052008-11-03 00:27:11 -08001354 if (copy_to_user(optval, &ucval, 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 return -EFAULT;
1356 } else {
1357 len = min_t(unsigned int, sizeof(int), len);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001358 if (put_user(len, optlen))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 return -EFAULT;
Jianjun Kong09cb1052008-11-03 00:27:11 -08001360 if (copy_to_user(optval, &val, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 return -EFAULT;
1362 }
1363 return 0;
1364}
1365
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001366int ip_getsockopt(struct sock *sk, int level,
Stephen Hemminger132adf52007-03-08 20:44:43 -08001367 int optname, char __user *optval, int __user *optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001368{
1369 int err;
1370
Daniel Balutadd23198e2011-08-07 22:31:07 -07001371 err = do_ip_getsockopt(sk, level, optname, optval, optlen, 0);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001372#ifdef CONFIG_NETFILTER
1373 /* we need to exclude all possible ENOPROTOOPTs except default case */
Pavel Emelyanov6a9fb942007-11-05 21:32:31 -08001374 if (err == -ENOPROTOOPT && optname != IP_PKTOPTIONS &&
1375 !ip_mroute_opt(optname)) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001376 int len;
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001377
Jianjun Kong09cb1052008-11-03 00:27:11 -08001378 if (get_user(len, optlen))
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001379 return -EFAULT;
1380
1381 lock_sock(sk);
1382 err = nf_getsockopt(sk, PF_INET, optname, optval,
1383 &len);
1384 release_sock(sk);
1385 if (err >= 0)
1386 err = put_user(len, optlen);
1387 return err;
1388 }
1389#endif
1390 return err;
1391}
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001392EXPORT_SYMBOL(ip_getsockopt);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001393
1394#ifdef CONFIG_COMPAT
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001395int compat_ip_getsockopt(struct sock *sk, int level, int optname,
1396 char __user *optval, int __user *optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001397{
David L Stevens42908c62008-04-29 03:23:22 -07001398 int err;
1399
1400 if (optname == MCAST_MSFILTER)
1401 return compat_mc_getsockopt(sk, level, optname, optval, optlen,
1402 ip_getsockopt);
1403
Daniel Balutadd23198e2011-08-07 22:31:07 -07001404 err = do_ip_getsockopt(sk, level, optname, optval, optlen,
1405 MSG_CMSG_COMPAT);
David L Stevens42908c62008-04-29 03:23:22 -07001406
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001407#ifdef CONFIG_NETFILTER
1408 /* we need to exclude all possible ENOPROTOOPTs except default case */
Pavel Emelyanov6a9fb942007-11-05 21:32:31 -08001409 if (err == -ENOPROTOOPT && optname != IP_PKTOPTIONS &&
1410 !ip_mroute_opt(optname)) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001411 int len;
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001412
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001413 if (get_user(len, optlen))
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001414 return -EFAULT;
1415
1416 lock_sock(sk);
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001417 err = compat_nf_getsockopt(sk, PF_INET, optname, optval, &len);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001418 release_sock(sk);
1419 if (err >= 0)
1420 err = put_user(len, optlen);
1421 return err;
1422 }
1423#endif
1424 return err;
1425}
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001426EXPORT_SYMBOL(compat_ip_getsockopt);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001427#endif