raw.c 25 KB

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