blob: 580dd96666e09b9c88a7e5f9b57f613ce7027a2d [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
Denis V. Lunev7a6adb92008-03-24 15:30:27 -0700189int ip_cmsg_send(struct net *net, struct msghdr *msg, struct ipcm_cookie *ipc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190{
Francesco Fuscof02db312013-09-24 15:43:08 +0200191 int err, val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 struct cmsghdr *cmsg;
193
194 for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) {
195 if (!CMSG_OK(msg, cmsg))
196 return -EINVAL;
197 if (cmsg->cmsg_level != SOL_IP)
198 continue;
199 switch (cmsg->cmsg_type) {
200 case IP_RETOPTS:
201 err = cmsg->cmsg_len - CMSG_ALIGN(sizeof(struct cmsghdr));
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700202 err = ip_options_get(net, &ipc->opt, CMSG_DATA(cmsg),
203 err < 40 ? err : 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 if (err)
205 return err;
206 break;
207 case IP_PKTINFO:
208 {
209 struct in_pktinfo *info;
210 if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct in_pktinfo)))
211 return -EINVAL;
212 info = (struct in_pktinfo *)CMSG_DATA(cmsg);
213 ipc->oif = info->ipi_ifindex;
214 ipc->addr = info->ipi_spec_dst.s_addr;
215 break;
216 }
Francesco Fuscof02db312013-09-24 15:43:08 +0200217 case IP_TTL:
218 if (cmsg->cmsg_len != CMSG_LEN(sizeof(int)))
219 return -EINVAL;
220 val = *(int *)CMSG_DATA(cmsg);
221 if (val < 1 || val > 255)
222 return -EINVAL;
223 ipc->ttl = val;
224 break;
225 case IP_TOS:
226 if (cmsg->cmsg_len != CMSG_LEN(sizeof(int)))
227 return -EINVAL;
228 val = *(int *)CMSG_DATA(cmsg);
229 if (val < 0 || val > 255)
230 return -EINVAL;
231 ipc->tos = val;
232 ipc->priority = rt_tos2priority(ipc->tos);
233 break;
234
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 default:
236 return -EINVAL;
237 }
238 }
239 return 0;
240}
241
242
243/* Special input handler for packets caught by router alert option.
244 They are selected only by protocol field, and then processed likely
245 local ones; but only if someone wants them! Otherwise, router
246 not running rsvpd will kill RSVP.
247
248 It is user level problem, what it will make with them.
249 I have no idea, how it will masquearde or NAT them (it is joke, joke :-)),
250 but receiver should be enough clever f.e. to forward mtrace requests,
251 sent to multicast group to reach destination designated router.
252 */
Eric Dumazet43a951e2010-10-25 03:32:44 +0000253struct ip_ra_chain __rcu *ip_ra_chain;
Eric Dumazet66018502010-06-07 03:12:08 +0000254static DEFINE_SPINLOCK(ip_ra_lock);
255
Eric Dumazet592fcb92010-06-09 16:21:07 +0000256
257static void ip_ra_destroy_rcu(struct rcu_head *head)
Eric Dumazet66018502010-06-07 03:12:08 +0000258{
Eric Dumazet592fcb92010-06-09 16:21:07 +0000259 struct ip_ra_chain *ra = container_of(head, struct ip_ra_chain, rcu);
260
261 sock_put(ra->saved_sk);
262 kfree(ra);
Eric Dumazet66018502010-06-07 03:12:08 +0000263}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700265int ip_ra_control(struct sock *sk, unsigned char on,
266 void (*destructor)(struct sock *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267{
Eric Dumazet43a951e2010-10-25 03:32:44 +0000268 struct ip_ra_chain *ra, *new_ra;
269 struct ip_ra_chain __rcu **rap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000271 if (sk->sk_type != SOCK_RAW || inet_sk(sk)->inet_num == IPPROTO_RAW)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 return -EINVAL;
273
274 new_ra = on ? kmalloc(sizeof(*new_ra), GFP_KERNEL) : NULL;
275
Eric Dumazet66018502010-06-07 03:12:08 +0000276 spin_lock_bh(&ip_ra_lock);
Eric Dumazet43a951e2010-10-25 03:32:44 +0000277 for (rap = &ip_ra_chain;
278 (ra = rcu_dereference_protected(*rap,
279 lockdep_is_held(&ip_ra_lock))) != NULL;
280 rap = &ra->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 if (ra->sk == sk) {
282 if (on) {
Eric Dumazet66018502010-06-07 03:12:08 +0000283 spin_unlock_bh(&ip_ra_lock);
Jesper Juhla51482b2005-11-08 09:41:34 -0800284 kfree(new_ra);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 return -EADDRINUSE;
286 }
Eric Dumazet592fcb92010-06-09 16:21:07 +0000287 /* dont let ip_call_ra_chain() use sk again */
288 ra->sk = NULL;
Eric Dumazet66018502010-06-07 03:12:08 +0000289 rcu_assign_pointer(*rap, ra->next);
290 spin_unlock_bh(&ip_ra_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
292 if (ra->destructor)
293 ra->destructor(sk);
Eric Dumazet592fcb92010-06-09 16:21:07 +0000294 /*
295 * Delay sock_put(sk) and kfree(ra) after one rcu grace
296 * period. This guarantee ip_call_ra_chain() dont need
297 * to mess with socket refcounts.
298 */
299 ra->saved_sk = sk;
300 call_rcu(&ra->rcu, ip_ra_destroy_rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 return 0;
302 }
303 }
304 if (new_ra == NULL) {
Eric Dumazet66018502010-06-07 03:12:08 +0000305 spin_unlock_bh(&ip_ra_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 return -ENOBUFS;
307 }
308 new_ra->sk = sk;
309 new_ra->destructor = destructor;
310
311 new_ra->next = ra;
Eric Dumazet66018502010-06-07 03:12:08 +0000312 rcu_assign_pointer(*rap, new_ra);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 sock_hold(sk);
Eric Dumazet66018502010-06-07 03:12:08 +0000314 spin_unlock_bh(&ip_ra_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
316 return 0;
317}
318
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900319void ip_icmp_error(struct sock *sk, struct sk_buff *skb, int err,
Al Viro35986b32006-09-27 18:34:21 -0700320 __be16 port, u32 info, u8 *payload)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 struct sock_exterr_skb *serr;
323
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 skb = skb_clone(skb, GFP_ATOMIC);
325 if (!skb)
326 return;
327
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900328 serr = SKB_EXT_ERR(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 serr->ee.ee_errno = err;
330 serr->ee.ee_origin = SO_EE_ORIGIN_ICMP;
Arnaldo Carvalho de Melo88c76642007-03-13 14:43:18 -0300331 serr->ee.ee_type = icmp_hdr(skb)->type;
332 serr->ee.ee_code = icmp_hdr(skb)->code;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 serr->ee.ee_pad = 0;
334 serr->ee.ee_info = info;
335 serr->ee.ee_data = 0;
Arnaldo Carvalho de Melo88c76642007-03-13 14:43:18 -0300336 serr->addr_offset = (u8 *)&(((struct iphdr *)(icmp_hdr(skb) + 1))->daddr) -
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700337 skb_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 serr->port = port;
339
Arnaldo Carvalho de Melobd823932007-03-13 17:10:43 -0300340 if (skb_pull(skb, payload - skb->data) != NULL) {
341 skb_reset_transport_header(skb);
342 if (sock_queue_err_skb(sk, skb) == 0)
343 return;
344 }
345 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346}
347
Al Viro05790162006-09-27 18:33:40 -0700348void ip_local_error(struct sock *sk, int err, __be32 daddr, __be16 port, u32 info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349{
350 struct inet_sock *inet = inet_sk(sk);
351 struct sock_exterr_skb *serr;
352 struct iphdr *iph;
353 struct sk_buff *skb;
354
355 if (!inet->recverr)
356 return;
357
358 skb = alloc_skb(sizeof(struct iphdr), GFP_ATOMIC);
359 if (!skb)
360 return;
361
Arnaldo Carvalho de Melo2ca9e6f2007-03-10 19:15:25 -0300362 skb_put(skb, sizeof(struct iphdr));
363 skb_reset_network_header(skb);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700364 iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 iph->daddr = daddr;
366
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900367 serr = SKB_EXT_ERR(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 serr->ee.ee_errno = err;
369 serr->ee.ee_origin = SO_EE_ORIGIN_LOCAL;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900370 serr->ee.ee_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 serr->ee.ee_code = 0;
372 serr->ee.ee_pad = 0;
373 serr->ee.ee_info = info;
374 serr->ee.ee_data = 0;
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700375 serr->addr_offset = (u8 *)&iph->daddr - skb_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 serr->port = port;
377
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700378 __skb_pull(skb, skb_tail_pointer(skb) - skb->data);
Arnaldo Carvalho de Melobd823932007-03-13 17:10:43 -0300379 skb_reset_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
381 if (sock_queue_err_skb(sk, skb))
382 kfree_skb(skb);
383}
384
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900385/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 * Handle MSG_ERRQUEUE
387 */
Hannes Frederic Sowa85fbaa72013-11-23 00:46:12 +0100388int ip_recv_error(struct sock *sk, struct msghdr *msg, int len, int *addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389{
390 struct sock_exterr_skb *serr;
391 struct sk_buff *skb, *skb2;
Steffen Hurrle342dfc32014-01-17 22:53:15 +0100392 DECLARE_SOCKADDR(struct sockaddr_in *, sin, msg->msg_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 struct {
394 struct sock_extended_err ee;
395 struct sockaddr_in offender;
396 } errhdr;
397 int err;
398 int copied;
399
400 err = -EAGAIN;
401 skb = skb_dequeue(&sk->sk_error_queue);
402 if (skb == NULL)
403 goto out;
404
405 copied = skb->len;
406 if (copied > len) {
407 msg->msg_flags |= MSG_TRUNC;
408 copied = len;
409 }
410 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
411 if (err)
412 goto out_free_skb;
413
414 sock_recv_timestamp(msg, sk, skb);
415
416 serr = SKB_EXT_ERR(skb);
417
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 if (sin) {
419 sin->sin_family = AF_INET;
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700420 sin->sin_addr.s_addr = *(__be32 *)(skb_network_header(skb) +
421 serr->addr_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 sin->sin_port = serr->port;
423 memset(&sin->sin_zero, 0, sizeof(sin->sin_zero));
Hannes Frederic Sowa85fbaa72013-11-23 00:46:12 +0100424 *addr_len = sizeof(*sin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 }
426
427 memcpy(&errhdr.ee, &serr->ee, sizeof(struct sock_extended_err));
428 sin = &errhdr.offender;
429 sin->sin_family = AF_UNSPEC;
430 if (serr->ee.ee_origin == SO_EE_ORIGIN_ICMP) {
431 struct inet_sock *inet = inet_sk(sk);
432
433 sin->sin_family = AF_INET;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700434 sin->sin_addr.s_addr = ip_hdr(skb)->saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 sin->sin_port = 0;
436 memset(&sin->sin_zero, 0, sizeof(sin->sin_zero));
437 if (inet->cmsg_flags)
438 ip_cmsg_recv(msg, skb);
439 }
440
441 put_cmsg(msg, SOL_IP, IP_RECVERR, sizeof(errhdr), &errhdr);
442
443 /* Now we could try to dump offended packet options */
444
445 msg->msg_flags |= MSG_ERRQUEUE;
446 err = copied;
447
448 /* Reset and regenerate socket error */
Herbert Xue0f9f852005-06-18 22:56:18 -0700449 spin_lock_bh(&sk->sk_error_queue.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 sk->sk_err = 0;
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700451 skb2 = skb_peek(&sk->sk_error_queue);
452 if (skb2 != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 sk->sk_err = SKB_EXT_ERR(skb2)->ee.ee_errno;
Herbert Xue0f9f852005-06-18 22:56:18 -0700454 spin_unlock_bh(&sk->sk_error_queue.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 sk->sk_error_report(sk);
456 } else
Herbert Xue0f9f852005-06-18 22:56:18 -0700457 spin_unlock_bh(&sk->sk_error_queue.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900459out_free_skb:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 kfree_skb(skb);
461out:
462 return err;
463}
464
465
466/*
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700467 * Socket option code for IP. This is the end of the line after any
468 * TCP,UDP etc options on an IP socket.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 */
470
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800471static int do_ip_setsockopt(struct sock *sk, int level,
David S. Millerb7058842009-09-30 16:12:20 -0700472 int optname, char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473{
474 struct inet_sock *inet = inet_sk(sk);
Jianjun Kong09cb1052008-11-03 00:27:11 -0800475 int val = 0, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
Xi Wang0c9f79b2012-11-11 11:20:01 +0000477 switch (optname) {
478 case IP_PKTINFO:
479 case IP_RECVTTL:
480 case IP_RECVOPTS:
481 case IP_RECVTOS:
482 case IP_RETOPTS:
483 case IP_TOS:
484 case IP_TTL:
485 case IP_HDRINCL:
486 case IP_MTU_DISCOVER:
487 case IP_RECVERR:
488 case IP_ROUTER_ALERT:
489 case IP_FREEBIND:
490 case IP_PASSSEC:
491 case IP_TRANSPARENT:
492 case IP_MINTTL:
493 case IP_NODEFRAG:
494 case IP_UNICAST_IF:
495 case IP_MULTICAST_TTL:
496 case IP_MULTICAST_ALL:
497 case IP_MULTICAST_LOOP:
498 case IP_RECVORIGDSTADDR:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 if (optlen >= sizeof(int)) {
500 if (get_user(val, (int __user *) optval))
501 return -EFAULT;
502 } else if (optlen >= sizeof(char)) {
503 unsigned char ucval;
504
505 if (get_user(ucval, (unsigned char __user *) optval))
506 return -EFAULT;
507 val = (int) ucval;
508 }
509 }
510
511 /* If optlen==0, it is equivalent to val == 0 */
512
Pavel Emelyanov6a9fb942007-11-05 21:32:31 -0800513 if (ip_mroute_opt(optname))
Jianjun Kong09cb1052008-11-03 00:27:11 -0800514 return ip_mroute_setsockopt(sk, optname, optval, optlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
516 err = 0;
517 lock_sock(sk);
518
519 switch (optname) {
Stephen Hemminger132adf52007-03-08 20:44:43 -0800520 case IP_OPTIONS:
521 {
Eric Dumazetf6d8bd02011-04-21 09:45:37 +0000522 struct ip_options_rcu *old, *opt = NULL;
523
roel kluin65a1c4f2009-10-23 05:59:21 +0000524 if (optlen > 40)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800525 goto e_inval;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900526 err = ip_options_get_from_user(sock_net(sk), &opt,
Denis V. Lunevcb846632008-03-24 15:31:00 -0700527 optval, optlen);
Stephen Hemminger132adf52007-03-08 20:44:43 -0800528 if (err)
529 break;
Eric Dumazetf6d8bd02011-04-21 09:45:37 +0000530 old = rcu_dereference_protected(inet->inet_opt,
531 sock_owned_by_user(sk));
Stephen Hemminger132adf52007-03-08 20:44:43 -0800532 if (inet->is_icsk) {
533 struct inet_connection_sock *icsk = inet_csk(sk);
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000534#if IS_ENABLED(CONFIG_IPV6)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800535 if (sk->sk_family == PF_INET ||
536 (!((1 << sk->sk_state) &
537 (TCPF_LISTEN | TCPF_CLOSE)) &&
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000538 inet->inet_daddr != LOOPBACK4_IPV6)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539#endif
Eric Dumazetf6d8bd02011-04-21 09:45:37 +0000540 if (old)
541 icsk->icsk_ext_hdr_len -= old->opt.optlen;
Stephen Hemminger132adf52007-03-08 20:44:43 -0800542 if (opt)
Eric Dumazetf6d8bd02011-04-21 09:45:37 +0000543 icsk->icsk_ext_hdr_len += opt->opt.optlen;
Stephen Hemminger132adf52007-03-08 20:44:43 -0800544 icsk->icsk_sync_mss(sk, icsk->icsk_pmtu_cookie);
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000545#if IS_ENABLED(CONFIG_IPV6)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800546 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 }
Eric Dumazetf6d8bd02011-04-21 09:45:37 +0000549 rcu_assign_pointer(inet->inet_opt, opt);
550 if (old)
Paul E. McKenney605b4af2012-01-06 17:08:33 -0800551 kfree_rcu(old, rcu);
Stephen Hemminger132adf52007-03-08 20:44:43 -0800552 break;
553 }
554 case IP_PKTINFO:
555 if (val)
556 inet->cmsg_flags |= IP_CMSG_PKTINFO;
557 else
558 inet->cmsg_flags &= ~IP_CMSG_PKTINFO;
559 break;
560 case IP_RECVTTL:
561 if (val)
562 inet->cmsg_flags |= IP_CMSG_TTL;
563 else
564 inet->cmsg_flags &= ~IP_CMSG_TTL;
565 break;
566 case IP_RECVTOS:
567 if (val)
568 inet->cmsg_flags |= IP_CMSG_TOS;
569 else
570 inet->cmsg_flags &= ~IP_CMSG_TOS;
571 break;
572 case IP_RECVOPTS:
573 if (val)
574 inet->cmsg_flags |= IP_CMSG_RECVOPTS;
575 else
576 inet->cmsg_flags &= ~IP_CMSG_RECVOPTS;
577 break;
578 case IP_RETOPTS:
579 if (val)
580 inet->cmsg_flags |= IP_CMSG_RETOPTS;
581 else
582 inet->cmsg_flags &= ~IP_CMSG_RETOPTS;
583 break;
584 case IP_PASSSEC:
585 if (val)
586 inet->cmsg_flags |= IP_CMSG_PASSSEC;
587 else
588 inet->cmsg_flags &= ~IP_CMSG_PASSSEC;
589 break;
Balazs Scheidlere8b2dfe2008-11-16 19:32:39 -0800590 case IP_RECVORIGDSTADDR:
591 if (val)
592 inet->cmsg_flags |= IP_CMSG_ORIGDSTADDR;
593 else
594 inet->cmsg_flags &= ~IP_CMSG_ORIGDSTADDR;
595 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -0800596 case IP_TOS: /* This sets both TOS and Precedence */
597 if (sk->sk_type == SOCK_STREAM) {
Maciej Żenczykowski2c67e9a2011-10-22 00:07:47 -0400598 val &= ~INET_ECN_MASK;
599 val |= inet->tos & INET_ECN_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 }
Stephen Hemminger132adf52007-03-08 20:44:43 -0800601 if (inet->tos != val) {
602 inet->tos = val;
603 sk->sk_priority = rt_tos2priority(val);
604 sk_dst_reset(sk);
605 }
606 break;
607 case IP_TTL:
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700608 if (optlen < 1)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800609 goto e_inval;
Cong Wangc9be4a52013-01-07 21:17:00 +0000610 if (val != -1 && (val < 1 || val > 255))
Stephen Hemminger132adf52007-03-08 20:44:43 -0800611 goto e_inval;
612 inet->uc_ttl = val;
613 break;
614 case IP_HDRINCL:
615 if (sk->sk_type != SOCK_RAW) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 err = -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -0800618 }
619 inet->hdrincl = val ? 1 : 0;
620 break;
Jiri Olsa7b2ff182010-06-15 01:07:31 +0000621 case IP_NODEFRAG:
622 if (sk->sk_type != SOCK_RAW) {
623 err = -ENOPROTOOPT;
624 break;
625 }
626 inet->nodefrag = val ? 1 : 0;
627 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -0800628 case IP_MTU_DISCOVER:
Hannes Frederic Sowa482fc602013-11-05 02:24:17 +0100629 if (val < IP_PMTUDISC_DONT || val > IP_PMTUDISC_INTERFACE)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800630 goto e_inval;
631 inet->pmtudisc = val;
632 break;
633 case IP_RECVERR:
634 inet->recverr = !!val;
635 if (!val)
636 skb_queue_purge(&sk->sk_error_queue);
637 break;
638 case IP_MULTICAST_TTL:
639 if (sk->sk_type == SOCK_STREAM)
640 goto e_inval;
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700641 if (optlen < 1)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800642 goto e_inval;
Jianjun Kong09cb1052008-11-03 00:27:11 -0800643 if (val == -1)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800644 val = 1;
645 if (val < 0 || val > 255)
646 goto e_inval;
647 inet->mc_ttl = val;
648 break;
649 case IP_MULTICAST_LOOP:
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700650 if (optlen < 1)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800651 goto e_inval;
652 inet->mc_loop = !!val;
653 break;
Erich E. Hoover76e21052012-02-08 09:11:07 +0000654 case IP_UNICAST_IF:
655 {
656 struct net_device *dev = NULL;
657 int ifindex;
658
659 if (optlen != sizeof(int))
660 goto e_inval;
661
662 ifindex = (__force int)ntohl((__force __be32)val);
663 if (ifindex == 0) {
664 inet->uc_index = 0;
665 err = 0;
666 break;
667 }
668
669 dev = dev_get_by_index(sock_net(sk), ifindex);
670 err = -EADDRNOTAVAIL;
671 if (!dev)
672 break;
673 dev_put(dev);
674
675 err = -EINVAL;
676 if (sk->sk_bound_dev_if)
677 break;
678
679 inet->uc_index = ifindex;
680 err = 0;
681 break;
682 }
Stephen Hemminger132adf52007-03-08 20:44:43 -0800683 case IP_MULTICAST_IF:
684 {
685 struct ip_mreqn mreq;
686 struct net_device *dev = NULL;
687
688 if (sk->sk_type == SOCK_STREAM)
689 goto e_inval;
690 /*
691 * Check the arguments are allowable
692 */
693
Shan Wei09159212009-09-22 15:41:10 +0000694 if (optlen < sizeof(struct in_addr))
695 goto e_inval;
696
Stephen Hemminger132adf52007-03-08 20:44:43 -0800697 err = -EFAULT;
698 if (optlen >= sizeof(struct ip_mreqn)) {
Jianjun Kong09cb1052008-11-03 00:27:11 -0800699 if (copy_from_user(&mreq, optval, sizeof(mreq)))
Stephen Hemminger132adf52007-03-08 20:44:43 -0800700 break;
701 } else {
702 memset(&mreq, 0, sizeof(mreq));
Jiri Pirko3a084dd2012-05-03 22:37:45 +0000703 if (optlen >= sizeof(struct ip_mreq)) {
704 if (copy_from_user(&mreq, optval,
705 sizeof(struct ip_mreq)))
706 break;
707 } else if (optlen >= sizeof(struct in_addr)) {
708 if (copy_from_user(&mreq.imr_address, optval,
709 sizeof(struct in_addr)))
710 break;
711 }
Stephen Hemminger132adf52007-03-08 20:44:43 -0800712 }
713
714 if (!mreq.imr_ifindex) {
Al Viroe6f1ceb2008-03-17 22:44:53 -0700715 if (mreq.imr_address.s_addr == htonl(INADDR_ANY)) {
Stephen Hemminger132adf52007-03-08 20:44:43 -0800716 inet->mc_index = 0;
717 inet->mc_addr = 0;
718 err = 0;
719 break;
720 }
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900721 dev = ip_dev_find(sock_net(sk), mreq.imr_address.s_addr);
Eric Dumazet55b80502009-10-19 06:41:58 +0000722 if (dev)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800723 mreq.imr_ifindex = dev->ifindex;
Stephen Hemminger132adf52007-03-08 20:44:43 -0800724 } else
Eric Dumazet55b80502009-10-19 06:41:58 +0000725 dev = dev_get_by_index(sock_net(sk), mreq.imr_ifindex);
Stephen Hemminger132adf52007-03-08 20:44:43 -0800726
727
728 err = -EADDRNOTAVAIL;
729 if (!dev)
730 break;
Eric Dumazet55b80502009-10-19 06:41:58 +0000731 dev_put(dev);
Stephen Hemminger132adf52007-03-08 20:44:43 -0800732
733 err = -EINVAL;
734 if (sk->sk_bound_dev_if &&
735 mreq.imr_ifindex != sk->sk_bound_dev_if)
736 break;
737
738 inet->mc_index = mreq.imr_ifindex;
739 inet->mc_addr = mreq.imr_address.s_addr;
740 err = 0;
741 break;
742 }
743
744 case IP_ADD_MEMBERSHIP:
745 case IP_DROP_MEMBERSHIP:
746 {
747 struct ip_mreqn mreq;
748
Flavio Leitnera96fb492007-08-24 22:16:39 -0700749 err = -EPROTO;
750 if (inet_sk(sk)->is_icsk)
751 break;
752
Stephen Hemminger132adf52007-03-08 20:44:43 -0800753 if (optlen < sizeof(struct ip_mreq))
754 goto e_inval;
755 err = -EFAULT;
756 if (optlen >= sizeof(struct ip_mreqn)) {
Jianjun Kong09cb1052008-11-03 00:27:11 -0800757 if (copy_from_user(&mreq, optval, sizeof(mreq)))
Stephen Hemminger132adf52007-03-08 20:44:43 -0800758 break;
759 } else {
760 memset(&mreq, 0, sizeof(mreq));
Jianjun Kong09cb1052008-11-03 00:27:11 -0800761 if (copy_from_user(&mreq, optval, sizeof(struct ip_mreq)))
Stephen Hemminger132adf52007-03-08 20:44:43 -0800762 break;
763 }
764
765 if (optname == IP_ADD_MEMBERSHIP)
766 err = ip_mc_join_group(sk, &mreq);
767 else
768 err = ip_mc_leave_group(sk, &mreq);
769 break;
770 }
771 case IP_MSFILTER:
772 {
Stephen Hemminger132adf52007-03-08 20:44:43 -0800773 struct ip_msfilter *msf;
774
775 if (optlen < IP_MSFILTER_SIZE(0))
776 goto e_inval;
777 if (optlen > sysctl_optmem_max) {
778 err = -ENOBUFS;
779 break;
780 }
781 msf = kmalloc(optlen, GFP_KERNEL);
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -0700782 if (!msf) {
Stephen Hemminger132adf52007-03-08 20:44:43 -0800783 err = -ENOBUFS;
784 break;
785 }
786 err = -EFAULT;
787 if (copy_from_user(msf, optval, optlen)) {
788 kfree(msf);
789 break;
790 }
791 /* numsrc >= (1G-4) overflow in 32 bits */
792 if (msf->imsf_numsrc >= 0x3ffffffcU ||
793 msf->imsf_numsrc > sysctl_igmp_max_msf) {
794 kfree(msf);
795 err = -ENOBUFS;
796 break;
797 }
798 if (IP_MSFILTER_SIZE(msf->imsf_numsrc) > optlen) {
799 kfree(msf);
800 err = -EINVAL;
801 break;
802 }
803 err = ip_mc_msfilter(sk, msf, 0);
804 kfree(msf);
805 break;
806 }
807 case IP_BLOCK_SOURCE:
808 case IP_UNBLOCK_SOURCE:
809 case IP_ADD_SOURCE_MEMBERSHIP:
810 case IP_DROP_SOURCE_MEMBERSHIP:
811 {
812 struct ip_mreq_source mreqs;
813 int omode, add;
814
815 if (optlen != sizeof(struct ip_mreq_source))
816 goto e_inval;
817 if (copy_from_user(&mreqs, optval, sizeof(mreqs))) {
818 err = -EFAULT;
819 break;
820 }
821 if (optname == IP_BLOCK_SOURCE) {
822 omode = MCAST_EXCLUDE;
823 add = 1;
824 } else if (optname == IP_UNBLOCK_SOURCE) {
825 omode = MCAST_EXCLUDE;
826 add = 0;
827 } else if (optname == IP_ADD_SOURCE_MEMBERSHIP) {
828 struct ip_mreqn mreq;
829
830 mreq.imr_multiaddr.s_addr = mreqs.imr_multiaddr;
831 mreq.imr_address.s_addr = mreqs.imr_interface;
832 mreq.imr_ifindex = 0;
833 err = ip_mc_join_group(sk, &mreq);
834 if (err && err != -EADDRINUSE)
835 break;
836 omode = MCAST_INCLUDE;
837 add = 1;
838 } else /* IP_DROP_SOURCE_MEMBERSHIP */ {
839 omode = MCAST_INCLUDE;
840 add = 0;
841 }
842 err = ip_mc_source(add, omode, sk, &mreqs, 0);
843 break;
844 }
845 case MCAST_JOIN_GROUP:
846 case MCAST_LEAVE_GROUP:
847 {
848 struct group_req greq;
849 struct sockaddr_in *psin;
850 struct ip_mreqn mreq;
851
852 if (optlen < sizeof(struct group_req))
853 goto e_inval;
854 err = -EFAULT;
855 if (copy_from_user(&greq, optval, sizeof(greq)))
856 break;
857 psin = (struct sockaddr_in *)&greq.gr_group;
858 if (psin->sin_family != AF_INET)
859 goto e_inval;
860 memset(&mreq, 0, sizeof(mreq));
861 mreq.imr_multiaddr = psin->sin_addr;
862 mreq.imr_ifindex = greq.gr_interface;
863
864 if (optname == MCAST_JOIN_GROUP)
865 err = ip_mc_join_group(sk, &mreq);
866 else
867 err = ip_mc_leave_group(sk, &mreq);
868 break;
869 }
870 case MCAST_JOIN_SOURCE_GROUP:
871 case MCAST_LEAVE_SOURCE_GROUP:
872 case MCAST_BLOCK_SOURCE:
873 case MCAST_UNBLOCK_SOURCE:
874 {
875 struct group_source_req greqs;
876 struct ip_mreq_source mreqs;
877 struct sockaddr_in *psin;
878 int omode, add;
879
880 if (optlen != sizeof(struct group_source_req))
881 goto e_inval;
882 if (copy_from_user(&greqs, optval, sizeof(greqs))) {
883 err = -EFAULT;
884 break;
885 }
886 if (greqs.gsr_group.ss_family != AF_INET ||
887 greqs.gsr_source.ss_family != AF_INET) {
888 err = -EADDRNOTAVAIL;
889 break;
890 }
891 psin = (struct sockaddr_in *)&greqs.gsr_group;
892 mreqs.imr_multiaddr = psin->sin_addr.s_addr;
893 psin = (struct sockaddr_in *)&greqs.gsr_source;
894 mreqs.imr_sourceaddr = psin->sin_addr.s_addr;
895 mreqs.imr_interface = 0; /* use index for mc_source */
896
897 if (optname == MCAST_BLOCK_SOURCE) {
898 omode = MCAST_EXCLUDE;
899 add = 1;
900 } else if (optname == MCAST_UNBLOCK_SOURCE) {
901 omode = MCAST_EXCLUDE;
902 add = 0;
903 } else if (optname == MCAST_JOIN_SOURCE_GROUP) {
904 struct ip_mreqn mreq;
905
906 psin = (struct sockaddr_in *)&greqs.gsr_group;
907 mreq.imr_multiaddr = psin->sin_addr;
908 mreq.imr_address.s_addr = 0;
909 mreq.imr_ifindex = greqs.gsr_interface;
910 err = ip_mc_join_group(sk, &mreq);
911 if (err && err != -EADDRINUSE)
912 break;
913 greqs.gsr_interface = mreq.imr_ifindex;
914 omode = MCAST_INCLUDE;
915 add = 1;
916 } else /* MCAST_LEAVE_SOURCE_GROUP */ {
917 omode = MCAST_INCLUDE;
918 add = 0;
919 }
920 err = ip_mc_source(add, omode, sk, &mreqs,
921 greqs.gsr_interface);
922 break;
923 }
924 case MCAST_MSFILTER:
925 {
Stephen Hemminger132adf52007-03-08 20:44:43 -0800926 struct sockaddr_in *psin;
927 struct ip_msfilter *msf = NULL;
928 struct group_filter *gsf = NULL;
929 int msize, i, ifindex;
930
931 if (optlen < GROUP_FILTER_SIZE(0))
932 goto e_inval;
933 if (optlen > sysctl_optmem_max) {
934 err = -ENOBUFS;
935 break;
936 }
Jianjun Kong09cb1052008-11-03 00:27:11 -0800937 gsf = kmalloc(optlen, GFP_KERNEL);
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -0700938 if (!gsf) {
Stephen Hemminger132adf52007-03-08 20:44:43 -0800939 err = -ENOBUFS;
940 break;
941 }
942 err = -EFAULT;
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700943 if (copy_from_user(gsf, optval, optlen))
Stephen Hemminger132adf52007-03-08 20:44:43 -0800944 goto mc_msf_out;
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700945
Stephen Hemminger132adf52007-03-08 20:44:43 -0800946 /* numsrc >= (4G-140)/128 overflow in 32 bits */
947 if (gsf->gf_numsrc >= 0x1ffffff ||
948 gsf->gf_numsrc > sysctl_igmp_max_msf) {
949 err = -ENOBUFS;
950 goto mc_msf_out;
951 }
952 if (GROUP_FILTER_SIZE(gsf->gf_numsrc) > optlen) {
953 err = -EINVAL;
954 goto mc_msf_out;
955 }
956 msize = IP_MSFILTER_SIZE(gsf->gf_numsrc);
Jianjun Kong09cb1052008-11-03 00:27:11 -0800957 msf = kmalloc(msize, GFP_KERNEL);
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -0700958 if (!msf) {
Stephen Hemminger132adf52007-03-08 20:44:43 -0800959 err = -ENOBUFS;
960 goto mc_msf_out;
961 }
962 ifindex = gsf->gf_interface;
963 psin = (struct sockaddr_in *)&gsf->gf_group;
964 if (psin->sin_family != AF_INET) {
965 err = -EADDRNOTAVAIL;
966 goto mc_msf_out;
967 }
968 msf->imsf_multiaddr = psin->sin_addr.s_addr;
969 msf->imsf_interface = 0;
970 msf->imsf_fmode = gsf->gf_fmode;
971 msf->imsf_numsrc = gsf->gf_numsrc;
972 err = -EADDRNOTAVAIL;
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700973 for (i = 0; i < gsf->gf_numsrc; ++i) {
Stephen Hemminger132adf52007-03-08 20:44:43 -0800974 psin = (struct sockaddr_in *)&gsf->gf_slist[i];
975
976 if (psin->sin_family != AF_INET)
977 goto mc_msf_out;
978 msf->imsf_slist[i] = psin->sin_addr.s_addr;
979 }
980 kfree(gsf);
981 gsf = NULL;
982
983 err = ip_mc_msfilter(sk, msf, ifindex);
Eric Dumazet4d52cfb2009-06-02 00:42:16 -0700984mc_msf_out:
Stephen Hemminger132adf52007-03-08 20:44:43 -0800985 kfree(msf);
986 kfree(gsf);
987 break;
988 }
Nivedita Singhvif771bef2009-05-28 07:00:46 +0000989 case IP_MULTICAST_ALL:
990 if (optlen < 1)
991 goto e_inval;
992 if (val != 0 && val != 1)
993 goto e_inval;
994 inet->mc_all = val;
995 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -0800996 case IP_ROUTER_ALERT:
997 err = ip_ra_control(sk, val ? 1 : 0, NULL);
998 break;
999
1000 case IP_FREEBIND:
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001001 if (optlen < 1)
Stephen Hemminger132adf52007-03-08 20:44:43 -08001002 goto e_inval;
1003 inet->freebind = !!val;
1004 break;
1005
1006 case IP_IPSEC_POLICY:
1007 case IP_XFRM_POLICY:
1008 err = -EPERM;
Eric W. Biederman52e804c2012-11-16 03:03:05 +00001009 if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001010 break;
1011 err = xfrm_user_policy(sk, optname, optval, optlen);
1012 break;
1013
KOVACS Krisztianf5715ae2008-10-01 07:30:02 -07001014 case IP_TRANSPARENT:
Eric W. Biederman52e804c2012-11-16 03:03:05 +00001015 if (!!val && !ns_capable(sock_net(sk)->user_ns, CAP_NET_RAW) &&
1016 !ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN)) {
KOVACS Krisztianf5715ae2008-10-01 07:30:02 -07001017 err = -EPERM;
1018 break;
1019 }
1020 if (optlen < 1)
1021 goto e_inval;
1022 inet->transparent = !!val;
1023 break;
1024
Stephen Hemmingerd218d112010-01-11 16:28:01 -08001025 case IP_MINTTL:
1026 if (optlen < 1)
1027 goto e_inval;
1028 if (val < 0 || val > 255)
1029 goto e_inval;
1030 inet->min_ttl = val;
1031 break;
1032
Stephen Hemminger132adf52007-03-08 20:44:43 -08001033 default:
1034 err = -ENOPROTOOPT;
1035 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 }
1037 release_sock(sk);
1038 return err;
1039
1040e_inval:
1041 release_sock(sk);
1042 return -EINVAL;
1043}
1044
Eric Dumazetf84af322010-04-28 15:31:51 -07001045/**
Eric Dumazetd826eb12011-11-09 07:24:35 +00001046 * ipv4_pktinfo_prepare - transfert some info from rtable to skb
Eric Dumazetf84af322010-04-28 15:31:51 -07001047 * @sk: socket
1048 * @skb: buffer
1049 *
David S. Miller35ebf652012-06-28 03:59:11 -07001050 * To support IP_CMSG_PKTINFO option, we store rt_iif and specific
1051 * destination in skb->cb[] before dst drop.
stephen hemminger8e3bff92013-12-08 12:15:44 -08001052 * This way, receiver doesn't make cache line misses to read rtable.
Eric Dumazetf84af322010-04-28 15:31:51 -07001053 */
Shawn Bohrerfbf88662013-10-07 11:01:40 -05001054void ipv4_pktinfo_prepare(const struct sock *sk, struct sk_buff *skb)
Eric Dumazetf84af322010-04-28 15:31:51 -07001055{
Eric Dumazetd826eb12011-11-09 07:24:35 +00001056 struct in_pktinfo *pktinfo = PKTINFO_SKB_CB(skb);
Hannes Frederic Sowa4b261c72014-01-20 03:43:08 +01001057 bool prepare = (inet_sk(sk)->cmsg_flags & IP_CMSG_PKTINFO) ||
1058 ipv6_sk_rxinfo(sk);
Eric Dumazetd826eb12011-11-09 07:24:35 +00001059
Hannes Frederic Sowa4b261c72014-01-20 03:43:08 +01001060 if (prepare && skb_rtable(skb)) {
David S. Miller92101b32012-07-23 16:29:00 -07001061 pktinfo->ipi_ifindex = inet_iif(skb);
David S. Miller35ebf652012-06-28 03:59:11 -07001062 pktinfo->ipi_spec_dst.s_addr = fib_compute_spec_dst(skb);
Eric Dumazetd826eb12011-11-09 07:24:35 +00001063 } else {
1064 pktinfo->ipi_ifindex = 0;
1065 pktinfo->ipi_spec_dst.s_addr = 0;
1066 }
1067 skb_dst_drop(skb);
Eric Dumazetf84af322010-04-28 15:31:51 -07001068}
Eric Dumazetf84af322010-04-28 15:31:51 -07001069
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001070int ip_setsockopt(struct sock *sk, int level,
David S. Millerb7058842009-09-30 16:12:20 -07001071 int optname, char __user *optval, unsigned int optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001072{
1073 int err;
1074
1075 if (level != SOL_IP)
1076 return -ENOPROTOOPT;
1077
1078 err = do_ip_setsockopt(sk, level, optname, optval, optlen);
1079#ifdef CONFIG_NETFILTER
1080 /* we need to exclude all possible ENOPROTOOPTs except default case */
1081 if (err == -ENOPROTOOPT && optname != IP_HDRINCL &&
Pavel Emelyanov6a9fb942007-11-05 21:32:31 -08001082 optname != IP_IPSEC_POLICY &&
1083 optname != IP_XFRM_POLICY &&
1084 !ip_mroute_opt(optname)) {
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001085 lock_sock(sk);
1086 err = nf_setsockopt(sk, PF_INET, optname, optval, optlen);
1087 release_sock(sk);
1088 }
1089#endif
1090 return err;
1091}
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001092EXPORT_SYMBOL(ip_setsockopt);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001093
1094#ifdef CONFIG_COMPAT
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001095int compat_ip_setsockopt(struct sock *sk, int level, int optname,
David S. Millerb7058842009-09-30 16:12:20 -07001096 char __user *optval, unsigned int optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001097{
1098 int err;
1099
1100 if (level != SOL_IP)
1101 return -ENOPROTOOPT;
1102
David L Stevensdae50292008-04-27 01:06:07 -07001103 if (optname >= MCAST_JOIN_GROUP && optname <= MCAST_MSFILTER)
1104 return compat_mc_setsockopt(sk, level, optname, optval, optlen,
1105 ip_setsockopt);
1106
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001107 err = do_ip_setsockopt(sk, level, optname, optval, optlen);
1108#ifdef CONFIG_NETFILTER
1109 /* we need to exclude all possible ENOPROTOOPTs except default case */
1110 if (err == -ENOPROTOOPT && optname != IP_HDRINCL &&
Pavel Emelyanov6a9fb942007-11-05 21:32:31 -08001111 optname != IP_IPSEC_POLICY &&
1112 optname != IP_XFRM_POLICY &&
1113 !ip_mroute_opt(optname)) {
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001114 lock_sock(sk);
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001115 err = compat_nf_setsockopt(sk, PF_INET, optname,
1116 optval, optlen);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001117 release_sock(sk);
1118 }
1119#endif
1120 return err;
1121}
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001122EXPORT_SYMBOL(compat_ip_setsockopt);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001123#endif
1124
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125/*
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001126 * Get the options. Note for future reference. The GET of IP options gets
1127 * the _received_ ones. The set sets the _sent_ ones.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 */
1129
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001130static int do_ip_getsockopt(struct sock *sk, int level, int optname,
Eric Dumazet95c96172012-04-15 05:58:06 +00001131 char __user *optval, int __user *optlen, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132{
1133 struct inet_sock *inet = inet_sk(sk);
1134 int val;
1135 int len;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001136
Stephen Hemminger132adf52007-03-08 20:44:43 -08001137 if (level != SOL_IP)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 return -EOPNOTSUPP;
1139
Pavel Emelyanov6a9fb942007-11-05 21:32:31 -08001140 if (ip_mroute_opt(optname))
Jianjun Kong09cb1052008-11-03 00:27:11 -08001141 return ip_mroute_getsockopt(sk, optname, optval, optlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142
Jianjun Kong09cb1052008-11-03 00:27:11 -08001143 if (get_user(len, optlen))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 return -EFAULT;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001145 if (len < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 return -EINVAL;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001147
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 lock_sock(sk);
1149
Stephen Hemminger132adf52007-03-08 20:44:43 -08001150 switch (optname) {
1151 case IP_OPTIONS:
1152 {
1153 unsigned char optbuf[sizeof(struct ip_options)+40];
Eric Dumazetf6d8bd02011-04-21 09:45:37 +00001154 struct ip_options *opt = (struct ip_options *)optbuf;
1155 struct ip_options_rcu *inet_opt;
1156
1157 inet_opt = rcu_dereference_protected(inet->inet_opt,
1158 sock_owned_by_user(sk));
Stephen Hemminger132adf52007-03-08 20:44:43 -08001159 opt->optlen = 0;
Eric Dumazetf6d8bd02011-04-21 09:45:37 +00001160 if (inet_opt)
1161 memcpy(optbuf, &inet_opt->opt,
1162 sizeof(struct ip_options) +
1163 inet_opt->opt.optlen);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001164 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165
Stephen Hemminger132adf52007-03-08 20:44:43 -08001166 if (opt->optlen == 0)
1167 return put_user(0, optlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168
Stephen Hemminger132adf52007-03-08 20:44:43 -08001169 ip_options_undo(opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170
Stephen Hemminger132adf52007-03-08 20:44:43 -08001171 len = min_t(unsigned int, len, opt->optlen);
1172 if (put_user(len, optlen))
1173 return -EFAULT;
1174 if (copy_to_user(optval, opt->__data, len))
1175 return -EFAULT;
1176 return 0;
1177 }
1178 case IP_PKTINFO:
1179 val = (inet->cmsg_flags & IP_CMSG_PKTINFO) != 0;
1180 break;
1181 case IP_RECVTTL:
1182 val = (inet->cmsg_flags & IP_CMSG_TTL) != 0;
1183 break;
1184 case IP_RECVTOS:
1185 val = (inet->cmsg_flags & IP_CMSG_TOS) != 0;
1186 break;
1187 case IP_RECVOPTS:
1188 val = (inet->cmsg_flags & IP_CMSG_RECVOPTS) != 0;
1189 break;
1190 case IP_RETOPTS:
1191 val = (inet->cmsg_flags & IP_CMSG_RETOPTS) != 0;
1192 break;
1193 case IP_PASSSEC:
1194 val = (inet->cmsg_flags & IP_CMSG_PASSSEC) != 0;
1195 break;
Balazs Scheidlere8b2dfe2008-11-16 19:32:39 -08001196 case IP_RECVORIGDSTADDR:
1197 val = (inet->cmsg_flags & IP_CMSG_ORIGDSTADDR) != 0;
1198 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001199 case IP_TOS:
1200 val = inet->tos;
1201 break;
1202 case IP_TTL:
1203 val = (inet->uc_ttl == -1 ?
1204 sysctl_ip_default_ttl :
1205 inet->uc_ttl);
1206 break;
1207 case IP_HDRINCL:
1208 val = inet->hdrincl;
1209 break;
Michael Kerriska89b4762010-09-10 20:26:56 +00001210 case IP_NODEFRAG:
1211 val = inet->nodefrag;
1212 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001213 case IP_MTU_DISCOVER:
1214 val = inet->pmtudisc;
1215 break;
1216 case IP_MTU:
1217 {
1218 struct dst_entry *dst;
1219 val = 0;
1220 dst = sk_dst_get(sk);
1221 if (dst) {
1222 val = dst_mtu(dst);
1223 dst_release(dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001225 if (!val) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 release_sock(sk);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001227 return -ENOTCONN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001229 break;
1230 }
1231 case IP_RECVERR:
1232 val = inet->recverr;
1233 break;
1234 case IP_MULTICAST_TTL:
1235 val = inet->mc_ttl;
1236 break;
1237 case IP_MULTICAST_LOOP:
1238 val = inet->mc_loop;
1239 break;
Erich E. Hoover76e21052012-02-08 09:11:07 +00001240 case IP_UNICAST_IF:
1241 val = (__force int)htonl((__u32) inet->uc_index);
1242 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001243 case IP_MULTICAST_IF:
1244 {
1245 struct in_addr addr;
1246 len = min_t(unsigned int, len, sizeof(struct in_addr));
1247 addr.s_addr = inet->mc_addr;
1248 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249
Stephen Hemminger132adf52007-03-08 20:44:43 -08001250 if (put_user(len, optlen))
1251 return -EFAULT;
1252 if (copy_to_user(optval, &addr, len))
1253 return -EFAULT;
1254 return 0;
1255 }
1256 case IP_MSFILTER:
1257 {
1258 struct ip_msfilter msf;
1259 int err;
1260
1261 if (len < IP_MSFILTER_SIZE(0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 release_sock(sk);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001263 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001265 if (copy_from_user(&msf, optval, IP_MSFILTER_SIZE(0))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 release_sock(sk);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001267 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001269 err = ip_mc_msfget(sk, &msf,
1270 (struct ip_msfilter __user *)optval, optlen);
1271 release_sock(sk);
1272 return err;
1273 }
1274 case MCAST_MSFILTER:
1275 {
1276 struct group_filter gsf;
1277 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278
Stephen Hemminger132adf52007-03-08 20:44:43 -08001279 if (len < GROUP_FILTER_SIZE(0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 release_sock(sk);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001281 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001283 if (copy_from_user(&gsf, optval, GROUP_FILTER_SIZE(0))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 release_sock(sk);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001285 return -EFAULT;
1286 }
1287 err = ip_mc_gsfget(sk, &gsf,
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001288 (struct group_filter __user *)optval,
1289 optlen);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001290 release_sock(sk);
1291 return err;
1292 }
Nivedita Singhvif771bef2009-05-28 07:00:46 +00001293 case IP_MULTICAST_ALL:
1294 val = inet->mc_all;
1295 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001296 case IP_PKTOPTIONS:
1297 {
1298 struct msghdr msg;
1299
1300 release_sock(sk);
1301
1302 if (sk->sk_type != SOCK_STREAM)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 return -ENOPROTOOPT;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001304
1305 msg.msg_control = optval;
1306 msg.msg_controllen = len;
Daniel Balutadd23198e2011-08-07 22:31:07 -07001307 msg.msg_flags = flags;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001308
1309 if (inet->cmsg_flags & IP_CMSG_PKTINFO) {
1310 struct in_pktinfo info;
1311
Eric Dumazetc720c7e2009-10-15 06:30:45 +00001312 info.ipi_addr.s_addr = inet->inet_rcv_saddr;
1313 info.ipi_spec_dst.s_addr = inet->inet_rcv_saddr;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001314 info.ipi_ifindex = inet->mc_index;
1315 put_cmsg(&msg, SOL_IP, IP_PKTINFO, sizeof(info), &info);
1316 }
1317 if (inet->cmsg_flags & IP_CMSG_TTL) {
1318 int hlim = inet->mc_ttl;
1319 put_cmsg(&msg, SOL_IP, IP_TTL, sizeof(hlim), &hlim);
1320 }
Jiri Benc4c507d22012-02-09 09:35:49 +00001321 if (inet->cmsg_flags & IP_CMSG_TOS) {
1322 int tos = inet->rcv_tos;
1323 put_cmsg(&msg, SOL_IP, IP_TOS, sizeof(tos), &tos);
1324 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001325 len -= msg.msg_controllen;
1326 return put_user(len, optlen);
1327 }
1328 case IP_FREEBIND:
1329 val = inet->freebind;
1330 break;
KOVACS Krisztianf5715ae2008-10-01 07:30:02 -07001331 case IP_TRANSPARENT:
1332 val = inet->transparent;
1333 break;
Stephen Hemmingerd218d112010-01-11 16:28:01 -08001334 case IP_MINTTL:
1335 val = inet->min_ttl;
1336 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001337 default:
1338 release_sock(sk);
1339 return -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 }
1341 release_sock(sk);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001342
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001343 if (len < sizeof(int) && len > 0 && val >= 0 && val <= 255) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 unsigned char ucval = (unsigned char)val;
1345 len = 1;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001346 if (put_user(len, optlen))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 return -EFAULT;
Jianjun Kong09cb1052008-11-03 00:27:11 -08001348 if (copy_to_user(optval, &ucval, 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 return -EFAULT;
1350 } else {
1351 len = min_t(unsigned int, sizeof(int), len);
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, &val, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 return -EFAULT;
1356 }
1357 return 0;
1358}
1359
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001360int ip_getsockopt(struct sock *sk, int level,
Stephen Hemminger132adf52007-03-08 20:44:43 -08001361 int optname, char __user *optval, int __user *optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001362{
1363 int err;
1364
Daniel Balutadd23198e2011-08-07 22:31:07 -07001365 err = do_ip_getsockopt(sk, level, optname, optval, optlen, 0);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001366#ifdef CONFIG_NETFILTER
1367 /* we need to exclude all possible ENOPROTOOPTs except default case */
Pavel Emelyanov6a9fb942007-11-05 21:32:31 -08001368 if (err == -ENOPROTOOPT && optname != IP_PKTOPTIONS &&
1369 !ip_mroute_opt(optname)) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001370 int len;
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001371
Jianjun Kong09cb1052008-11-03 00:27:11 -08001372 if (get_user(len, optlen))
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001373 return -EFAULT;
1374
1375 lock_sock(sk);
1376 err = nf_getsockopt(sk, PF_INET, optname, optval,
1377 &len);
1378 release_sock(sk);
1379 if (err >= 0)
1380 err = put_user(len, optlen);
1381 return err;
1382 }
1383#endif
1384 return err;
1385}
Eric Dumazet4d52cfb2009-06-02 00:42:16 -07001386EXPORT_SYMBOL(ip_getsockopt);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001387
1388#ifdef CONFIG_COMPAT
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001389int compat_ip_getsockopt(struct sock *sk, int level, int optname,
1390 char __user *optval, int __user *optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001391{
David L Stevens42908c62008-04-29 03:23:22 -07001392 int err;
1393
1394 if (optname == MCAST_MSFILTER)
1395 return compat_mc_getsockopt(sk, level, optname, optval, optlen,
1396 ip_getsockopt);
1397
Daniel Balutadd23198e2011-08-07 22:31:07 -07001398 err = do_ip_getsockopt(sk, level, optname, optval, optlen,
1399 MSG_CMSG_COMPAT);
David L Stevens42908c62008-04-29 03:23:22 -07001400
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001401#ifdef CONFIG_NETFILTER
1402 /* we need to exclude all possible ENOPROTOOPTs except default case */
Pavel Emelyanov6a9fb942007-11-05 21:32:31 -08001403 if (err == -ENOPROTOOPT && optname != IP_PKTOPTIONS &&
1404 !ip_mroute_opt(optname)) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001405 int len;
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001406
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001407 if (get_user(len, optlen))
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001408 return -EFAULT;
1409
1410 lock_sock(sk);
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001411 err = compat_nf_getsockopt(sk, PF_INET, optname, optval, &len);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001412 release_sock(sk);
1413 if (err >= 0)
1414 err = put_user(len, optlen);
1415 return err;
1416 }
1417#endif
1418 return err;
1419}
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001420EXPORT_SYMBOL(compat_ip_getsockopt);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001421#endif