raw.c 20 KB

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