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