Branch data Line data Source code
1 : : /*
2 : : * INET An implementation of the TCP/IP protocol suite for the LINUX
3 : : * operating system. INET is implemented using the BSD Socket
4 : : * interface as the means of communication with the user level.
5 : : *
6 : : * The IP to API glue.
7 : : *
8 : : * Authors: see ip.c
9 : : *
10 : : * Fixes:
11 : : * Many : Split from ip.c , see ip.c for history.
12 : : * Martin Mares : TOS setting fixed.
13 : : * Alan Cox : Fixed a couple of oopses in Martin's
14 : : * TOS tweaks.
15 : : * Mike McLagan : Routing by source
16 : : */
17 : :
18 : : #include <linux/module.h>
19 : : #include <linux/types.h>
20 : : #include <linux/mm.h>
21 : : #include <linux/skbuff.h>
22 : : #include <linux/ip.h>
23 : : #include <linux/icmp.h>
24 : : #include <linux/inetdevice.h>
25 : : #include <linux/netdevice.h>
26 : : #include <linux/slab.h>
27 : : #include <net/sock.h>
28 : : #include <net/ip.h>
29 : : #include <net/icmp.h>
30 : : #include <net/tcp_states.h>
31 : : #include <linux/udp.h>
32 : : #include <linux/igmp.h>
33 : : #include <linux/netfilter.h>
34 : : #include <linux/route.h>
35 : : #include <linux/mroute.h>
36 : : #include <net/inet_ecn.h>
37 : : #include <net/route.h>
38 : : #include <net/xfrm.h>
39 : : #include <net/compat.h>
40 : : #if IS_ENABLED(CONFIG_IPV6)
41 : : #include <net/transp_v6.h>
42 : : #endif
43 : : #include <net/ip_fib.h>
44 : :
45 : : #include <linux/errqueue.h>
46 : : #include <asm/uaccess.h>
47 : :
48 : : #define IP_CMSG_PKTINFO 1
49 : : #define IP_CMSG_TTL 2
50 : : #define IP_CMSG_TOS 4
51 : : #define IP_CMSG_RECVOPTS 8
52 : : #define IP_CMSG_RETOPTS 16
53 : : #define IP_CMSG_PASSSEC 32
54 : : #define IP_CMSG_ORIGDSTADDR 64
55 : :
56 : : /*
57 : : * SOL_IP control messages.
58 : : */
59 : :
60 : 0 : static void ip_cmsg_recv_pktinfo(struct msghdr *msg, struct sk_buff *skb)
61 : : {
62 : 0 : struct in_pktinfo info = *PKTINFO_SKB_CB(skb);
63 : :
64 : 0 : info.ipi_addr.s_addr = ip_hdr(skb)->daddr;
65 : :
66 : 0 : put_cmsg(msg, SOL_IP, IP_PKTINFO, sizeof(info), &info);
67 : 0 : }
68 : :
69 : 0 : static void ip_cmsg_recv_ttl(struct msghdr *msg, struct sk_buff *skb)
70 : : {
71 : 0 : int ttl = ip_hdr(skb)->ttl;
72 : 0 : put_cmsg(msg, SOL_IP, IP_TTL, sizeof(int), &ttl);
73 : 0 : }
74 : :
75 : : static void ip_cmsg_recv_tos(struct msghdr *msg, struct sk_buff *skb)
76 : : {
77 : 0 : put_cmsg(msg, SOL_IP, IP_TOS, 1, &ip_hdr(skb)->tos);
78 : : }
79 : :
80 : 0 : static void ip_cmsg_recv_opts(struct msghdr *msg, struct sk_buff *skb)
81 : : {
82 [ # # ]: 0 : if (IPCB(skb)->opt.optlen == 0)
83 : 0 : return;
84 : :
85 : 0 : put_cmsg(msg, SOL_IP, IP_RECVOPTS, IPCB(skb)->opt.optlen,
86 : : ip_hdr(skb) + 1);
87 : : }
88 : :
89 : :
90 : 0 : static void ip_cmsg_recv_retopts(struct msghdr *msg, struct sk_buff *skb)
91 : : {
92 : : unsigned char optbuf[sizeof(struct ip_options) + 40];
93 : : struct ip_options *opt = (struct ip_options *)optbuf;
94 : :
95 [ # # ]: 0 : if (IPCB(skb)->opt.optlen == 0)
96 : 0 : return;
97 : :
98 [ # # ]: 0 : if (ip_options_echo(opt, skb)) {
99 : 0 : msg->msg_flags |= MSG_CTRUNC;
100 : 0 : return;
101 : : }
102 : 0 : ip_options_undo(opt);
103 : :
104 : 0 : put_cmsg(msg, SOL_IP, IP_RETOPTS, opt->optlen, opt->__data);
105 : : }
106 : :
107 : 0 : static void ip_cmsg_recv_security(struct msghdr *msg, struct sk_buff *skb)
108 : : {
109 : : char *secdata;
110 : : u32 seclen, secid;
111 : : int err;
112 : :
113 : 0 : err = security_socket_getpeersec_dgram(NULL, skb, &secid);
114 [ # # ]: 0 : if (err)
115 : 0 : return;
116 : :
117 : 0 : err = security_secid_to_secctx(secid, &secdata, &seclen);
118 [ # # ]: 0 : if (err)
119 : : return;
120 : :
121 : 0 : put_cmsg(msg, SOL_IP, SCM_SECURITY, seclen, secdata);
122 : 0 : security_release_secctx(secdata, seclen);
123 : : }
124 : :
125 : 0 : static void ip_cmsg_recv_dstaddr(struct msghdr *msg, struct sk_buff *skb)
126 : : {
127 : : struct sockaddr_in sin;
128 : : const struct iphdr *iph = ip_hdr(skb);
129 : : __be16 *ports = (__be16 *)skb_transport_header(skb);
130 : :
131 [ # # ]: 0 : if (skb_transport_offset(skb) + 4 > skb->len)
132 : 0 : 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 : 0 : sin.sin_family = AF_INET;
140 : 0 : sin.sin_addr.s_addr = iph->daddr;
141 : 0 : sin.sin_port = ports[1];
142 : 0 : memset(sin.sin_zero, 0, sizeof(sin.sin_zero));
143 : :
144 : 0 : put_cmsg(msg, SOL_IP, IP_ORIGDSTADDR, sizeof(sin), &sin);
145 : : }
146 : :
147 : 0 : void ip_cmsg_recv(struct msghdr *msg, struct sk_buff *skb)
148 : : {
149 : 0 : struct inet_sock *inet = inet_sk(skb->sk);
150 : 0 : unsigned int flags = inet->cmsg_flags;
151 : :
152 : : /* Ordered by supposed usage frequency */
153 [ # # ]: 0 : if (flags & 1)
154 : 0 : ip_cmsg_recv_pktinfo(msg, skb);
155 [ # # ]: 0 : if ((flags >>= 1) == 0)
156 : : return;
157 : :
158 [ # # ]: 0 : if (flags & 1)
159 : 0 : ip_cmsg_recv_ttl(msg, skb);
160 [ # # ]: 0 : if ((flags >>= 1) == 0)
161 : : return;
162 : :
163 [ # # ]: 0 : if (flags & 1)
164 : : ip_cmsg_recv_tos(msg, skb);
165 [ # # ]: 0 : if ((flags >>= 1) == 0)
166 : : return;
167 : :
168 [ # # ]: 0 : if (flags & 1)
169 : 0 : ip_cmsg_recv_opts(msg, skb);
170 [ # # ]: 0 : if ((flags >>= 1) == 0)
171 : : return;
172 : :
173 [ # # ]: 0 : if (flags & 1)
174 : 0 : ip_cmsg_recv_retopts(msg, skb);
175 [ # # ]: 0 : if ((flags >>= 1) == 0)
176 : : return;
177 : :
178 [ # # ]: 0 : if (flags & 1)
179 : 0 : ip_cmsg_recv_security(msg, skb);
180 : :
181 [ # # ]: 0 : if ((flags >>= 1) == 0)
182 : : return;
183 [ # # ]: 0 : if (flags & 1)
184 : 0 : ip_cmsg_recv_dstaddr(msg, skb);
185 : :
186 : : }
187 : : EXPORT_SYMBOL(ip_cmsg_recv);
188 : :
189 : 0 : int ip_cmsg_send(struct net *net, struct msghdr *msg, struct ipcm_cookie *ipc)
190 : : {
191 : : int err, val;
192 : : struct cmsghdr *cmsg;
193 : :
194 [ # # ][ # # ]: 0 : for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) {
195 [ # # ][ # # ]: 0 : if (!CMSG_OK(msg, cmsg))
196 : : return -EINVAL;
197 [ # # ]: 0 : if (cmsg->cmsg_level != SOL_IP)
198 : 0 : continue;
199 [ # # # # : 0 : switch (cmsg->cmsg_type) {
# ]
200 : : case IP_RETOPTS:
201 : 0 : err = cmsg->cmsg_len - CMSG_ALIGN(sizeof(struct cmsghdr));
202 : 0 : err = ip_options_get(net, &ipc->opt, CMSG_DATA(cmsg),
203 : : err < 40 ? err : 40);
204 [ # # ]: 0 : if (err)
205 : : return err;
206 : : break;
207 : : case IP_PKTINFO:
208 : : {
209 : : struct in_pktinfo *info;
210 [ # # ]: 0 : if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct in_pktinfo)))
211 : : return -EINVAL;
212 : : info = (struct in_pktinfo *)CMSG_DATA(cmsg);
213 : 0 : ipc->oif = info->ipi_ifindex;
214 : 0 : ipc->addr = info->ipi_spec_dst.s_addr;
215 : 0 : break;
216 : : }
217 : : case IP_TTL:
218 [ # # ]: 0 : if (cmsg->cmsg_len != CMSG_LEN(sizeof(int)))
219 : : return -EINVAL;
220 : 0 : val = *(int *)CMSG_DATA(cmsg);
221 [ # # ]: 0 : if (val < 1 || val > 255)
222 : : return -EINVAL;
223 : 0 : ipc->ttl = val;
224 : 0 : break;
225 : : case IP_TOS:
226 [ # # ]: 0 : if (cmsg->cmsg_len != CMSG_LEN(sizeof(int)))
227 : : return -EINVAL;
228 : 0 : val = *(int *)CMSG_DATA(cmsg);
229 [ # # ]: 0 : if (val < 0 || val > 255)
230 : : return -EINVAL;
231 : 0 : ipc->tos = val;
232 : 0 : ipc->priority = rt_tos2priority(ipc->tos);
233 : 0 : break;
234 : :
235 : : 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 : : */
253 : : struct ip_ra_chain __rcu *ip_ra_chain;
254 : : static DEFINE_SPINLOCK(ip_ra_lock);
255 : :
256 : :
257 : 0 : static void ip_ra_destroy_rcu(struct rcu_head *head)
258 : : {
259 : 0 : struct ip_ra_chain *ra = container_of(head, struct ip_ra_chain, rcu);
260 : :
261 : 0 : sock_put(ra->saved_sk);
262 : 0 : kfree(ra);
263 : 0 : }
264 : :
265 : 0 : int ip_ra_control(struct sock *sk, unsigned char on,
266 : : void (*destructor)(struct sock *))
267 : : {
268 : : struct ip_ra_chain *ra, *new_ra;
269 : : struct ip_ra_chain __rcu **rap;
270 : :
271 [ # # ][ # # ]: 0 : if (sk->sk_type != SOCK_RAW || inet_sk(sk)->inet_num == IPPROTO_RAW)
272 : : return -EINVAL;
273 : :
274 [ # # ]: 0 : new_ra = on ? kmalloc(sizeof(*new_ra), GFP_KERNEL) : NULL;
275 : :
276 : : spin_lock_bh(&ip_ra_lock);
277 [ # # ]: 0 : for (rap = &ip_ra_chain;
278 : 0 : (ra = rcu_dereference_protected(*rap,
279 : : lockdep_is_held(&ip_ra_lock))) != NULL;
280 : 0 : rap = &ra->next) {
281 [ # # ]: 0 : if (ra->sk == sk) {
282 [ # # ]: 0 : if (on) {
283 : : spin_unlock_bh(&ip_ra_lock);
284 : 0 : kfree(new_ra);
285 : 0 : return -EADDRINUSE;
286 : : }
287 : : /* dont let ip_call_ra_chain() use sk again */
288 : 0 : ra->sk = NULL;
289 : 0 : rcu_assign_pointer(*rap, ra->next);
290 : : spin_unlock_bh(&ip_ra_lock);
291 : :
292 [ # # ]: 0 : if (ra->destructor)
293 : 0 : ra->destructor(sk);
294 : : /*
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 : 0 : ra->saved_sk = sk;
300 : 0 : call_rcu(&ra->rcu, ip_ra_destroy_rcu);
301 : 0 : return 0;
302 : : }
303 : : }
304 [ # # ]: 0 : if (new_ra == NULL) {
305 : : spin_unlock_bh(&ip_ra_lock);
306 : 0 : return -ENOBUFS;
307 : : }
308 : 0 : new_ra->sk = sk;
309 : 0 : new_ra->destructor = destructor;
310 : :
311 : 0 : new_ra->next = ra;
312 : 0 : rcu_assign_pointer(*rap, new_ra);
313 : : sock_hold(sk);
314 : : spin_unlock_bh(&ip_ra_lock);
315 : :
316 : 0 : return 0;
317 : : }
318 : :
319 : 0 : void ip_icmp_error(struct sock *sk, struct sk_buff *skb, int err,
320 : : __be16 port, u32 info, u8 *payload)
321 : : {
322 : : struct sock_exterr_skb *serr;
323 : :
324 : 0 : skb = skb_clone(skb, GFP_ATOMIC);
325 [ # # ]: 0 : if (!skb)
326 : : return;
327 : :
328 : : serr = SKB_EXT_ERR(skb);
329 : 0 : serr->ee.ee_errno = err;
330 : 0 : serr->ee.ee_origin = SO_EE_ORIGIN_ICMP;
331 : 0 : serr->ee.ee_type = icmp_hdr(skb)->type;
332 : 0 : serr->ee.ee_code = icmp_hdr(skb)->code;
333 : 0 : serr->ee.ee_pad = 0;
334 : 0 : serr->ee.ee_info = info;
335 : 0 : serr->ee.ee_data = 0;
336 : 0 : serr->addr_offset = (u8 *)&(((struct iphdr *)(icmp_hdr(skb) + 1))->daddr) -
337 : : skb_network_header(skb);
338 : 0 : serr->port = port;
339 : :
340 [ # # ]: 0 : if (skb_pull(skb, payload - skb->data) != NULL) {
341 : : skb_reset_transport_header(skb);
342 [ # # ]: 0 : if (sock_queue_err_skb(sk, skb) == 0)
343 : : return;
344 : : }
345 : 0 : kfree_skb(skb);
346 : : }
347 : :
348 : 0 : void ip_local_error(struct sock *sk, int err, __be32 daddr, __be16 port, u32 info)
349 : : {
350 : : struct inet_sock *inet = inet_sk(sk);
351 : : struct sock_exterr_skb *serr;
352 : : struct iphdr *iph;
353 : 0 : struct sk_buff *skb;
354 : :
355 [ # # ]: 0 : if (!inet->recverr)
356 : : return;
357 : :
358 : : skb = alloc_skb(sizeof(struct iphdr), GFP_ATOMIC);
359 [ # # ]: 0 : if (!skb)
360 : : return;
361 : :
362 : 0 : skb_put(skb, sizeof(struct iphdr));
363 : : skb_reset_network_header(skb);
364 : : iph = ip_hdr(skb);
365 : 0 : iph->daddr = daddr;
366 : :
367 : : serr = SKB_EXT_ERR(skb);
368 : 0 : serr->ee.ee_errno = err;
369 : 0 : serr->ee.ee_origin = SO_EE_ORIGIN_LOCAL;
370 : 0 : serr->ee.ee_type = 0;
371 : 0 : serr->ee.ee_code = 0;
372 : 0 : serr->ee.ee_pad = 0;
373 : 0 : serr->ee.ee_info = info;
374 : 0 : serr->ee.ee_data = 0;
375 : 0 : serr->addr_offset = (u8 *)&iph->daddr - skb_network_header(skb);
376 : 0 : serr->port = port;
377 : :
378 : 0 : __skb_pull(skb, skb_tail_pointer(skb) - skb->data);
379 : : skb_reset_transport_header(skb);
380 : :
381 [ # # ]: 0 : if (sock_queue_err_skb(sk, skb))
382 : 0 : kfree_skb(skb);
383 : : }
384 : :
385 : : /*
386 : : * Handle MSG_ERRQUEUE
387 : : */
388 : 0 : int ip_recv_error(struct sock *sk, struct msghdr *msg, int len, int *addr_len)
389 : : {
390 : : struct sock_exterr_skb *serr;
391 : 0 : struct sk_buff *skb, *skb2;
392 : 0 : DECLARE_SOCKADDR(struct sockaddr_in *, sin, msg->msg_name);
393 : : 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 : 0 : skb = skb_dequeue(&sk->sk_error_queue);
402 [ # # ]: 0 : if (skb == NULL)
403 : : goto out;
404 : :
405 : 0 : copied = skb->len;
406 [ # # ]: 0 : if (copied > len) {
407 : 0 : msg->msg_flags |= MSG_TRUNC;
408 : : copied = len;
409 : : }
410 : 0 : err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
411 [ # # ]: 0 : if (err)
412 : : goto out_free_skb;
413 : :
414 : : sock_recv_timestamp(msg, sk, skb);
415 : :
416 : : serr = SKB_EXT_ERR(skb);
417 : :
418 [ # # ]: 0 : if (sin) {
419 : 0 : sin->sin_family = AF_INET;
420 : 0 : sin->sin_addr.s_addr = *(__be32 *)(skb_network_header(skb) +
421 : 0 : serr->addr_offset);
422 : 0 : sin->sin_port = serr->port;
423 : 0 : memset(&sin->sin_zero, 0, sizeof(sin->sin_zero));
424 : 0 : *addr_len = sizeof(*sin);
425 : : }
426 : :
427 : 0 : memcpy(&errhdr.ee, &serr->ee, sizeof(struct sock_extended_err));
428 : : sin = &errhdr.offender;
429 : 0 : sin->sin_family = AF_UNSPEC;
430 [ # # ]: 0 : if (serr->ee.ee_origin == SO_EE_ORIGIN_ICMP) {
431 : : struct inet_sock *inet = inet_sk(sk);
432 : :
433 : 0 : sin->sin_family = AF_INET;
434 : 0 : sin->sin_addr.s_addr = ip_hdr(skb)->saddr;
435 : 0 : sin->sin_port = 0;
436 : 0 : memset(&sin->sin_zero, 0, sizeof(sin->sin_zero));
437 [ # # ]: 0 : if (inet->cmsg_flags)
438 : 0 : ip_cmsg_recv(msg, skb);
439 : : }
440 : :
441 : 0 : put_cmsg(msg, SOL_IP, IP_RECVERR, sizeof(errhdr), &errhdr);
442 : :
443 : : /* Now we could try to dump offended packet options */
444 : :
445 : 0 : msg->msg_flags |= MSG_ERRQUEUE;
446 : : err = copied;
447 : :
448 : : /* Reset and regenerate socket error */
449 : : spin_lock_bh(&sk->sk_error_queue.lock);
450 : 0 : sk->sk_err = 0;
451 : : skb2 = skb_peek(&sk->sk_error_queue);
452 [ # # ]: 0 : if (skb2 != NULL) {
453 : 0 : sk->sk_err = SKB_EXT_ERR(skb2)->ee.ee_errno;
454 : : spin_unlock_bh(&sk->sk_error_queue.lock);
455 : 0 : sk->sk_error_report(sk);
456 : : } else
457 : : spin_unlock_bh(&sk->sk_error_queue.lock);
458 : :
459 : : out_free_skb:
460 : 0 : kfree_skb(skb);
461 : : out:
462 : 0 : return err;
463 : : }
464 : :
465 : :
466 : : /*
467 : : * Socket option code for IP. This is the end of the line after any
468 : : * TCP,UDP etc options on an IP socket.
469 : : */
470 : :
471 : 0 : static int do_ip_setsockopt(struct sock *sk, int level,
472 : : int optname, char __user *optval, unsigned int optlen)
473 : : {
474 : : struct inet_sock *inet = inet_sk(sk);
475 : : int val = 0, err;
476 : :
477 [ - + ]: 1 : 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:
499 [ # # ]: 0 : if (optlen >= sizeof(int)) {
500 [ # # ]: 0 : if (get_user(val, (int __user *) optval))
501 : : return -EFAULT;
502 [ # # ]: 0 : } else if (optlen >= sizeof(char)) {
503 : : unsigned char ucval;
504 : :
505 [ # # ]: 0 : if (get_user(ucval, (unsigned char __user *) optval))
506 : : return -EFAULT;
507 : 0 : val = (int) ucval;
508 : : }
509 : : }
510 : :
511 : : /* If optlen==0, it is equivalent to val == 0 */
512 : :
513 : : if (ip_mroute_opt(optname))
514 : : return ip_mroute_setsockopt(sk, optname, optval, optlen);
515 : :
516 : : err = 0;
517 : : lock_sock(sk);
518 : :
519 [ - - - - : 1 : switch (optname) {
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- + ]
520 : : case IP_OPTIONS:
521 : : {
522 : 0 : struct ip_options_rcu *old, *opt = NULL;
523 : :
524 [ # # ]: 0 : if (optlen > 40)
525 : : goto e_inval;
526 : 0 : err = ip_options_get_from_user(sock_net(sk), &opt,
527 : : optval, optlen);
528 [ # # ]: 0 : if (err)
529 : : break;
530 : 0 : old = rcu_dereference_protected(inet->inet_opt,
531 : : sock_owned_by_user(sk));
532 [ # # ]: 0 : if (inet->is_icsk) {
533 : : struct inet_connection_sock *icsk = inet_csk(sk);
534 : : #if IS_ENABLED(CONFIG_IPV6)
535 [ # # ][ # # ]: 0 : if (sk->sk_family == PF_INET ||
536 : 0 : (!((1 << sk->sk_state) &
537 [ # # ]: 0 : (TCPF_LISTEN | TCPF_CLOSE)) &&
538 : 0 : inet->inet_daddr != LOOPBACK4_IPV6)) {
539 : : #endif
540 [ # # ]: 0 : if (old)
541 : 0 : icsk->icsk_ext_hdr_len -= old->opt.optlen;
542 [ # # ]: 0 : if (opt)
543 : 0 : icsk->icsk_ext_hdr_len += opt->opt.optlen;
544 : 0 : icsk->icsk_sync_mss(sk, icsk->icsk_pmtu_cookie);
545 : : #if IS_ENABLED(CONFIG_IPV6)
546 : : }
547 : : #endif
548 : : }
549 : 0 : rcu_assign_pointer(inet->inet_opt, opt);
550 [ # # ]: 0 : if (old)
551 : 0 : kfree_rcu(old, rcu);
552 : : break;
553 : : }
554 : : case IP_PKTINFO:
555 [ # # ]: 0 : if (val)
556 : 0 : inet->cmsg_flags |= IP_CMSG_PKTINFO;
557 : : else
558 : 0 : inet->cmsg_flags &= ~IP_CMSG_PKTINFO;
559 : : break;
560 : : case IP_RECVTTL:
561 [ # # ]: 0 : if (val)
562 : 0 : inet->cmsg_flags |= IP_CMSG_TTL;
563 : : else
564 : 0 : inet->cmsg_flags &= ~IP_CMSG_TTL;
565 : : break;
566 : : case IP_RECVTOS:
567 [ # # ]: 0 : if (val)
568 : 0 : inet->cmsg_flags |= IP_CMSG_TOS;
569 : : else
570 : 0 : inet->cmsg_flags &= ~IP_CMSG_TOS;
571 : : break;
572 : : case IP_RECVOPTS:
573 [ # # ]: 0 : if (val)
574 : 0 : inet->cmsg_flags |= IP_CMSG_RECVOPTS;
575 : : else
576 : 0 : inet->cmsg_flags &= ~IP_CMSG_RECVOPTS;
577 : : break;
578 : : case IP_RETOPTS:
579 [ # # ]: 0 : if (val)
580 : 0 : inet->cmsg_flags |= IP_CMSG_RETOPTS;
581 : : else
582 : 0 : inet->cmsg_flags &= ~IP_CMSG_RETOPTS;
583 : : break;
584 : : case IP_PASSSEC:
585 [ # # ]: 0 : if (val)
586 : 0 : inet->cmsg_flags |= IP_CMSG_PASSSEC;
587 : : else
588 : 0 : inet->cmsg_flags &= ~IP_CMSG_PASSSEC;
589 : : break;
590 : : case IP_RECVORIGDSTADDR:
591 [ # # ]: 0 : if (val)
592 : 0 : inet->cmsg_flags |= IP_CMSG_ORIGDSTADDR;
593 : : else
594 : 0 : inet->cmsg_flags &= ~IP_CMSG_ORIGDSTADDR;
595 : : break;
596 : : case IP_TOS: /* This sets both TOS and Precedence */
597 [ # # ]: 0 : if (sk->sk_type == SOCK_STREAM) {
598 : 0 : val &= ~INET_ECN_MASK;
599 : 0 : val |= inet->tos & INET_ECN_MASK;
600 : : }
601 [ # # ]: 0 : if (inet->tos != val) {
602 : 0 : inet->tos = val;
603 : 0 : sk->sk_priority = rt_tos2priority(val);
604 : : sk_dst_reset(sk);
605 : : }
606 : : break;
607 : : case IP_TTL:
608 [ # # ]: 0 : if (optlen < 1)
609 : : goto e_inval;
610 [ # # ]: 0 : if (val != -1 && (val < 1 || val > 255))
611 : : goto e_inval;
612 : 0 : inet->uc_ttl = val;
613 : : break;
614 : : case IP_HDRINCL:
615 [ # # ]: 0 : if (sk->sk_type != SOCK_RAW) {
616 : : err = -ENOPROTOOPT;
617 : : break;
618 : : }
619 : 0 : inet->hdrincl = val ? 1 : 0;
620 : : break;
621 : : case IP_NODEFRAG:
622 [ # # ]: 0 : if (sk->sk_type != SOCK_RAW) {
623 : : err = -ENOPROTOOPT;
624 : : break;
625 : : }
626 : 0 : inet->nodefrag = val ? 1 : 0;
627 : : break;
628 : : case IP_MTU_DISCOVER:
629 [ # # ]: 0 : if (val < IP_PMTUDISC_DONT || val > IP_PMTUDISC_INTERFACE)
630 : : goto e_inval;
631 : 0 : inet->pmtudisc = val;
632 : : break;
633 : : case IP_RECVERR:
634 : 0 : inet->recverr = !!val;
635 [ # # ]: 0 : if (!val)
636 : 0 : skb_queue_purge(&sk->sk_error_queue);
637 : : break;
638 : : case IP_MULTICAST_TTL:
639 [ # # ]: 0 : if (sk->sk_type == SOCK_STREAM)
640 : : goto e_inval;
641 [ # # ]: 0 : if (optlen < 1)
642 : : goto e_inval;
643 [ # # ]: 0 : if (val == -1)
644 : : val = 1;
645 [ # # ]: 0 : if (val < 0 || val > 255)
646 : : goto e_inval;
647 : 0 : inet->mc_ttl = val;
648 : : break;
649 : : case IP_MULTICAST_LOOP:
650 [ # # ]: 0 : if (optlen < 1)
651 : : goto e_inval;
652 : 0 : inet->mc_loop = !!val;
653 : : break;
654 : : case IP_UNICAST_IF:
655 : : {
656 : : struct net_device *dev = NULL;
657 : : int ifindex;
658 : :
659 [ # # ]: 0 : if (optlen != sizeof(int))
660 : : goto e_inval;
661 : :
662 [ # # ]: 0 : ifindex = (__force int)ntohl((__force __be32)val);
663 [ # # ]: 0 : if (ifindex == 0) {
664 : 0 : inet->uc_index = 0;
665 : : err = 0;
666 : : break;
667 : : }
668 : :
669 : 0 : dev = dev_get_by_index(sock_net(sk), ifindex);
670 : : err = -EADDRNOTAVAIL;
671 [ # # ]: 0 : if (!dev)
672 : : break;
673 : : dev_put(dev);
674 : :
675 : : err = -EINVAL;
676 [ # # ]: 0 : if (sk->sk_bound_dev_if)
677 : : break;
678 : :
679 : 0 : inet->uc_index = ifindex;
680 : : err = 0;
681 : : break;
682 : : }
683 : : case IP_MULTICAST_IF:
684 : : {
685 : : struct ip_mreqn mreq;
686 : : struct net_device *dev = NULL;
687 : :
688 [ # # ]: 0 : if (sk->sk_type == SOCK_STREAM)
689 : : goto e_inval;
690 : : /*
691 : : * Check the arguments are allowable
692 : : */
693 : :
694 [ # # ]: 0 : if (optlen < sizeof(struct in_addr))
695 : : goto e_inval;
696 : :
697 : : err = -EFAULT;
698 [ # # ]: 0 : if (optlen >= sizeof(struct ip_mreqn)) {
699 [ # # ]: 0 : if (copy_from_user(&mreq, optval, sizeof(mreq)))
700 : : break;
701 : : } else {
702 : 0 : memset(&mreq, 0, sizeof(mreq));
703 [ # # ]: 0 : if (optlen >= sizeof(struct ip_mreq)) {
704 [ # # ]: 0 : if (copy_from_user(&mreq, optval,
705 : : sizeof(struct ip_mreq)))
706 : : break;
707 [ # # ]: 0 : } else if (optlen >= sizeof(struct in_addr)) {
708 [ # # ]: 0 : if (copy_from_user(&mreq.imr_address, optval,
709 : : sizeof(struct in_addr)))
710 : : break;
711 : : }
712 : : }
713 : :
714 [ # # ]: 0 : if (!mreq.imr_ifindex) {
715 [ # # ]: 0 : if (mreq.imr_address.s_addr == htonl(INADDR_ANY)) {
716 : 0 : inet->mc_index = 0;
717 : 0 : inet->mc_addr = 0;
718 : : err = 0;
719 : : break;
720 : : }
721 : : dev = ip_dev_find(sock_net(sk), mreq.imr_address.s_addr);
722 [ # # ]: 0 : if (dev)
723 : 0 : mreq.imr_ifindex = dev->ifindex;
724 : : } else
725 : 0 : dev = dev_get_by_index(sock_net(sk), mreq.imr_ifindex);
726 : :
727 : :
728 : : err = -EADDRNOTAVAIL;
729 [ # # ]: 0 : if (!dev)
730 : : break;
731 : : dev_put(dev);
732 : :
733 : : err = -EINVAL;
734 [ # # ][ # # ]: 0 : if (sk->sk_bound_dev_if &&
735 : 0 : mreq.imr_ifindex != sk->sk_bound_dev_if)
736 : : break;
737 : :
738 : 0 : inet->mc_index = mreq.imr_ifindex;
739 : 0 : 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 : :
749 : : err = -EPROTO;
750 [ # # ]: 0 : if (inet_sk(sk)->is_icsk)
751 : : break;
752 : :
753 [ # # ]: 0 : if (optlen < sizeof(struct ip_mreq))
754 : : goto e_inval;
755 : : err = -EFAULT;
756 [ # # ]: 0 : if (optlen >= sizeof(struct ip_mreqn)) {
757 [ # # ]: 0 : if (copy_from_user(&mreq, optval, sizeof(mreq)))
758 : : break;
759 : : } else {
760 : 0 : memset(&mreq, 0, sizeof(mreq));
761 [ # # ]: 0 : if (copy_from_user(&mreq, optval, sizeof(struct ip_mreq)))
762 : : break;
763 : : }
764 : :
765 [ # # ]: 0 : if (optname == IP_ADD_MEMBERSHIP)
766 : 0 : err = ip_mc_join_group(sk, &mreq);
767 : : else
768 : 0 : err = ip_mc_leave_group(sk, &mreq);
769 : : break;
770 : : }
771 : : case IP_MSFILTER:
772 : : {
773 : : struct ip_msfilter *msf;
774 : :
775 [ # # ]: 0 : if (optlen < IP_MSFILTER_SIZE(0))
776 : : goto e_inval;
777 [ # # ]: 0 : if (optlen > sysctl_optmem_max) {
778 : : err = -ENOBUFS;
779 : : break;
780 : : }
781 : : msf = kmalloc(optlen, GFP_KERNEL);
782 [ # # ]: 0 : if (!msf) {
783 : : err = -ENOBUFS;
784 : : break;
785 : : }
786 : : err = -EFAULT;
787 [ # # ]: 0 : if (copy_from_user(msf, optval, optlen)) {
788 : 0 : kfree(msf);
789 : : break;
790 : : }
791 : : /* numsrc >= (1G-4) overflow in 32 bits */
792 [ # # ][ # # ]: 0 : if (msf->imsf_numsrc >= 0x3ffffffcU ||
793 : 0 : msf->imsf_numsrc > sysctl_igmp_max_msf) {
794 : 0 : kfree(msf);
795 : : err = -ENOBUFS;
796 : : break;
797 : : }
798 [ # # ]: 0 : if (IP_MSFILTER_SIZE(msf->imsf_numsrc) > optlen) {
799 : 0 : kfree(msf);
800 : : err = -EINVAL;
801 : : break;
802 : : }
803 : 0 : err = ip_mc_msfilter(sk, msf, 0);
804 : 0 : 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 [ # # ]: 0 : if (optlen != sizeof(struct ip_mreq_source))
816 : : goto e_inval;
817 [ # # ]: 0 : if (copy_from_user(&mreqs, optval, sizeof(mreqs))) {
818 : : err = -EFAULT;
819 : 0 : break;
820 : : }
821 [ # # ]: 0 : if (optname == IP_BLOCK_SOURCE) {
822 : : omode = MCAST_EXCLUDE;
823 : : add = 1;
824 [ # # ]: 0 : } else if (optname == IP_UNBLOCK_SOURCE) {
825 : : omode = MCAST_EXCLUDE;
826 : : add = 0;
827 [ # # ]: 0 : } else if (optname == IP_ADD_SOURCE_MEMBERSHIP) {
828 : : struct ip_mreqn mreq;
829 : :
830 : 0 : mreq.imr_multiaddr.s_addr = mreqs.imr_multiaddr;
831 : 0 : mreq.imr_address.s_addr = mreqs.imr_interface;
832 : 0 : mreq.imr_ifindex = 0;
833 : 0 : err = ip_mc_join_group(sk, &mreq);
834 [ # # ]: 0 : 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 : 0 : 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 [ # # ]: 0 : if (optlen < sizeof(struct group_req))
853 : : goto e_inval;
854 : : err = -EFAULT;
855 [ # # ]: 0 : if (copy_from_user(&greq, optval, sizeof(greq)))
856 : : break;
857 : : psin = (struct sockaddr_in *)&greq.gr_group;
858 [ # # ]: 0 : if (psin->sin_family != AF_INET)
859 : : goto e_inval;
860 : 0 : memset(&mreq, 0, sizeof(mreq));
861 : 0 : mreq.imr_multiaddr = psin->sin_addr;
862 : 0 : mreq.imr_ifindex = greq.gr_interface;
863 : :
864 [ # # ]: 0 : if (optname == MCAST_JOIN_GROUP)
865 : 0 : err = ip_mc_join_group(sk, &mreq);
866 : : else
867 : 0 : 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 [ # # ]: 0 : if (optlen != sizeof(struct group_source_req))
881 : : goto e_inval;
882 [ # # ]: 0 : if (copy_from_user(&greqs, optval, sizeof(greqs))) {
883 : : err = -EFAULT;
884 : 0 : break;
885 : : }
886 [ # # ][ # # ]: 0 : if (greqs.gsr_group.ss_family != AF_INET ||
887 : 0 : greqs.gsr_source.ss_family != AF_INET) {
888 : : err = -EADDRNOTAVAIL;
889 : : break;
890 : : }
891 : : psin = (struct sockaddr_in *)&greqs.gsr_group;
892 : 0 : mreqs.imr_multiaddr = psin->sin_addr.s_addr;
893 : : psin = (struct sockaddr_in *)&greqs.gsr_source;
894 : 0 : mreqs.imr_sourceaddr = psin->sin_addr.s_addr;
895 : 0 : mreqs.imr_interface = 0; /* use index for mc_source */
896 : :
897 [ # # ]: 0 : if (optname == MCAST_BLOCK_SOURCE) {
898 : : omode = MCAST_EXCLUDE;
899 : : add = 1;
900 [ # # ]: 0 : } else if (optname == MCAST_UNBLOCK_SOURCE) {
901 : : omode = MCAST_EXCLUDE;
902 : : add = 0;
903 [ # # ]: 0 : } else if (optname == MCAST_JOIN_SOURCE_GROUP) {
904 : : struct ip_mreqn mreq;
905 : :
906 : : psin = (struct sockaddr_in *)&greqs.gsr_group;
907 : 0 : mreq.imr_multiaddr = psin->sin_addr;
908 : 0 : mreq.imr_address.s_addr = 0;
909 : 0 : mreq.imr_ifindex = greqs.gsr_interface;
910 : 0 : err = ip_mc_join_group(sk, &mreq);
911 [ # # ]: 0 : if (err && err != -EADDRINUSE)
912 : : break;
913 : 0 : 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 : 0 : err = ip_mc_source(add, omode, sk, &mreqs,
921 : 0 : greqs.gsr_interface);
922 : : break;
923 : : }
924 : : case MCAST_MSFILTER:
925 : : {
926 : : struct sockaddr_in *psin;
927 : : struct ip_msfilter *msf = NULL;
928 : : struct group_filter *gsf = NULL;
929 : : int msize, i, ifindex;
930 : :
931 [ # # ]: 0 : if (optlen < GROUP_FILTER_SIZE(0))
932 : : goto e_inval;
933 [ # # ]: 0 : if (optlen > sysctl_optmem_max) {
934 : : err = -ENOBUFS;
935 : : break;
936 : : }
937 : : gsf = kmalloc(optlen, GFP_KERNEL);
938 [ # # ]: 0 : if (!gsf) {
939 : : err = -ENOBUFS;
940 : : break;
941 : : }
942 : : err = -EFAULT;
943 [ # # ]: 0 : if (copy_from_user(gsf, optval, optlen))
944 : : goto mc_msf_out;
945 : :
946 : : /* numsrc >= (4G-140)/128 overflow in 32 bits */
947 [ # # ][ # # ]: 0 : if (gsf->gf_numsrc >= 0x1ffffff ||
948 : 0 : gsf->gf_numsrc > sysctl_igmp_max_msf) {
949 : : err = -ENOBUFS;
950 : : goto mc_msf_out;
951 : : }
952 [ # # ]: 0 : if (GROUP_FILTER_SIZE(gsf->gf_numsrc) > optlen) {
953 : : err = -EINVAL;
954 : : goto mc_msf_out;
955 : : }
956 : 0 : msize = IP_MSFILTER_SIZE(gsf->gf_numsrc);
957 : : msf = kmalloc(msize, GFP_KERNEL);
958 [ # # ]: 0 : if (!msf) {
959 : : err = -ENOBUFS;
960 : : goto mc_msf_out;
961 : : }
962 : 0 : ifindex = gsf->gf_interface;
963 : : psin = (struct sockaddr_in *)&gsf->gf_group;
964 [ # # ]: 0 : if (psin->sin_family != AF_INET) {
965 : : err = -EADDRNOTAVAIL;
966 : : goto mc_msf_out;
967 : : }
968 : 0 : msf->imsf_multiaddr = psin->sin_addr.s_addr;
969 : 0 : msf->imsf_interface = 0;
970 : 0 : msf->imsf_fmode = gsf->gf_fmode;
971 : 0 : msf->imsf_numsrc = gsf->gf_numsrc;
972 : : err = -EADDRNOTAVAIL;
973 [ # # ]: 0 : for (i = 0; i < gsf->gf_numsrc; ++i) {
974 : 0 : psin = (struct sockaddr_in *)&gsf->gf_slist[i];
975 : :
976 [ # # ]: 0 : if (psin->sin_family != AF_INET)
977 : : goto mc_msf_out;
978 : 0 : msf->imsf_slist[i] = psin->sin_addr.s_addr;
979 : : }
980 : 0 : kfree(gsf);
981 : : gsf = NULL;
982 : :
983 : 0 : err = ip_mc_msfilter(sk, msf, ifindex);
984 : : mc_msf_out:
985 : 0 : kfree(msf);
986 : 0 : kfree(gsf);
987 : : break;
988 : : }
989 : : case IP_MULTICAST_ALL:
990 [ # # ]: 0 : if (optlen < 1)
991 : : goto e_inval;
992 [ # # ]: 0 : if (val != 0 && val != 1)
993 : : goto e_inval;
994 : 0 : inet->mc_all = val;
995 : : break;
996 : : case IP_ROUTER_ALERT:
997 : 0 : err = ip_ra_control(sk, val ? 1 : 0, NULL);
998 : : break;
999 : :
1000 : : case IP_FREEBIND:
1001 [ # # ]: 0 : if (optlen < 1)
1002 : : goto e_inval;
1003 : 0 : inet->freebind = !!val;
1004 : : break;
1005 : :
1006 : : case IP_IPSEC_POLICY:
1007 : : case IP_XFRM_POLICY:
1008 : : err = -EPERM;
1009 [ # # ]: 0 : if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
1010 : : break;
1011 : 0 : err = xfrm_user_policy(sk, optname, optval, optlen);
1012 : : break;
1013 : :
1014 : : case IP_TRANSPARENT:
1015 [ # # ]: 0 : if (!!val && !ns_capable(sock_net(sk)->user_ns, CAP_NET_RAW) &&
[ # # # # ]
1016 : 0 : !ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN)) {
1017 : : err = -EPERM;
1018 : : break;
1019 : : }
1020 [ # # ]: 0 : if (optlen < 1)
1021 : : goto e_inval;
1022 : 0 : inet->transparent = !!val;
1023 : : break;
1024 : :
1025 : : case IP_MINTTL:
1026 [ # # ]: 0 : if (optlen < 1)
1027 : : goto e_inval;
1028 [ # # ]: 0 : if (val < 0 || val > 255)
1029 : : goto e_inval;
1030 : 0 : inet->min_ttl = val;
1031 : : break;
1032 : :
1033 : : default:
1034 : : err = -ENOPROTOOPT;
1035 : : break;
1036 : : }
1037 : 1 : release_sock(sk);
1038 : : return err;
1039 : :
1040 : : e_inval:
1041 : 0 : release_sock(sk);
1042 : : return -EINVAL;
1043 : : }
1044 : :
1045 : : /**
1046 : : * ipv4_pktinfo_prepare - transfert some info from rtable to skb
1047 : : * @sk: socket
1048 : : * @skb: buffer
1049 : : *
1050 : : * To support IP_CMSG_PKTINFO option, we store rt_iif and specific
1051 : : * destination in skb->cb[] before dst drop.
1052 : : * This way, receiver doesn't make cache line misses to read rtable.
1053 : : */
1054 : 0 : void ipv4_pktinfo_prepare(const struct sock *sk, struct sk_buff *skb)
1055 : : {
1056 : : struct in_pktinfo *pktinfo = PKTINFO_SKB_CB(skb);
1057 [ + - ][ - + ]: 39 : bool prepare = (inet_sk(sk)->cmsg_flags & IP_CMSG_PKTINFO) ||
1058 [ # # ]: 39 : ipv6_sk_rxinfo(sk);
1059 : :
1060 [ - + ][ # # ]: 39 : if (prepare && skb_rtable(skb)) {
1061 : 0 : pktinfo->ipi_ifindex = inet_iif(skb);
1062 : 0 : pktinfo->ipi_spec_dst.s_addr = fib_compute_spec_dst(skb);
1063 : : } else {
1064 : 39 : pktinfo->ipi_ifindex = 0;
1065 : 39 : pktinfo->ipi_spec_dst.s_addr = 0;
1066 : : }
1067 : : skb_dst_drop(skb);
1068 : 39 : }
1069 : :
1070 : 0 : int ip_setsockopt(struct sock *sk, int level,
1071 : : int optname, char __user *optval, unsigned int optlen)
1072 : : {
1073 : : int err;
1074 : :
1075 [ + + ]: 3 : if (level != SOL_IP)
1076 : : return -ENOPROTOOPT;
1077 : :
1078 : 1 : 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 [ + - ]: 1 : if (err == -ENOPROTOOPT && optname != IP_HDRINCL &&
1082 [ + - ]: 1 : optname != IP_IPSEC_POLICY &&
1083 : : optname != IP_XFRM_POLICY &&
1084 : : !ip_mroute_opt(optname)) {
1085 : : lock_sock(sk);
1086 : 1 : err = nf_setsockopt(sk, PF_INET, optname, optval, optlen);
1087 : 1 : release_sock(sk);
1088 : : }
1089 : : #endif
1090 : 1 : return err;
1091 : : }
1092 : : EXPORT_SYMBOL(ip_setsockopt);
1093 : :
1094 : : #ifdef CONFIG_COMPAT
1095 : : int compat_ip_setsockopt(struct sock *sk, int level, int optname,
1096 : : char __user *optval, unsigned int optlen)
1097 : : {
1098 : : int err;
1099 : :
1100 : : if (level != SOL_IP)
1101 : : return -ENOPROTOOPT;
1102 : :
1103 : : if (optname >= MCAST_JOIN_GROUP && optname <= MCAST_MSFILTER)
1104 : : return compat_mc_setsockopt(sk, level, optname, optval, optlen,
1105 : : ip_setsockopt);
1106 : :
1107 : : 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 &&
1111 : : optname != IP_IPSEC_POLICY &&
1112 : : optname != IP_XFRM_POLICY &&
1113 : : !ip_mroute_opt(optname)) {
1114 : : lock_sock(sk);
1115 : : err = compat_nf_setsockopt(sk, PF_INET, optname,
1116 : : optval, optlen);
1117 : : release_sock(sk);
1118 : : }
1119 : : #endif
1120 : : return err;
1121 : : }
1122 : : EXPORT_SYMBOL(compat_ip_setsockopt);
1123 : : #endif
1124 : :
1125 : : /*
1126 : : * Get the options. Note for future reference. The GET of IP options gets
1127 : : * the _received_ ones. The set sets the _sent_ ones.
1128 : : */
1129 : :
1130 : 0 : static int do_ip_getsockopt(struct sock *sk, int level, int optname,
1131 : : char __user *optval, int __user *optlen, unsigned int flags)
1132 : : {
1133 : : struct inet_sock *inet = inet_sk(sk);
1134 : : int val;
1135 : : int len;
1136 : :
1137 [ + + ]: 4 : if (level != SOL_IP)
1138 : : return -EOPNOTSUPP;
1139 : :
1140 : : if (ip_mroute_opt(optname))
1141 : : return ip_mroute_getsockopt(sk, optname, optval, optlen);
1142 : :
1143 [ + - ]: 1 : if (get_user(len, optlen))
1144 : : return -EFAULT;
1145 [ + - ]: 1 : if (len < 0)
1146 : : return -EINVAL;
1147 : :
1148 : : lock_sock(sk);
1149 : :
1150 [ - - - - : 1 : switch (optname) {
- - - - -
- - - - -
- - - - -
- - - - -
- - + ]
1151 : : case IP_OPTIONS:
1152 : : {
1153 : : unsigned char optbuf[sizeof(struct ip_options)+40];
1154 : : struct ip_options *opt = (struct ip_options *)optbuf;
1155 : : struct ip_options_rcu *inet_opt;
1156 : :
1157 : 0 : inet_opt = rcu_dereference_protected(inet->inet_opt,
1158 : : sock_owned_by_user(sk));
1159 : 0 : opt->optlen = 0;
1160 [ # # ]: 0 : if (inet_opt)
1161 : 0 : memcpy(optbuf, &inet_opt->opt,
1162 : : sizeof(struct ip_options) +
1163 : 0 : inet_opt->opt.optlen);
1164 : 0 : release_sock(sk);
1165 : :
1166 [ # # ]: 0 : if (opt->optlen == 0)
1167 : 0 : return put_user(0, optlen);
1168 : :
1169 : 0 : ip_options_undo(opt);
1170 : :
1171 : 0 : len = min_t(unsigned int, len, opt->optlen);
1172 [ # # ]: 0 : if (put_user(len, optlen))
1173 : : return -EFAULT;
1174 [ # # ]: 0 : if (copy_to_user(optval, opt->__data, len))
1175 : : return -EFAULT;
1176 : 0 : return 0;
1177 : : }
1178 : : case IP_PKTINFO:
1179 : 0 : val = (inet->cmsg_flags & IP_CMSG_PKTINFO) != 0;
1180 : 0 : break;
1181 : : case IP_RECVTTL:
1182 : 0 : val = (inet->cmsg_flags & IP_CMSG_TTL) != 0;
1183 : 0 : break;
1184 : : case IP_RECVTOS:
1185 : 0 : val = (inet->cmsg_flags & IP_CMSG_TOS) != 0;
1186 : 0 : break;
1187 : : case IP_RECVOPTS:
1188 : 0 : val = (inet->cmsg_flags & IP_CMSG_RECVOPTS) != 0;
1189 : 0 : break;
1190 : : case IP_RETOPTS:
1191 : 0 : val = (inet->cmsg_flags & IP_CMSG_RETOPTS) != 0;
1192 : 0 : break;
1193 : : case IP_PASSSEC:
1194 : 0 : val = (inet->cmsg_flags & IP_CMSG_PASSSEC) != 0;
1195 : 0 : break;
1196 : : case IP_RECVORIGDSTADDR:
1197 : 0 : val = (inet->cmsg_flags & IP_CMSG_ORIGDSTADDR) != 0;
1198 : 0 : break;
1199 : : case IP_TOS:
1200 : 0 : val = inet->tos;
1201 : 0 : break;
1202 : : case IP_TTL:
1203 : 0 : val = (inet->uc_ttl == -1 ?
1204 [ # # ]: 0 : sysctl_ip_default_ttl :
1205 : : inet->uc_ttl);
1206 : 0 : break;
1207 : : case IP_HDRINCL:
1208 : 0 : val = inet->hdrincl;
1209 : 0 : break;
1210 : : case IP_NODEFRAG:
1211 : 0 : val = inet->nodefrag;
1212 : 0 : break;
1213 : : case IP_MTU_DISCOVER:
1214 : 0 : val = inet->pmtudisc;
1215 : 0 : break;
1216 : : case IP_MTU:
1217 : : {
1218 : : struct dst_entry *dst;
1219 : 0 : val = 0;
1220 : : dst = sk_dst_get(sk);
1221 [ # # ]: 0 : if (dst) {
1222 : 0 : val = dst_mtu(dst);
1223 : 0 : dst_release(dst);
1224 : : }
1225 [ # # ]: 0 : if (!val) {
1226 : 0 : release_sock(sk);
1227 : 0 : return -ENOTCONN;
1228 : : }
1229 : : break;
1230 : : }
1231 : : case IP_RECVERR:
1232 : 0 : val = inet->recverr;
1233 : 0 : break;
1234 : : case IP_MULTICAST_TTL:
1235 : 0 : val = inet->mc_ttl;
1236 : 0 : break;
1237 : : case IP_MULTICAST_LOOP:
1238 : 0 : val = inet->mc_loop;
1239 : 0 : break;
1240 : : case IP_UNICAST_IF:
1241 [ # # ]: 0 : val = (__force int)htonl((__u32) inet->uc_index);
1242 : 0 : break;
1243 : : case IP_MULTICAST_IF:
1244 : : {
1245 : : struct in_addr addr;
1246 : 0 : len = min_t(unsigned int, len, sizeof(struct in_addr));
1247 : 0 : addr.s_addr = inet->mc_addr;
1248 : 0 : release_sock(sk);
1249 : :
1250 [ # # ]: 0 : if (put_user(len, optlen))
1251 : : return -EFAULT;
1252 [ # # ]: 0 : if (copy_to_user(optval, &addr, len))
1253 : : return -EFAULT;
1254 : 0 : return 0;
1255 : : }
1256 : : case IP_MSFILTER:
1257 : : {
1258 : : struct ip_msfilter msf;
1259 : : int err;
1260 : :
1261 [ # # ]: 0 : if (len < IP_MSFILTER_SIZE(0)) {
1262 : 0 : release_sock(sk);
1263 : 0 : return -EINVAL;
1264 : : }
1265 [ # # ]: 0 : if (copy_from_user(&msf, optval, IP_MSFILTER_SIZE(0))) {
1266 : 0 : release_sock(sk);
1267 : 0 : return -EFAULT;
1268 : : }
1269 : 0 : err = ip_mc_msfget(sk, &msf,
1270 : : (struct ip_msfilter __user *)optval, optlen);
1271 : 0 : release_sock(sk);
1272 : 0 : return err;
1273 : : }
1274 : : case MCAST_MSFILTER:
1275 : : {
1276 : : struct group_filter gsf;
1277 : : int err;
1278 : :
1279 [ # # ]: 0 : if (len < GROUP_FILTER_SIZE(0)) {
1280 : 0 : release_sock(sk);
1281 : 0 : return -EINVAL;
1282 : : }
1283 [ # # ]: 0 : if (copy_from_user(&gsf, optval, GROUP_FILTER_SIZE(0))) {
1284 : 0 : release_sock(sk);
1285 : 0 : return -EFAULT;
1286 : : }
1287 : 0 : err = ip_mc_gsfget(sk, &gsf,
1288 : : (struct group_filter __user *)optval,
1289 : : optlen);
1290 : 0 : release_sock(sk);
1291 : 0 : return err;
1292 : : }
1293 : : case IP_MULTICAST_ALL:
1294 : 0 : val = inet->mc_all;
1295 : 0 : break;
1296 : : case IP_PKTOPTIONS:
1297 : : {
1298 : : struct msghdr msg;
1299 : :
1300 : 0 : release_sock(sk);
1301 : :
1302 [ # # ]: 0 : if (sk->sk_type != SOCK_STREAM)
1303 : : return -ENOPROTOOPT;
1304 : :
1305 : 0 : msg.msg_control = optval;
1306 : 0 : msg.msg_controllen = len;
1307 : 0 : msg.msg_flags = flags;
1308 : :
1309 [ # # ]: 0 : if (inet->cmsg_flags & IP_CMSG_PKTINFO) {
1310 : : struct in_pktinfo info;
1311 : :
1312 : 0 : info.ipi_addr.s_addr = inet->inet_rcv_saddr;
1313 : 0 : info.ipi_spec_dst.s_addr = inet->inet_rcv_saddr;
1314 : 0 : info.ipi_ifindex = inet->mc_index;
1315 : 0 : put_cmsg(&msg, SOL_IP, IP_PKTINFO, sizeof(info), &info);
1316 : : }
1317 [ # # ]: 0 : if (inet->cmsg_flags & IP_CMSG_TTL) {
1318 : 0 : int hlim = inet->mc_ttl;
1319 : 0 : put_cmsg(&msg, SOL_IP, IP_TTL, sizeof(hlim), &hlim);
1320 : : }
1321 [ # # ]: 0 : if (inet->cmsg_flags & IP_CMSG_TOS) {
1322 : 0 : int tos = inet->rcv_tos;
1323 : 0 : put_cmsg(&msg, SOL_IP, IP_TOS, sizeof(tos), &tos);
1324 : : }
1325 : 0 : len -= msg.msg_controllen;
1326 : 0 : return put_user(len, optlen);
1327 : : }
1328 : : case IP_FREEBIND:
1329 : 0 : val = inet->freebind;
1330 : 0 : break;
1331 : : case IP_TRANSPARENT:
1332 : 0 : val = inet->transparent;
1333 : 0 : break;
1334 : : case IP_MINTTL:
1335 : 0 : val = inet->min_ttl;
1336 : 0 : break;
1337 : : default:
1338 : 1 : release_sock(sk);
1339 : 1 : return -ENOPROTOOPT;
1340 : : }
1341 : 0 : release_sock(sk);
1342 : :
1343 [ # # ][ # # ]: 0 : if (len < sizeof(int) && len > 0 && val >= 0 && val <= 255) {
1344 : 0 : unsigned char ucval = (unsigned char)val;
1345 : : len = 1;
1346 [ # # ]: 0 : if (put_user(len, optlen))
1347 : 0 : return -EFAULT;
1348 [ # # ]: 0 : if (copy_to_user(optval, &ucval, 1))
1349 : : return -EFAULT;
1350 : : } else {
1351 : 0 : len = min_t(unsigned int, sizeof(int), len);
1352 [ # # ]: 0 : if (put_user(len, optlen))
1353 : : return -EFAULT;
1354 [ # # ]: 0 : if (copy_to_user(optval, &val, len))
1355 : : return -EFAULT;
1356 : : }
1357 : : return 0;
1358 : : }
1359 : :
1360 : 0 : int ip_getsockopt(struct sock *sk, int level,
1361 : : int optname, char __user *optval, int __user *optlen)
1362 : : {
1363 : : int err;
1364 : :
1365 : 4 : err = do_ip_getsockopt(sk, level, optname, optval, optlen, 0);
1366 : : #ifdef CONFIG_NETFILTER
1367 : : /* we need to exclude all possible ENOPROTOOPTs except default case */
1368 [ + + ]: 4 : if (err == -ENOPROTOOPT && optname != IP_PKTOPTIONS &&
1369 : : !ip_mroute_opt(optname)) {
1370 : : int len;
1371 : :
1372 [ + - ]: 1 : if (get_user(len, optlen))
1373 : : return -EFAULT;
1374 : :
1375 : : lock_sock(sk);
1376 : 1 : err = nf_getsockopt(sk, PF_INET, optname, optval,
1377 : : &len);
1378 : 1 : release_sock(sk);
1379 [ - + ]: 1 : if (err >= 0)
1380 : 0 : err = put_user(len, optlen);
1381 : 1 : return err;
1382 : : }
1383 : : #endif
1384 : : return err;
1385 : : }
1386 : : EXPORT_SYMBOL(ip_getsockopt);
1387 : :
1388 : : #ifdef CONFIG_COMPAT
1389 : : int compat_ip_getsockopt(struct sock *sk, int level, int optname,
1390 : : char __user *optval, int __user *optlen)
1391 : : {
1392 : : int err;
1393 : :
1394 : : if (optname == MCAST_MSFILTER)
1395 : : return compat_mc_getsockopt(sk, level, optname, optval, optlen,
1396 : : ip_getsockopt);
1397 : :
1398 : : err = do_ip_getsockopt(sk, level, optname, optval, optlen,
1399 : : MSG_CMSG_COMPAT);
1400 : :
1401 : : #ifdef CONFIG_NETFILTER
1402 : : /* we need to exclude all possible ENOPROTOOPTs except default case */
1403 : : if (err == -ENOPROTOOPT && optname != IP_PKTOPTIONS &&
1404 : : !ip_mroute_opt(optname)) {
1405 : : int len;
1406 : :
1407 : : if (get_user(len, optlen))
1408 : : return -EFAULT;
1409 : :
1410 : : lock_sock(sk);
1411 : : err = compat_nf_getsockopt(sk, PF_INET, optname, optval, &len);
1412 : : release_sock(sk);
1413 : : if (err >= 0)
1414 : : err = put_user(len, optlen);
1415 : : return err;
1416 : : }
1417 : : #endif
1418 : : return err;
1419 : : }
1420 : : EXPORT_SYMBOL(compat_ip_getsockopt);
1421 : : #endif
|