raw.c 25 KB

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