raw.c 23 KB

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