raw.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898
  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 <linux/types.h>
  43. #include <asm/atomic.h>
  44. #include <asm/byteorder.h>
  45. #include <asm/current.h>
  46. #include <asm/uaccess.h>
  47. #include <asm/ioctls.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. nf_reset(skb);
  232. skb_push(skb, skb->data - skb->nh.raw);
  233. raw_rcv_skb(sk, skb);
  234. return 0;
  235. }
  236. static int raw_send_hdrinc(struct sock *sk, void *from, size_t length,
  237. struct rtable *rt,
  238. unsigned int flags)
  239. {
  240. struct inet_sock *inet = inet_sk(sk);
  241. int hh_len;
  242. struct iphdr *iph;
  243. struct sk_buff *skb;
  244. int err;
  245. if (length > rt->u.dst.dev->mtu) {
  246. ip_local_error(sk, EMSGSIZE, rt->rt_dst, inet->dport,
  247. rt->u.dst.dev->mtu);
  248. return -EMSGSIZE;
  249. }
  250. if (flags&MSG_PROBE)
  251. goto out;
  252. hh_len = LL_RESERVED_SPACE(rt->u.dst.dev);
  253. skb = sock_alloc_send_skb(sk, length+hh_len+15,
  254. flags&MSG_DONTWAIT, &err);
  255. if (skb == NULL)
  256. goto error;
  257. skb_reserve(skb, hh_len);
  258. skb->priority = sk->sk_priority;
  259. skb->dst = dst_clone(&rt->u.dst);
  260. skb->nh.iph = iph = (struct iphdr *)skb_put(skb, length);
  261. skb->ip_summed = CHECKSUM_NONE;
  262. skb->h.raw = skb->nh.raw;
  263. err = memcpy_fromiovecend((void *)iph, from, 0, length);
  264. if (err)
  265. goto error_fault;
  266. /* We don't modify invalid header */
  267. if (length >= sizeof(*iph) && iph->ihl * 4U <= length) {
  268. if (!iph->saddr)
  269. iph->saddr = rt->rt_src;
  270. iph->check = 0;
  271. iph->tot_len = htons(length);
  272. if (!iph->id)
  273. ip_select_ident(iph, &rt->u.dst, NULL);
  274. iph->check = ip_fast_csum((unsigned char *)iph, iph->ihl);
  275. }
  276. err = NF_HOOK(PF_INET, NF_IP_LOCAL_OUT, skb, NULL, rt->u.dst.dev,
  277. dst_output);
  278. if (err > 0)
  279. err = inet->recverr ? net_xmit_errno(err) : 0;
  280. if (err)
  281. goto error;
  282. out:
  283. return 0;
  284. error_fault:
  285. err = -EFAULT;
  286. kfree_skb(skb);
  287. error:
  288. IP_INC_STATS(IPSTATS_MIB_OUTDISCARDS);
  289. return err;
  290. }
  291. static void raw_probe_proto_opt(struct flowi *fl, struct msghdr *msg)
  292. {
  293. struct iovec *iov;
  294. u8 __user *type = NULL;
  295. u8 __user *code = NULL;
  296. int probed = 0;
  297. unsigned int i;
  298. if (!msg->msg_iov)
  299. return;
  300. for (i = 0; i < msg->msg_iovlen; i++) {
  301. iov = &msg->msg_iov[i];
  302. if (!iov)
  303. continue;
  304. switch (fl->proto) {
  305. case IPPROTO_ICMP:
  306. /* check if one-byte field is readable or not. */
  307. if (iov->iov_base && iov->iov_len < 1)
  308. break;
  309. if (!type) {
  310. type = iov->iov_base;
  311. /* check if code field is readable or not. */
  312. if (iov->iov_len > 1)
  313. code = type + 1;
  314. } else if (!code)
  315. code = iov->iov_base;
  316. if (type && code) {
  317. get_user(fl->fl_icmp_type, type);
  318. get_user(fl->fl_icmp_code, code);
  319. probed = 1;
  320. }
  321. break;
  322. default:
  323. probed = 1;
  324. break;
  325. }
  326. if (probed)
  327. break;
  328. }
  329. }
  330. static int raw_sendmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
  331. size_t len)
  332. {
  333. struct inet_sock *inet = inet_sk(sk);
  334. struct ipcm_cookie ipc;
  335. struct rtable *rt = NULL;
  336. int free = 0;
  337. u32 daddr;
  338. u32 saddr;
  339. u8 tos;
  340. int err;
  341. err = -EMSGSIZE;
  342. if (len > 0xFFFF)
  343. goto out;
  344. /*
  345. * Check the flags.
  346. */
  347. err = -EOPNOTSUPP;
  348. if (msg->msg_flags & MSG_OOB) /* Mirror BSD error message */
  349. goto out; /* compatibility */
  350. /*
  351. * Get and verify the address.
  352. */
  353. if (msg->msg_namelen) {
  354. struct sockaddr_in *usin = (struct sockaddr_in*)msg->msg_name;
  355. err = -EINVAL;
  356. if (msg->msg_namelen < sizeof(*usin))
  357. goto out;
  358. if (usin->sin_family != AF_INET) {
  359. static int complained;
  360. if (!complained++)
  361. printk(KERN_INFO "%s forgot to set AF_INET in "
  362. "raw sendmsg. Fix it!\n",
  363. current->comm);
  364. err = -EAFNOSUPPORT;
  365. if (usin->sin_family)
  366. goto out;
  367. }
  368. daddr = usin->sin_addr.s_addr;
  369. /* ANK: I did not forget to get protocol from port field.
  370. * I just do not know, who uses this weirdness.
  371. * IP_HDRINCL is much more convenient.
  372. */
  373. } else {
  374. err = -EDESTADDRREQ;
  375. if (sk->sk_state != TCP_ESTABLISHED)
  376. goto out;
  377. daddr = inet->daddr;
  378. }
  379. ipc.addr = inet->saddr;
  380. ipc.opt = NULL;
  381. ipc.oif = sk->sk_bound_dev_if;
  382. if (msg->msg_controllen) {
  383. err = ip_cmsg_send(msg, &ipc);
  384. if (err)
  385. goto out;
  386. if (ipc.opt)
  387. free = 1;
  388. }
  389. saddr = ipc.addr;
  390. ipc.addr = daddr;
  391. if (!ipc.opt)
  392. ipc.opt = inet->opt;
  393. if (ipc.opt) {
  394. err = -EINVAL;
  395. /* Linux does not mangle headers on raw sockets,
  396. * so that IP options + IP_HDRINCL is non-sense.
  397. */
  398. if (inet->hdrincl)
  399. goto done;
  400. if (ipc.opt->srr) {
  401. if (!daddr)
  402. goto done;
  403. daddr = ipc.opt->faddr;
  404. }
  405. }
  406. tos = RT_CONN_FLAGS(sk);
  407. if (msg->msg_flags & MSG_DONTROUTE)
  408. tos |= RTO_ONLINK;
  409. if (MULTICAST(daddr)) {
  410. if (!ipc.oif)
  411. ipc.oif = inet->mc_index;
  412. if (!saddr)
  413. saddr = inet->mc_addr;
  414. }
  415. {
  416. struct flowi fl = { .oif = ipc.oif,
  417. .nl_u = { .ip4_u =
  418. { .daddr = daddr,
  419. .saddr = saddr,
  420. .tos = tos } },
  421. .proto = inet->hdrincl ? IPPROTO_RAW :
  422. sk->sk_protocol,
  423. };
  424. if (!inet->hdrincl)
  425. raw_probe_proto_opt(&fl, msg);
  426. err = ip_route_output_flow(&rt, &fl, sk, !(msg->msg_flags&MSG_DONTWAIT));
  427. }
  428. if (err)
  429. goto done;
  430. err = -EACCES;
  431. if (rt->rt_flags & RTCF_BROADCAST && !sock_flag(sk, SOCK_BROADCAST))
  432. goto done;
  433. if (msg->msg_flags & MSG_CONFIRM)
  434. goto do_confirm;
  435. back_from_confirm:
  436. if (inet->hdrincl)
  437. err = raw_send_hdrinc(sk, msg->msg_iov, len,
  438. rt, msg->msg_flags);
  439. else {
  440. if (!ipc.addr)
  441. ipc.addr = rt->rt_dst;
  442. lock_sock(sk);
  443. err = ip_append_data(sk, ip_generic_getfrag, msg->msg_iov, len, 0,
  444. &ipc, rt, msg->msg_flags);
  445. if (err)
  446. ip_flush_pending_frames(sk);
  447. else if (!(msg->msg_flags & MSG_MORE))
  448. err = ip_push_pending_frames(sk);
  449. release_sock(sk);
  450. }
  451. done:
  452. if (free)
  453. kfree(ipc.opt);
  454. ip_rt_put(rt);
  455. out:
  456. if (err < 0)
  457. return err;
  458. return len;
  459. do_confirm:
  460. dst_confirm(&rt->u.dst);
  461. if (!(msg->msg_flags & MSG_PROBE) || len)
  462. goto back_from_confirm;
  463. err = 0;
  464. goto done;
  465. }
  466. static void raw_close(struct sock *sk, long timeout)
  467. {
  468. /*
  469. * Raw sockets may have direct kernel refereneces. Kill them.
  470. */
  471. ip_ra_control(sk, 0, NULL);
  472. sk_common_release(sk);
  473. }
  474. /* This gets rid of all the nasties in af_inet. -DaveM */
  475. static int raw_bind(struct sock *sk, struct sockaddr *uaddr, int addr_len)
  476. {
  477. struct inet_sock *inet = inet_sk(sk);
  478. struct sockaddr_in *addr = (struct sockaddr_in *) uaddr;
  479. int ret = -EINVAL;
  480. int chk_addr_ret;
  481. if (sk->sk_state != TCP_CLOSE || addr_len < sizeof(struct sockaddr_in))
  482. goto out;
  483. chk_addr_ret = inet_addr_type(addr->sin_addr.s_addr);
  484. ret = -EADDRNOTAVAIL;
  485. if (addr->sin_addr.s_addr && chk_addr_ret != RTN_LOCAL &&
  486. chk_addr_ret != RTN_MULTICAST && chk_addr_ret != RTN_BROADCAST)
  487. goto out;
  488. inet->rcv_saddr = inet->saddr = addr->sin_addr.s_addr;
  489. if (chk_addr_ret == RTN_MULTICAST || chk_addr_ret == RTN_BROADCAST)
  490. inet->saddr = 0; /* Use device */
  491. sk_dst_reset(sk);
  492. ret = 0;
  493. out: return ret;
  494. }
  495. /*
  496. * This should be easy, if there is something there
  497. * we return it, otherwise we block.
  498. */
  499. static int raw_recvmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
  500. size_t len, int noblock, int flags, int *addr_len)
  501. {
  502. struct inet_sock *inet = inet_sk(sk);
  503. size_t copied = 0;
  504. int err = -EOPNOTSUPP;
  505. struct sockaddr_in *sin = (struct sockaddr_in *)msg->msg_name;
  506. struct sk_buff *skb;
  507. if (flags & MSG_OOB)
  508. goto out;
  509. if (addr_len)
  510. *addr_len = sizeof(*sin);
  511. if (flags & MSG_ERRQUEUE) {
  512. err = ip_recv_error(sk, msg, len);
  513. goto out;
  514. }
  515. skb = skb_recv_datagram(sk, flags, noblock, &err);
  516. if (!skb)
  517. goto out;
  518. copied = skb->len;
  519. if (len < copied) {
  520. msg->msg_flags |= MSG_TRUNC;
  521. copied = len;
  522. }
  523. err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
  524. if (err)
  525. goto done;
  526. sock_recv_timestamp(msg, sk, skb);
  527. /* Copy the address. */
  528. if (sin) {
  529. sin->sin_family = AF_INET;
  530. sin->sin_addr.s_addr = skb->nh.iph->saddr;
  531. memset(&sin->sin_zero, 0, sizeof(sin->sin_zero));
  532. }
  533. if (inet->cmsg_flags)
  534. ip_cmsg_recv(msg, skb);
  535. if (flags & MSG_TRUNC)
  536. copied = skb->len;
  537. done:
  538. skb_free_datagram(sk, skb);
  539. out:
  540. if (err)
  541. return err;
  542. return copied;
  543. }
  544. static int raw_init(struct sock *sk)
  545. {
  546. struct raw_sock *rp = raw_sk(sk);
  547. if (inet_sk(sk)->num == IPPROTO_ICMP)
  548. memset(&rp->filter, 0, sizeof(rp->filter));
  549. return 0;
  550. }
  551. static int raw_seticmpfilter(struct sock *sk, char __user *optval, int optlen)
  552. {
  553. if (optlen > sizeof(struct icmp_filter))
  554. optlen = sizeof(struct icmp_filter);
  555. if (copy_from_user(&raw_sk(sk)->filter, optval, optlen))
  556. return -EFAULT;
  557. return 0;
  558. }
  559. static int raw_geticmpfilter(struct sock *sk, char __user *optval, int __user *optlen)
  560. {
  561. int len, ret = -EFAULT;
  562. if (get_user(len, optlen))
  563. goto out;
  564. ret = -EINVAL;
  565. if (len < 0)
  566. goto out;
  567. if (len > sizeof(struct icmp_filter))
  568. len = sizeof(struct icmp_filter);
  569. ret = -EFAULT;
  570. if (put_user(len, optlen) ||
  571. copy_to_user(optval, &raw_sk(sk)->filter, len))
  572. goto out;
  573. ret = 0;
  574. out: return ret;
  575. }
  576. static int raw_setsockopt(struct sock *sk, int level, int optname,
  577. char __user *optval, int optlen)
  578. {
  579. if (level != SOL_RAW)
  580. return ip_setsockopt(sk, level, optname, optval, optlen);
  581. if (optname == ICMP_FILTER) {
  582. if (inet_sk(sk)->num != IPPROTO_ICMP)
  583. return -EOPNOTSUPP;
  584. else
  585. return raw_seticmpfilter(sk, optval, optlen);
  586. }
  587. return -ENOPROTOOPT;
  588. }
  589. static int raw_getsockopt(struct sock *sk, int level, int optname,
  590. char __user *optval, int __user *optlen)
  591. {
  592. if (level != SOL_RAW)
  593. return ip_getsockopt(sk, level, optname, optval, optlen);
  594. if (optname == ICMP_FILTER) {
  595. if (inet_sk(sk)->num != IPPROTO_ICMP)
  596. return -EOPNOTSUPP;
  597. else
  598. return raw_geticmpfilter(sk, optval, optlen);
  599. }
  600. return -ENOPROTOOPT;
  601. }
  602. static int raw_ioctl(struct sock *sk, int cmd, unsigned long arg)
  603. {
  604. switch (cmd) {
  605. case SIOCOUTQ: {
  606. int amount = atomic_read(&sk->sk_wmem_alloc);
  607. return put_user(amount, (int __user *)arg);
  608. }
  609. case SIOCINQ: {
  610. struct sk_buff *skb;
  611. int amount = 0;
  612. spin_lock_bh(&sk->sk_receive_queue.lock);
  613. skb = skb_peek(&sk->sk_receive_queue);
  614. if (skb != NULL)
  615. amount = skb->len;
  616. spin_unlock_bh(&sk->sk_receive_queue.lock);
  617. return put_user(amount, (int __user *)arg);
  618. }
  619. default:
  620. #ifdef CONFIG_IP_MROUTE
  621. return ipmr_ioctl(sk, cmd, (void __user *)arg);
  622. #else
  623. return -ENOIOCTLCMD;
  624. #endif
  625. }
  626. }
  627. struct proto raw_prot = {
  628. .name = "RAW",
  629. .owner = THIS_MODULE,
  630. .close = raw_close,
  631. .connect = ip4_datagram_connect,
  632. .disconnect = udp_disconnect,
  633. .ioctl = raw_ioctl,
  634. .init = raw_init,
  635. .setsockopt = raw_setsockopt,
  636. .getsockopt = raw_getsockopt,
  637. .sendmsg = raw_sendmsg,
  638. .recvmsg = raw_recvmsg,
  639. .bind = raw_bind,
  640. .backlog_rcv = raw_rcv_skb,
  641. .hash = raw_v4_hash,
  642. .unhash = raw_v4_unhash,
  643. .obj_size = sizeof(struct raw_sock),
  644. };
  645. #ifdef CONFIG_PROC_FS
  646. struct raw_iter_state {
  647. int bucket;
  648. };
  649. #define raw_seq_private(seq) ((struct raw_iter_state *)(seq)->private)
  650. static struct sock *raw_get_first(struct seq_file *seq)
  651. {
  652. struct sock *sk;
  653. struct raw_iter_state* state = raw_seq_private(seq);
  654. for (state->bucket = 0; state->bucket < RAWV4_HTABLE_SIZE; ++state->bucket) {
  655. struct hlist_node *node;
  656. sk_for_each(sk, node, &raw_v4_htable[state->bucket])
  657. if (sk->sk_family == PF_INET)
  658. goto found;
  659. }
  660. sk = NULL;
  661. found:
  662. return sk;
  663. }
  664. static struct sock *raw_get_next(struct seq_file *seq, struct sock *sk)
  665. {
  666. struct raw_iter_state* state = raw_seq_private(seq);
  667. do {
  668. sk = sk_next(sk);
  669. try_again:
  670. ;
  671. } while (sk && sk->sk_family != PF_INET);
  672. if (!sk && ++state->bucket < RAWV4_HTABLE_SIZE) {
  673. sk = sk_head(&raw_v4_htable[state->bucket]);
  674. goto try_again;
  675. }
  676. return sk;
  677. }
  678. static struct sock *raw_get_idx(struct seq_file *seq, loff_t pos)
  679. {
  680. struct sock *sk = raw_get_first(seq);
  681. if (sk)
  682. while (pos && (sk = raw_get_next(seq, sk)) != NULL)
  683. --pos;
  684. return pos ? NULL : sk;
  685. }
  686. static void *raw_seq_start(struct seq_file *seq, loff_t *pos)
  687. {
  688. read_lock(&raw_v4_lock);
  689. return *pos ? raw_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
  690. }
  691. static void *raw_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  692. {
  693. struct sock *sk;
  694. if (v == SEQ_START_TOKEN)
  695. sk = raw_get_first(seq);
  696. else
  697. sk = raw_get_next(seq, v);
  698. ++*pos;
  699. return sk;
  700. }
  701. static void raw_seq_stop(struct seq_file *seq, void *v)
  702. {
  703. read_unlock(&raw_v4_lock);
  704. }
  705. static __inline__ char *get_raw_sock(struct sock *sp, char *tmpbuf, int i)
  706. {
  707. struct inet_sock *inet = inet_sk(sp);
  708. unsigned int dest = inet->daddr,
  709. src = inet->rcv_saddr;
  710. __u16 destp = 0,
  711. srcp = inet->num;
  712. sprintf(tmpbuf, "%4d: %08X:%04X %08X:%04X"
  713. " %02X %08X:%08X %02X:%08lX %08X %5d %8d %lu %d %p",
  714. i, src, srcp, dest, destp, sp->sk_state,
  715. atomic_read(&sp->sk_wmem_alloc),
  716. atomic_read(&sp->sk_rmem_alloc),
  717. 0, 0L, 0, sock_i_uid(sp), 0, sock_i_ino(sp),
  718. atomic_read(&sp->sk_refcnt), sp);
  719. return tmpbuf;
  720. }
  721. static int raw_seq_show(struct seq_file *seq, void *v)
  722. {
  723. char tmpbuf[129];
  724. if (v == SEQ_START_TOKEN)
  725. seq_printf(seq, "%-127s\n",
  726. " sl local_address rem_address st tx_queue "
  727. "rx_queue tr tm->when retrnsmt uid timeout "
  728. "inode");
  729. else {
  730. struct raw_iter_state *state = raw_seq_private(seq);
  731. seq_printf(seq, "%-127s\n",
  732. get_raw_sock(v, tmpbuf, state->bucket));
  733. }
  734. return 0;
  735. }
  736. static struct seq_operations raw_seq_ops = {
  737. .start = raw_seq_start,
  738. .next = raw_seq_next,
  739. .stop = raw_seq_stop,
  740. .show = raw_seq_show,
  741. };
  742. static int raw_seq_open(struct inode *inode, struct file *file)
  743. {
  744. struct seq_file *seq;
  745. int rc = -ENOMEM;
  746. struct raw_iter_state *s = kmalloc(sizeof(*s), GFP_KERNEL);
  747. if (!s)
  748. goto out;
  749. rc = seq_open(file, &raw_seq_ops);
  750. if (rc)
  751. goto out_kfree;
  752. seq = file->private_data;
  753. seq->private = s;
  754. memset(s, 0, sizeof(*s));
  755. out:
  756. return rc;
  757. out_kfree:
  758. kfree(s);
  759. goto out;
  760. }
  761. static struct file_operations raw_seq_fops = {
  762. .owner = THIS_MODULE,
  763. .open = raw_seq_open,
  764. .read = seq_read,
  765. .llseek = seq_lseek,
  766. .release = seq_release_private,
  767. };
  768. int __init raw_proc_init(void)
  769. {
  770. if (!proc_net_fops_create("raw", S_IRUGO, &raw_seq_fops))
  771. return -ENOMEM;
  772. return 0;
  773. }
  774. void __init raw_proc_exit(void)
  775. {
  776. proc_net_remove("raw");
  777. }
  778. #endif /* CONFIG_PROC_FS */