raw.c 20 KB

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