raw.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066
  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. * RAW - implementation of IP "raw" sockets.
  7. *
  8. * Authors: Ross Biro
  9. * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
  10. *
  11. * Fixes:
  12. * Alan Cox : verify_area() fixed up
  13. * Alan Cox : ICMP error handling
  14. * Alan Cox : EMSGSIZE if you send too big a packet
  15. * Alan Cox : Now uses generic datagrams and shared
  16. * skbuff library. No more peek crashes,
  17. * no more backlogs
  18. * Alan Cox : Checks sk->broadcast.
  19. * Alan Cox : Uses skb_free_datagram/skb_copy_datagram
  20. * Alan Cox : Raw passes ip options too
  21. * Alan Cox : Setsocketopt added
  22. * Alan Cox : Fixed error return for broadcasts
  23. * Alan Cox : Removed wake_up calls
  24. * Alan Cox : Use ttl/tos
  25. * Alan Cox : Cleaned up old debugging
  26. * Alan Cox : Use new kernel side addresses
  27. * Arnt Gulbrandsen : Fixed MSG_DONTROUTE in raw sockets.
  28. * Alan Cox : BSD style RAW socket demultiplexing.
  29. * Alan Cox : Beginnings of mrouted support.
  30. * Alan Cox : Added IP_HDRINCL option.
  31. * Alan Cox : Skip broadcast check if BSDism set.
  32. * David S. Miller : New socket lookup architecture.
  33. *
  34. * This program is free software; you can redistribute it and/or
  35. * modify it under the terms of the GNU General Public License
  36. * as published by the Free Software Foundation; either version
  37. * 2 of the License, or (at your option) any later version.
  38. */
  39. #include <linux/types.h>
  40. #include <linux/atomic.h>
  41. #include <asm/byteorder.h>
  42. #include <asm/current.h>
  43. #include <asm/uaccess.h>
  44. #include <asm/ioctls.h>
  45. #include <linux/stddef.h>
  46. #include <linux/slab.h>
  47. #include <linux/errno.h>
  48. #include <linux/aio.h>
  49. #include <linux/kernel.h>
  50. #include <linux/export.h>
  51. #include <linux/spinlock.h>
  52. #include <linux/sockios.h>
  53. #include <linux/socket.h>
  54. #include <linux/in.h>
  55. #include <linux/mroute.h>
  56. #include <linux/netdevice.h>
  57. #include <linux/in_route.h>
  58. #include <linux/route.h>
  59. #include <linux/skbuff.h>
  60. #include <net/net_namespace.h>
  61. #include <net/dst.h>
  62. #include <net/sock.h>
  63. #include <linux/ip.h>
  64. #include <linux/net.h>
  65. #include <net/ip.h>
  66. #include <net/icmp.h>
  67. #include <net/udp.h>
  68. #include <net/raw.h>
  69. #include <net/snmp.h>
  70. #include <net/tcp_states.h>
  71. #include <net/inet_common.h>
  72. #include <net/checksum.h>
  73. #include <net/xfrm.h>
  74. #include <linux/rtnetlink.h>
  75. #include <linux/proc_fs.h>
  76. #include <linux/seq_file.h>
  77. #include <linux/netfilter.h>
  78. #include <linux/netfilter_ipv4.h>
  79. #include <linux/compat.h>
  80. static struct raw_hashinfo raw_v4_hashinfo = {
  81. .lock = __RW_LOCK_UNLOCKED(raw_v4_hashinfo.lock),
  82. };
  83. void raw_hash_sk(struct sock *sk)
  84. {
  85. struct raw_hashinfo *h = sk->sk_prot->h.raw_hash;
  86. struct hlist_head *head;
  87. head = &h->ht[inet_sk(sk)->inet_num & (RAW_HTABLE_SIZE - 1)];
  88. write_lock_bh(&h->lock);
  89. sk_add_node(sk, head);
  90. sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1);
  91. write_unlock_bh(&h->lock);
  92. }
  93. EXPORT_SYMBOL_GPL(raw_hash_sk);
  94. void raw_unhash_sk(struct sock *sk)
  95. {
  96. struct raw_hashinfo *h = sk->sk_prot->h.raw_hash;
  97. write_lock_bh(&h->lock);
  98. if (sk_del_node_init(sk))
  99. sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
  100. write_unlock_bh(&h->lock);
  101. }
  102. EXPORT_SYMBOL_GPL(raw_unhash_sk);
  103. static struct sock *__raw_v4_lookup(struct net *net, struct sock *sk,
  104. unsigned short num, __be32 raddr, __be32 laddr, int dif)
  105. {
  106. struct hlist_node *node;
  107. sk_for_each_from(sk, node) {
  108. struct inet_sock *inet = inet_sk(sk);
  109. if (net_eq(sock_net(sk), net) && inet->inet_num == num &&
  110. !(inet->inet_daddr && inet->inet_daddr != raddr) &&
  111. !(inet->inet_rcv_saddr && inet->inet_rcv_saddr != laddr) &&
  112. !(sk->sk_bound_dev_if && sk->sk_bound_dev_if != dif))
  113. goto found; /* gotcha */
  114. }
  115. sk = NULL;
  116. found:
  117. return sk;
  118. }
  119. /*
  120. * 0 - deliver
  121. * 1 - block
  122. */
  123. static __inline__ int icmp_filter(struct sock *sk, struct sk_buff *skb)
  124. {
  125. int type;
  126. if (!pskb_may_pull(skb, sizeof(struct icmphdr)))
  127. return 1;
  128. type = icmp_hdr(skb)->type;
  129. if (type < 32) {
  130. __u32 data = raw_sk(sk)->filter.data;
  131. return ((1 << type) & data) != 0;
  132. }
  133. /* Do not block unknown ICMP types */
  134. return 0;
  135. }
  136. /* IP input processing comes here for RAW socket delivery.
  137. * Caller owns SKB, so we must make clones.
  138. *
  139. * RFC 1122: SHOULD pass TOS value up to the transport layer.
  140. * -> It does. And not only TOS, but all IP header.
  141. */
  142. static int raw_v4_input(struct sk_buff *skb, const struct iphdr *iph, int hash)
  143. {
  144. struct sock *sk;
  145. struct hlist_head *head;
  146. int delivered = 0;
  147. struct net *net;
  148. read_lock(&raw_v4_hashinfo.lock);
  149. head = &raw_v4_hashinfo.ht[hash];
  150. if (hlist_empty(head))
  151. goto out;
  152. net = dev_net(skb->dev);
  153. sk = __raw_v4_lookup(net, __sk_head(head), iph->protocol,
  154. iph->saddr, iph->daddr,
  155. skb->dev->ifindex);
  156. while (sk) {
  157. delivered = 1;
  158. if (iph->protocol != IPPROTO_ICMP || !icmp_filter(sk, skb)) {
  159. struct sk_buff *clone = skb_clone(skb, GFP_ATOMIC);
  160. /* Not releasing hash table! */
  161. if (clone)
  162. raw_rcv(sk, clone);
  163. }
  164. sk = __raw_v4_lookup(net, sk_next(sk), iph->protocol,
  165. iph->saddr, iph->daddr,
  166. skb->dev->ifindex);
  167. }
  168. out:
  169. read_unlock(&raw_v4_hashinfo.lock);
  170. return delivered;
  171. }
  172. int raw_local_deliver(struct sk_buff *skb, int protocol)
  173. {
  174. int hash;
  175. struct sock *raw_sk;
  176. hash = protocol & (RAW_HTABLE_SIZE - 1);
  177. raw_sk = sk_head(&raw_v4_hashinfo.ht[hash]);
  178. /* If there maybe a raw socket we must check - if not we
  179. * don't care less
  180. */
  181. if (raw_sk && !raw_v4_input(skb, ip_hdr(skb), hash))
  182. raw_sk = NULL;
  183. return raw_sk != NULL;
  184. }
  185. static void raw_err(struct sock *sk, struct sk_buff *skb, u32 info)
  186. {
  187. struct inet_sock *inet = inet_sk(sk);
  188. const int type = icmp_hdr(skb)->type;
  189. const int code = icmp_hdr(skb)->code;
  190. int err = 0;
  191. int harderr = 0;
  192. /* Report error on raw socket, if:
  193. 1. User requested ip_recverr.
  194. 2. Socket is connected (otherwise the error indication
  195. is useless without ip_recverr and error is hard.
  196. */
  197. if (!inet->recverr && sk->sk_state != TCP_ESTABLISHED)
  198. return;
  199. switch (type) {
  200. default:
  201. case ICMP_TIME_EXCEEDED:
  202. err = EHOSTUNREACH;
  203. break;
  204. case ICMP_SOURCE_QUENCH:
  205. return;
  206. case ICMP_PARAMETERPROB:
  207. err = EPROTO;
  208. harderr = 1;
  209. break;
  210. case ICMP_DEST_UNREACH:
  211. err = EHOSTUNREACH;
  212. if (code > NR_ICMP_UNREACH)
  213. break;
  214. err = icmp_err_convert[code].errno;
  215. harderr = icmp_err_convert[code].fatal;
  216. if (code == ICMP_FRAG_NEEDED) {
  217. harderr = inet->pmtudisc != IP_PMTUDISC_DONT;
  218. err = EMSGSIZE;
  219. }
  220. }
  221. if (inet->recverr) {
  222. const struct iphdr *iph = (const struct iphdr *)skb->data;
  223. u8 *payload = skb->data + (iph->ihl << 2);
  224. if (inet->hdrincl)
  225. payload = skb->data;
  226. ip_icmp_error(sk, skb, err, 0, info, payload);
  227. }
  228. if (inet->recverr || harderr) {
  229. sk->sk_err = err;
  230. sk->sk_error_report(sk);
  231. }
  232. }
  233. void raw_icmp_error(struct sk_buff *skb, int protocol, u32 info)
  234. {
  235. int hash;
  236. struct sock *raw_sk;
  237. const struct iphdr *iph;
  238. struct net *net;
  239. hash = protocol & (RAW_HTABLE_SIZE - 1);
  240. read_lock(&raw_v4_hashinfo.lock);
  241. raw_sk = sk_head(&raw_v4_hashinfo.ht[hash]);
  242. if (raw_sk != NULL) {
  243. iph = (const struct iphdr *)skb->data;
  244. net = dev_net(skb->dev);
  245. while ((raw_sk = __raw_v4_lookup(net, raw_sk, protocol,
  246. iph->daddr, iph->saddr,
  247. skb->dev->ifindex)) != NULL) {
  248. raw_err(raw_sk, skb, info);
  249. raw_sk = sk_next(raw_sk);
  250. iph = (const struct iphdr *)skb->data;
  251. }
  252. }
  253. read_unlock(&raw_v4_hashinfo.lock);
  254. }
  255. static int raw_rcv_skb(struct sock * sk, struct sk_buff * skb)
  256. {
  257. /* Charge it to the socket. */
  258. if (ip_queue_rcv_skb(sk, skb) < 0) {
  259. kfree_skb(skb);
  260. return NET_RX_DROP;
  261. }
  262. return NET_RX_SUCCESS;
  263. }
  264. int raw_rcv(struct sock *sk, struct sk_buff *skb)
  265. {
  266. if (!xfrm4_policy_check(sk, XFRM_POLICY_IN, skb)) {
  267. atomic_inc(&sk->sk_drops);
  268. kfree_skb(skb);
  269. return NET_RX_DROP;
  270. }
  271. nf_reset(skb);
  272. skb_push(skb, skb->data - skb_network_header(skb));
  273. raw_rcv_skb(sk, skb);
  274. return 0;
  275. }
  276. static int raw_send_hdrinc(struct sock *sk, struct flowi4 *fl4,
  277. void *from, size_t length,
  278. struct rtable **rtp,
  279. unsigned int flags)
  280. {
  281. struct inet_sock *inet = inet_sk(sk);
  282. struct net *net = sock_net(sk);
  283. struct iphdr *iph;
  284. struct sk_buff *skb;
  285. unsigned int iphlen;
  286. int err;
  287. struct rtable *rt = *rtp;
  288. if (length > rt->dst.dev->mtu) {
  289. ip_local_error(sk, EMSGSIZE, fl4->daddr, inet->inet_dport,
  290. rt->dst.dev->mtu);
  291. return -EMSGSIZE;
  292. }
  293. if (flags&MSG_PROBE)
  294. goto out;
  295. skb = sock_alloc_send_skb(sk,
  296. length + LL_ALLOCATED_SPACE(rt->dst.dev) + 15,
  297. flags & MSG_DONTWAIT, &err);
  298. if (skb == NULL)
  299. goto error;
  300. skb_reserve(skb, LL_RESERVED_SPACE(rt->dst.dev));
  301. skb->priority = sk->sk_priority;
  302. skb->mark = sk->sk_mark;
  303. skb_dst_set(skb, &rt->dst);
  304. *rtp = NULL;
  305. skb_reset_network_header(skb);
  306. iph = ip_hdr(skb);
  307. skb_put(skb, length);
  308. skb->ip_summed = CHECKSUM_NONE;
  309. skb->transport_header = skb->network_header;
  310. err = -EFAULT;
  311. if (memcpy_fromiovecend((void *)iph, from, 0, length))
  312. goto error_free;
  313. iphlen = iph->ihl * 4;
  314. /*
  315. * We don't want to modify the ip header, but we do need to
  316. * be sure that it won't cause problems later along the network
  317. * stack. Specifically we want to make sure that iph->ihl is a
  318. * sane value. If ihl points beyond the length of the buffer passed
  319. * in, reject the frame as invalid
  320. */
  321. err = -EINVAL;
  322. if (iphlen > length)
  323. goto error_free;
  324. if (iphlen >= sizeof(*iph)) {
  325. if (!iph->saddr)
  326. iph->saddr = fl4->saddr;
  327. iph->check = 0;
  328. iph->tot_len = htons(length);
  329. if (!iph->id)
  330. ip_select_ident(iph, &rt->dst, NULL);
  331. iph->check = ip_fast_csum((unsigned char *)iph, iph->ihl);
  332. }
  333. if (iph->protocol == IPPROTO_ICMP)
  334. icmp_out_count(net, ((struct icmphdr *)
  335. skb_transport_header(skb))->type);
  336. err = NF_HOOK(NFPROTO_IPV4, NF_INET_LOCAL_OUT, skb, NULL,
  337. rt->dst.dev, dst_output);
  338. if (err > 0)
  339. err = net_xmit_errno(err);
  340. if (err)
  341. goto error;
  342. out:
  343. return 0;
  344. error_free:
  345. kfree_skb(skb);
  346. error:
  347. IP_INC_STATS(net, IPSTATS_MIB_OUTDISCARDS);
  348. if (err == -ENOBUFS && !inet->recverr)
  349. err = 0;
  350. return err;
  351. }
  352. static int raw_probe_proto_opt(struct flowi4 *fl4, struct msghdr *msg)
  353. {
  354. struct iovec *iov;
  355. u8 __user *type = NULL;
  356. u8 __user *code = NULL;
  357. int probed = 0;
  358. unsigned int i;
  359. if (!msg->msg_iov)
  360. return 0;
  361. for (i = 0; i < msg->msg_iovlen; i++) {
  362. iov = &msg->msg_iov[i];
  363. if (!iov)
  364. continue;
  365. switch (fl4->flowi4_proto) {
  366. case IPPROTO_ICMP:
  367. /* check if one-byte field is readable or not. */
  368. if (iov->iov_base && iov->iov_len < 1)
  369. break;
  370. if (!type) {
  371. type = iov->iov_base;
  372. /* check if code field is readable or not. */
  373. if (iov->iov_len > 1)
  374. code = type + 1;
  375. } else if (!code)
  376. code = iov->iov_base;
  377. if (type && code) {
  378. if (get_user(fl4->fl4_icmp_type, type) ||
  379. get_user(fl4->fl4_icmp_code, code))
  380. return -EFAULT;
  381. probed = 1;
  382. }
  383. break;
  384. default:
  385. probed = 1;
  386. break;
  387. }
  388. if (probed)
  389. break;
  390. }
  391. return 0;
  392. }
  393. static int raw_sendmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
  394. size_t len)
  395. {
  396. struct inet_sock *inet = inet_sk(sk);
  397. struct ipcm_cookie ipc;
  398. struct rtable *rt = NULL;
  399. struct flowi4 fl4;
  400. int free = 0;
  401. __be32 daddr;
  402. __be32 saddr;
  403. u8 tos;
  404. int err;
  405. struct ip_options_data opt_copy;
  406. err = -EMSGSIZE;
  407. if (len > 0xFFFF)
  408. goto out;
  409. /*
  410. * Check the flags.
  411. */
  412. err = -EOPNOTSUPP;
  413. if (msg->msg_flags & MSG_OOB) /* Mirror BSD error message */
  414. goto out; /* compatibility */
  415. /*
  416. * Get and verify the address.
  417. */
  418. if (msg->msg_namelen) {
  419. struct sockaddr_in *usin = (struct sockaddr_in *)msg->msg_name;
  420. err = -EINVAL;
  421. if (msg->msg_namelen < sizeof(*usin))
  422. goto out;
  423. if (usin->sin_family != AF_INET) {
  424. static int complained;
  425. if (!complained++)
  426. printk(KERN_INFO "%s forgot to set AF_INET in "
  427. "raw sendmsg. Fix it!\n",
  428. current->comm);
  429. err = -EAFNOSUPPORT;
  430. if (usin->sin_family)
  431. goto out;
  432. }
  433. daddr = usin->sin_addr.s_addr;
  434. /* ANK: I did not forget to get protocol from port field.
  435. * I just do not know, who uses this weirdness.
  436. * IP_HDRINCL is much more convenient.
  437. */
  438. } else {
  439. err = -EDESTADDRREQ;
  440. if (sk->sk_state != TCP_ESTABLISHED)
  441. goto out;
  442. daddr = inet->inet_daddr;
  443. }
  444. ipc.addr = inet->inet_saddr;
  445. ipc.opt = NULL;
  446. ipc.tx_flags = 0;
  447. ipc.oif = sk->sk_bound_dev_if;
  448. if (msg->msg_controllen) {
  449. err = ip_cmsg_send(sock_net(sk), msg, &ipc);
  450. if (err)
  451. goto out;
  452. if (ipc.opt)
  453. free = 1;
  454. }
  455. saddr = ipc.addr;
  456. ipc.addr = daddr;
  457. if (!ipc.opt) {
  458. struct ip_options_rcu *inet_opt;
  459. rcu_read_lock();
  460. inet_opt = rcu_dereference(inet->inet_opt);
  461. if (inet_opt) {
  462. memcpy(&opt_copy, inet_opt,
  463. sizeof(*inet_opt) + inet_opt->opt.optlen);
  464. ipc.opt = &opt_copy.opt;
  465. }
  466. rcu_read_unlock();
  467. }
  468. if (ipc.opt) {
  469. err = -EINVAL;
  470. /* Linux does not mangle headers on raw sockets,
  471. * so that IP options + IP_HDRINCL is non-sense.
  472. */
  473. if (inet->hdrincl)
  474. goto done;
  475. if (ipc.opt->opt.srr) {
  476. if (!daddr)
  477. goto done;
  478. daddr = ipc.opt->opt.faddr;
  479. }
  480. }
  481. tos = RT_CONN_FLAGS(sk);
  482. if (msg->msg_flags & MSG_DONTROUTE)
  483. tos |= RTO_ONLINK;
  484. if (ipv4_is_multicast(daddr)) {
  485. if (!ipc.oif)
  486. ipc.oif = inet->mc_index;
  487. if (!saddr)
  488. saddr = inet->mc_addr;
  489. }
  490. flowi4_init_output(&fl4, ipc.oif, sk->sk_mark, tos,
  491. RT_SCOPE_UNIVERSE,
  492. inet->hdrincl ? IPPROTO_RAW : sk->sk_protocol,
  493. inet_sk_flowi_flags(sk) | FLOWI_FLAG_CAN_SLEEP,
  494. daddr, saddr, 0, 0);
  495. if (!inet->hdrincl) {
  496. err = raw_probe_proto_opt(&fl4, msg);
  497. if (err)
  498. goto done;
  499. }
  500. security_sk_classify_flow(sk, flowi4_to_flowi(&fl4));
  501. rt = ip_route_output_flow(sock_net(sk), &fl4, sk);
  502. if (IS_ERR(rt)) {
  503. err = PTR_ERR(rt);
  504. rt = NULL;
  505. goto done;
  506. }
  507. err = -EACCES;
  508. if (rt->rt_flags & RTCF_BROADCAST && !sock_flag(sk, SOCK_BROADCAST))
  509. goto done;
  510. if (msg->msg_flags & MSG_CONFIRM)
  511. goto do_confirm;
  512. back_from_confirm:
  513. if (inet->hdrincl)
  514. err = raw_send_hdrinc(sk, &fl4, msg->msg_iov, len,
  515. &rt, msg->msg_flags);
  516. else {
  517. if (!ipc.addr)
  518. ipc.addr = fl4.daddr;
  519. lock_sock(sk);
  520. err = ip_append_data(sk, &fl4, ip_generic_getfrag,
  521. msg->msg_iov, len, 0,
  522. &ipc, &rt, msg->msg_flags);
  523. if (err)
  524. ip_flush_pending_frames(sk);
  525. else if (!(msg->msg_flags & MSG_MORE)) {
  526. err = ip_push_pending_frames(sk, &fl4);
  527. if (err == -ENOBUFS && !inet->recverr)
  528. err = 0;
  529. }
  530. release_sock(sk);
  531. }
  532. done:
  533. if (free)
  534. kfree(ipc.opt);
  535. ip_rt_put(rt);
  536. out:
  537. if (err < 0)
  538. return err;
  539. return len;
  540. do_confirm:
  541. dst_confirm(&rt->dst);
  542. if (!(msg->msg_flags & MSG_PROBE) || len)
  543. goto back_from_confirm;
  544. err = 0;
  545. goto done;
  546. }
  547. static void raw_close(struct sock *sk, long timeout)
  548. {
  549. /*
  550. * Raw sockets may have direct kernel references. Kill them.
  551. */
  552. ip_ra_control(sk, 0, NULL);
  553. sk_common_release(sk);
  554. }
  555. static void raw_destroy(struct sock *sk)
  556. {
  557. lock_sock(sk);
  558. ip_flush_pending_frames(sk);
  559. release_sock(sk);
  560. }
  561. /* This gets rid of all the nasties in af_inet. -DaveM */
  562. static int raw_bind(struct sock *sk, struct sockaddr *uaddr, int addr_len)
  563. {
  564. struct inet_sock *inet = inet_sk(sk);
  565. struct sockaddr_in *addr = (struct sockaddr_in *) uaddr;
  566. int ret = -EINVAL;
  567. int chk_addr_ret;
  568. if (sk->sk_state != TCP_CLOSE || addr_len < sizeof(struct sockaddr_in))
  569. goto out;
  570. chk_addr_ret = inet_addr_type(sock_net(sk), addr->sin_addr.s_addr);
  571. ret = -EADDRNOTAVAIL;
  572. if (addr->sin_addr.s_addr && chk_addr_ret != RTN_LOCAL &&
  573. chk_addr_ret != RTN_MULTICAST && chk_addr_ret != RTN_BROADCAST)
  574. goto out;
  575. inet->inet_rcv_saddr = inet->inet_saddr = addr->sin_addr.s_addr;
  576. if (chk_addr_ret == RTN_MULTICAST || chk_addr_ret == RTN_BROADCAST)
  577. inet->inet_saddr = 0; /* Use device */
  578. sk_dst_reset(sk);
  579. ret = 0;
  580. out: return ret;
  581. }
  582. /*
  583. * This should be easy, if there is something there
  584. * we return it, otherwise we block.
  585. */
  586. static int raw_recvmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
  587. size_t len, int noblock, int flags, int *addr_len)
  588. {
  589. struct inet_sock *inet = inet_sk(sk);
  590. size_t copied = 0;
  591. int err = -EOPNOTSUPP;
  592. struct sockaddr_in *sin = (struct sockaddr_in *)msg->msg_name;
  593. struct sk_buff *skb;
  594. if (flags & MSG_OOB)
  595. goto out;
  596. if (addr_len)
  597. *addr_len = sizeof(*sin);
  598. if (flags & MSG_ERRQUEUE) {
  599. err = ip_recv_error(sk, msg, len);
  600. goto out;
  601. }
  602. skb = skb_recv_datagram(sk, flags, noblock, &err);
  603. if (!skb)
  604. goto out;
  605. copied = skb->len;
  606. if (len < copied) {
  607. msg->msg_flags |= MSG_TRUNC;
  608. copied = len;
  609. }
  610. err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
  611. if (err)
  612. goto done;
  613. sock_recv_ts_and_drops(msg, sk, skb);
  614. /* Copy the address. */
  615. if (sin) {
  616. sin->sin_family = AF_INET;
  617. sin->sin_addr.s_addr = ip_hdr(skb)->saddr;
  618. sin->sin_port = 0;
  619. memset(&sin->sin_zero, 0, sizeof(sin->sin_zero));
  620. }
  621. if (inet->cmsg_flags)
  622. ip_cmsg_recv(msg, skb);
  623. if (flags & MSG_TRUNC)
  624. copied = skb->len;
  625. done:
  626. skb_free_datagram(sk, skb);
  627. out:
  628. if (err)
  629. return err;
  630. return copied;
  631. }
  632. static int raw_init(struct sock *sk)
  633. {
  634. struct raw_sock *rp = raw_sk(sk);
  635. if (inet_sk(sk)->inet_num == IPPROTO_ICMP)
  636. memset(&rp->filter, 0, sizeof(rp->filter));
  637. return 0;
  638. }
  639. static int raw_seticmpfilter(struct sock *sk, char __user *optval, int optlen)
  640. {
  641. if (optlen > sizeof(struct icmp_filter))
  642. optlen = sizeof(struct icmp_filter);
  643. if (copy_from_user(&raw_sk(sk)->filter, optval, optlen))
  644. return -EFAULT;
  645. return 0;
  646. }
  647. static int raw_geticmpfilter(struct sock *sk, char __user *optval, int __user *optlen)
  648. {
  649. int len, ret = -EFAULT;
  650. if (get_user(len, optlen))
  651. goto out;
  652. ret = -EINVAL;
  653. if (len < 0)
  654. goto out;
  655. if (len > sizeof(struct icmp_filter))
  656. len = sizeof(struct icmp_filter);
  657. ret = -EFAULT;
  658. if (put_user(len, optlen) ||
  659. copy_to_user(optval, &raw_sk(sk)->filter, len))
  660. goto out;
  661. ret = 0;
  662. out: return ret;
  663. }
  664. static int do_raw_setsockopt(struct sock *sk, int level, int optname,
  665. char __user *optval, unsigned int optlen)
  666. {
  667. if (optname == ICMP_FILTER) {
  668. if (inet_sk(sk)->inet_num != IPPROTO_ICMP)
  669. return -EOPNOTSUPP;
  670. else
  671. return raw_seticmpfilter(sk, optval, optlen);
  672. }
  673. return -ENOPROTOOPT;
  674. }
  675. static int raw_setsockopt(struct sock *sk, int level, int optname,
  676. char __user *optval, unsigned int optlen)
  677. {
  678. if (level != SOL_RAW)
  679. return ip_setsockopt(sk, level, optname, optval, optlen);
  680. return do_raw_setsockopt(sk, level, optname, optval, optlen);
  681. }
  682. #ifdef CONFIG_COMPAT
  683. static int compat_raw_setsockopt(struct sock *sk, int level, int optname,
  684. char __user *optval, unsigned int optlen)
  685. {
  686. if (level != SOL_RAW)
  687. return compat_ip_setsockopt(sk, level, optname, optval, optlen);
  688. return do_raw_setsockopt(sk, level, optname, optval, optlen);
  689. }
  690. #endif
  691. static int do_raw_getsockopt(struct sock *sk, int level, int optname,
  692. char __user *optval, int __user *optlen)
  693. {
  694. if (optname == ICMP_FILTER) {
  695. if (inet_sk(sk)->inet_num != IPPROTO_ICMP)
  696. return -EOPNOTSUPP;
  697. else
  698. return raw_geticmpfilter(sk, optval, optlen);
  699. }
  700. return -ENOPROTOOPT;
  701. }
  702. static int raw_getsockopt(struct sock *sk, int level, int optname,
  703. char __user *optval, int __user *optlen)
  704. {
  705. if (level != SOL_RAW)
  706. return ip_getsockopt(sk, level, optname, optval, optlen);
  707. return do_raw_getsockopt(sk, level, optname, optval, optlen);
  708. }
  709. #ifdef CONFIG_COMPAT
  710. static int compat_raw_getsockopt(struct sock *sk, int level, int optname,
  711. char __user *optval, int __user *optlen)
  712. {
  713. if (level != SOL_RAW)
  714. return compat_ip_getsockopt(sk, level, optname, optval, optlen);
  715. return do_raw_getsockopt(sk, level, optname, optval, optlen);
  716. }
  717. #endif
  718. static int raw_ioctl(struct sock *sk, int cmd, unsigned long arg)
  719. {
  720. switch (cmd) {
  721. case SIOCOUTQ: {
  722. int amount = sk_wmem_alloc_get(sk);
  723. return put_user(amount, (int __user *)arg);
  724. }
  725. case SIOCINQ: {
  726. struct sk_buff *skb;
  727. int amount = 0;
  728. spin_lock_bh(&sk->sk_receive_queue.lock);
  729. skb = skb_peek(&sk->sk_receive_queue);
  730. if (skb != NULL)
  731. amount = skb->len;
  732. spin_unlock_bh(&sk->sk_receive_queue.lock);
  733. return put_user(amount, (int __user *)arg);
  734. }
  735. default:
  736. #ifdef CONFIG_IP_MROUTE
  737. return ipmr_ioctl(sk, cmd, (void __user *)arg);
  738. #else
  739. return -ENOIOCTLCMD;
  740. #endif
  741. }
  742. }
  743. #ifdef CONFIG_COMPAT
  744. static int compat_raw_ioctl(struct sock *sk, unsigned int cmd, unsigned long arg)
  745. {
  746. switch (cmd) {
  747. case SIOCOUTQ:
  748. case SIOCINQ:
  749. return -ENOIOCTLCMD;
  750. default:
  751. #ifdef CONFIG_IP_MROUTE
  752. return ipmr_compat_ioctl(sk, cmd, compat_ptr(arg));
  753. #else
  754. return -ENOIOCTLCMD;
  755. #endif
  756. }
  757. }
  758. #endif
  759. struct proto raw_prot = {
  760. .name = "RAW",
  761. .owner = THIS_MODULE,
  762. .close = raw_close,
  763. .destroy = raw_destroy,
  764. .connect = ip4_datagram_connect,
  765. .disconnect = udp_disconnect,
  766. .ioctl = raw_ioctl,
  767. .init = raw_init,
  768. .setsockopt = raw_setsockopt,
  769. .getsockopt = raw_getsockopt,
  770. .sendmsg = raw_sendmsg,
  771. .recvmsg = raw_recvmsg,
  772. .bind = raw_bind,
  773. .backlog_rcv = raw_rcv_skb,
  774. .hash = raw_hash_sk,
  775. .unhash = raw_unhash_sk,
  776. .obj_size = sizeof(struct raw_sock),
  777. .h.raw_hash = &raw_v4_hashinfo,
  778. #ifdef CONFIG_COMPAT
  779. .compat_setsockopt = compat_raw_setsockopt,
  780. .compat_getsockopt = compat_raw_getsockopt,
  781. .compat_ioctl = compat_raw_ioctl,
  782. #endif
  783. };
  784. #ifdef CONFIG_PROC_FS
  785. static struct sock *raw_get_first(struct seq_file *seq)
  786. {
  787. struct sock *sk;
  788. struct raw_iter_state *state = raw_seq_private(seq);
  789. for (state->bucket = 0; state->bucket < RAW_HTABLE_SIZE;
  790. ++state->bucket) {
  791. struct hlist_node *node;
  792. sk_for_each(sk, node, &state->h->ht[state->bucket])
  793. if (sock_net(sk) == seq_file_net(seq))
  794. goto found;
  795. }
  796. sk = NULL;
  797. found:
  798. return sk;
  799. }
  800. static struct sock *raw_get_next(struct seq_file *seq, struct sock *sk)
  801. {
  802. struct raw_iter_state *state = raw_seq_private(seq);
  803. do {
  804. sk = sk_next(sk);
  805. try_again:
  806. ;
  807. } while (sk && sock_net(sk) != seq_file_net(seq));
  808. if (!sk && ++state->bucket < RAW_HTABLE_SIZE) {
  809. sk = sk_head(&state->h->ht[state->bucket]);
  810. goto try_again;
  811. }
  812. return sk;
  813. }
  814. static struct sock *raw_get_idx(struct seq_file *seq, loff_t pos)
  815. {
  816. struct sock *sk = raw_get_first(seq);
  817. if (sk)
  818. while (pos && (sk = raw_get_next(seq, sk)) != NULL)
  819. --pos;
  820. return pos ? NULL : sk;
  821. }
  822. void *raw_seq_start(struct seq_file *seq, loff_t *pos)
  823. {
  824. struct raw_iter_state *state = raw_seq_private(seq);
  825. read_lock(&state->h->lock);
  826. return *pos ? raw_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
  827. }
  828. EXPORT_SYMBOL_GPL(raw_seq_start);
  829. void *raw_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  830. {
  831. struct sock *sk;
  832. if (v == SEQ_START_TOKEN)
  833. sk = raw_get_first(seq);
  834. else
  835. sk = raw_get_next(seq, v);
  836. ++*pos;
  837. return sk;
  838. }
  839. EXPORT_SYMBOL_GPL(raw_seq_next);
  840. void raw_seq_stop(struct seq_file *seq, void *v)
  841. {
  842. struct raw_iter_state *state = raw_seq_private(seq);
  843. read_unlock(&state->h->lock);
  844. }
  845. EXPORT_SYMBOL_GPL(raw_seq_stop);
  846. static void raw_sock_seq_show(struct seq_file *seq, struct sock *sp, int i)
  847. {
  848. struct inet_sock *inet = inet_sk(sp);
  849. __be32 dest = inet->inet_daddr,
  850. src = inet->inet_rcv_saddr;
  851. __u16 destp = 0,
  852. srcp = inet->inet_num;
  853. seq_printf(seq, "%4d: %08X:%04X %08X:%04X"
  854. " %02X %08X:%08X %02X:%08lX %08X %5d %8d %lu %d %pK %d\n",
  855. i, src, srcp, dest, destp, sp->sk_state,
  856. sk_wmem_alloc_get(sp),
  857. sk_rmem_alloc_get(sp),
  858. 0, 0L, 0, sock_i_uid(sp), 0, sock_i_ino(sp),
  859. atomic_read(&sp->sk_refcnt), sp, atomic_read(&sp->sk_drops));
  860. }
  861. static int raw_seq_show(struct seq_file *seq, void *v)
  862. {
  863. if (v == SEQ_START_TOKEN)
  864. seq_printf(seq, " sl local_address rem_address st tx_queue "
  865. "rx_queue tr tm->when retrnsmt uid timeout "
  866. "inode ref pointer drops\n");
  867. else
  868. raw_sock_seq_show(seq, v, raw_seq_private(seq)->bucket);
  869. return 0;
  870. }
  871. static const struct seq_operations raw_seq_ops = {
  872. .start = raw_seq_start,
  873. .next = raw_seq_next,
  874. .stop = raw_seq_stop,
  875. .show = raw_seq_show,
  876. };
  877. int raw_seq_open(struct inode *ino, struct file *file,
  878. struct raw_hashinfo *h, const struct seq_operations *ops)
  879. {
  880. int err;
  881. struct raw_iter_state *i;
  882. err = seq_open_net(ino, file, ops, sizeof(struct raw_iter_state));
  883. if (err < 0)
  884. return err;
  885. i = raw_seq_private((struct seq_file *)file->private_data);
  886. i->h = h;
  887. return 0;
  888. }
  889. EXPORT_SYMBOL_GPL(raw_seq_open);
  890. static int raw_v4_seq_open(struct inode *inode, struct file *file)
  891. {
  892. return raw_seq_open(inode, file, &raw_v4_hashinfo, &raw_seq_ops);
  893. }
  894. static const struct file_operations raw_seq_fops = {
  895. .owner = THIS_MODULE,
  896. .open = raw_v4_seq_open,
  897. .read = seq_read,
  898. .llseek = seq_lseek,
  899. .release = seq_release_net,
  900. };
  901. static __net_init int raw_init_net(struct net *net)
  902. {
  903. if (!proc_net_fops_create(net, "raw", S_IRUGO, &raw_seq_fops))
  904. return -ENOMEM;
  905. return 0;
  906. }
  907. static __net_exit void raw_exit_net(struct net *net)
  908. {
  909. proc_net_remove(net, "raw");
  910. }
  911. static __net_initdata struct pernet_operations raw_net_ops = {
  912. .init = raw_init_net,
  913. .exit = raw_exit_net,
  914. };
  915. int __init raw_proc_init(void)
  916. {
  917. return register_pernet_subsys(&raw_net_ops);
  918. }
  919. void __init raw_proc_exit(void)
  920. {
  921. unregister_pernet_subsys(&raw_net_ops);
  922. }
  923. #endif /* CONFIG_PROC_FS */