raw.c 26 KB

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