raw.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236
  1. /*
  2. * RAW sockets for IPv6
  3. * Linux INET6 implementation
  4. *
  5. * Authors:
  6. * Pedro Roque <roque@di.fc.ul.pt>
  7. *
  8. * Adapted from linux/net/ipv4/raw.c
  9. *
  10. * $Id: raw.c,v 1.51 2002/02/01 22:01:04 davem Exp $
  11. *
  12. * Fixes:
  13. * Hideaki YOSHIFUJI : sin6_scope_id support
  14. * YOSHIFUJI,H.@USAGI : raw checksum (RFC2292(bis) compliance)
  15. * Kazunori MIYAZAWA @USAGI: change process style to use ip6_append_data
  16. *
  17. * This program is free software; you can redistribute it and/or
  18. * modify it under the terms of the GNU General Public License
  19. * as published by the Free Software Foundation; either version
  20. * 2 of the License, or (at your option) any later version.
  21. */
  22. #include <linux/errno.h>
  23. #include <linux/types.h>
  24. #include <linux/socket.h>
  25. #include <linux/sockios.h>
  26. #include <linux/sched.h>
  27. #include <linux/net.h>
  28. #include <linux/in6.h>
  29. #include <linux/netdevice.h>
  30. #include <linux/if_arp.h>
  31. #include <linux/icmpv6.h>
  32. #include <linux/netfilter.h>
  33. #include <linux/netfilter_ipv6.h>
  34. #include <linux/skbuff.h>
  35. #include <asm/uaccess.h>
  36. #include <asm/ioctls.h>
  37. #include <net/ip.h>
  38. #include <net/sock.h>
  39. #include <net/snmp.h>
  40. #include <net/ipv6.h>
  41. #include <net/ndisc.h>
  42. #include <net/protocol.h>
  43. #include <net/ip6_route.h>
  44. #include <net/ip6_checksum.h>
  45. #include <net/addrconf.h>
  46. #include <net/transp_v6.h>
  47. #include <net/udp.h>
  48. #include <net/inet_common.h>
  49. #include <net/tcp_states.h>
  50. #include <net/rawv6.h>
  51. #include <net/xfrm.h>
  52. #include <linux/proc_fs.h>
  53. #include <linux/seq_file.h>
  54. struct hlist_head raw_v6_htable[RAWV6_HTABLE_SIZE];
  55. DEFINE_RWLOCK(raw_v6_lock);
  56. static void raw_v6_hash(struct sock *sk)
  57. {
  58. struct hlist_head *list = &raw_v6_htable[inet_sk(sk)->num &
  59. (RAWV6_HTABLE_SIZE - 1)];
  60. write_lock_bh(&raw_v6_lock);
  61. sk_add_node(sk, list);
  62. sock_prot_inc_use(sk->sk_prot);
  63. write_unlock_bh(&raw_v6_lock);
  64. }
  65. static void raw_v6_unhash(struct sock *sk)
  66. {
  67. write_lock_bh(&raw_v6_lock);
  68. if (sk_del_node_init(sk))
  69. sock_prot_dec_use(sk->sk_prot);
  70. write_unlock_bh(&raw_v6_lock);
  71. }
  72. /* Grumble... icmp and ip_input want to get at this... */
  73. struct sock *__raw_v6_lookup(struct sock *sk, unsigned short num,
  74. struct in6_addr *loc_addr, struct in6_addr *rmt_addr,
  75. int dif)
  76. {
  77. struct hlist_node *node;
  78. int is_multicast = ipv6_addr_is_multicast(loc_addr);
  79. sk_for_each_from(sk, node)
  80. if (inet_sk(sk)->num == num) {
  81. struct ipv6_pinfo *np = inet6_sk(sk);
  82. if (!ipv6_addr_any(&np->daddr) &&
  83. !ipv6_addr_equal(&np->daddr, rmt_addr))
  84. continue;
  85. if (sk->sk_bound_dev_if && sk->sk_bound_dev_if != dif)
  86. continue;
  87. if (!ipv6_addr_any(&np->rcv_saddr)) {
  88. if (ipv6_addr_equal(&np->rcv_saddr, loc_addr))
  89. goto found;
  90. if (is_multicast &&
  91. inet6_mc_check(sk, loc_addr, rmt_addr))
  92. goto found;
  93. continue;
  94. }
  95. goto found;
  96. }
  97. sk = NULL;
  98. found:
  99. return sk;
  100. }
  101. /*
  102. * 0 - deliver
  103. * 1 - block
  104. */
  105. static __inline__ int icmpv6_filter(struct sock *sk, struct sk_buff *skb)
  106. {
  107. struct icmp6hdr *icmph;
  108. struct raw6_sock *rp = raw6_sk(sk);
  109. if (pskb_may_pull(skb, sizeof(struct icmp6hdr))) {
  110. __u32 *data = &rp->filter.data[0];
  111. int bit_nr;
  112. icmph = (struct icmp6hdr *) skb->data;
  113. bit_nr = icmph->icmp6_type;
  114. return (data[bit_nr >> 5] & (1 << (bit_nr & 31))) != 0;
  115. }
  116. return 0;
  117. }
  118. /*
  119. * demultiplex raw sockets.
  120. * (should consider queueing the skb in the sock receive_queue
  121. * without calling rawv6.c)
  122. *
  123. * Caller owns SKB so we must make clones.
  124. */
  125. int ipv6_raw_deliver(struct sk_buff *skb, int nexthdr)
  126. {
  127. struct in6_addr *saddr;
  128. struct in6_addr *daddr;
  129. struct sock *sk;
  130. int delivered = 0;
  131. __u8 hash;
  132. saddr = &skb->nh.ipv6h->saddr;
  133. daddr = saddr + 1;
  134. hash = nexthdr & (MAX_INET_PROTOS - 1);
  135. read_lock(&raw_v6_lock);
  136. sk = sk_head(&raw_v6_htable[hash]);
  137. /*
  138. * The first socket found will be delivered after
  139. * delivery to transport protocols.
  140. */
  141. if (sk == NULL)
  142. goto out;
  143. sk = __raw_v6_lookup(sk, nexthdr, daddr, saddr, IP6CB(skb)->iif);
  144. while (sk) {
  145. delivered = 1;
  146. if (nexthdr != IPPROTO_ICMPV6 || !icmpv6_filter(sk, skb)) {
  147. struct sk_buff *clone = skb_clone(skb, GFP_ATOMIC);
  148. /* Not releasing hash table! */
  149. if (clone) {
  150. nf_reset(clone);
  151. rawv6_rcv(sk, clone);
  152. }
  153. }
  154. sk = __raw_v6_lookup(sk_next(sk), nexthdr, daddr, saddr,
  155. IP6CB(skb)->iif);
  156. }
  157. out:
  158. read_unlock(&raw_v6_lock);
  159. return delivered;
  160. }
  161. /* This cleans up af_inet6 a bit. -DaveM */
  162. static int rawv6_bind(struct sock *sk, struct sockaddr *uaddr, int addr_len)
  163. {
  164. struct inet_sock *inet = inet_sk(sk);
  165. struct ipv6_pinfo *np = inet6_sk(sk);
  166. struct sockaddr_in6 *addr = (struct sockaddr_in6 *) uaddr;
  167. __u32 v4addr = 0;
  168. int addr_type;
  169. int err;
  170. if (addr_len < SIN6_LEN_RFC2133)
  171. return -EINVAL;
  172. addr_type = ipv6_addr_type(&addr->sin6_addr);
  173. /* Raw sockets are IPv6 only */
  174. if (addr_type == IPV6_ADDR_MAPPED)
  175. return(-EADDRNOTAVAIL);
  176. lock_sock(sk);
  177. err = -EINVAL;
  178. if (sk->sk_state != TCP_CLOSE)
  179. goto out;
  180. /* Check if the address belongs to the host. */
  181. if (addr_type != IPV6_ADDR_ANY) {
  182. struct net_device *dev = NULL;
  183. if (addr_type & IPV6_ADDR_LINKLOCAL) {
  184. if (addr_len >= sizeof(struct sockaddr_in6) &&
  185. addr->sin6_scope_id) {
  186. /* Override any existing binding, if another
  187. * one is supplied by user.
  188. */
  189. sk->sk_bound_dev_if = addr->sin6_scope_id;
  190. }
  191. /* Binding to link-local address requires an interface */
  192. if (!sk->sk_bound_dev_if)
  193. goto out;
  194. dev = dev_get_by_index(sk->sk_bound_dev_if);
  195. if (!dev) {
  196. err = -ENODEV;
  197. goto out;
  198. }
  199. }
  200. /* ipv4 addr of the socket is invalid. Only the
  201. * unspecified and mapped address have a v4 equivalent.
  202. */
  203. v4addr = LOOPBACK4_IPV6;
  204. if (!(addr_type & IPV6_ADDR_MULTICAST)) {
  205. err = -EADDRNOTAVAIL;
  206. if (!ipv6_chk_addr(&addr->sin6_addr, dev, 0)) {
  207. if (dev)
  208. dev_put(dev);
  209. goto out;
  210. }
  211. }
  212. if (dev)
  213. dev_put(dev);
  214. }
  215. inet->rcv_saddr = inet->saddr = v4addr;
  216. ipv6_addr_copy(&np->rcv_saddr, &addr->sin6_addr);
  217. if (!(addr_type & IPV6_ADDR_MULTICAST))
  218. ipv6_addr_copy(&np->saddr, &addr->sin6_addr);
  219. err = 0;
  220. out:
  221. release_sock(sk);
  222. return err;
  223. }
  224. void rawv6_err(struct sock *sk, struct sk_buff *skb,
  225. struct inet6_skb_parm *opt,
  226. int type, int code, int offset, u32 info)
  227. {
  228. struct inet_sock *inet = inet_sk(sk);
  229. struct ipv6_pinfo *np = inet6_sk(sk);
  230. int err;
  231. int harderr;
  232. /* Report error on raw socket, if:
  233. 1. User requested recverr.
  234. 2. Socket is connected (otherwise the error indication
  235. is useless without recverr and error is hard.
  236. */
  237. if (!np->recverr && sk->sk_state != TCP_ESTABLISHED)
  238. return;
  239. harderr = icmpv6_err_convert(type, code, &err);
  240. if (type == ICMPV6_PKT_TOOBIG)
  241. harderr = (np->pmtudisc == IPV6_PMTUDISC_DO);
  242. if (np->recverr) {
  243. u8 *payload = skb->data;
  244. if (!inet->hdrincl)
  245. payload += offset;
  246. ipv6_icmp_error(sk, skb, err, 0, ntohl(info), payload);
  247. }
  248. if (np->recverr || harderr) {
  249. sk->sk_err = err;
  250. sk->sk_error_report(sk);
  251. }
  252. }
  253. static inline int rawv6_rcv_skb(struct sock * sk, struct sk_buff * skb)
  254. {
  255. if ((raw6_sk(sk)->checksum || sk->sk_filter) &&
  256. skb_checksum_complete(skb)) {
  257. /* FIXME: increment a raw6 drops counter here */
  258. kfree_skb(skb);
  259. return 0;
  260. }
  261. /* Charge it to the socket. */
  262. if (sock_queue_rcv_skb(sk,skb)<0) {
  263. /* FIXME: increment a raw6 drops counter here */
  264. kfree_skb(skb);
  265. return 0;
  266. }
  267. return 0;
  268. }
  269. /*
  270. * This is next to useless...
  271. * if we demultiplex in network layer we don't need the extra call
  272. * just to queue the skb...
  273. * maybe we could have the network decide upon a hint if it
  274. * should call raw_rcv for demultiplexing
  275. */
  276. int rawv6_rcv(struct sock *sk, struct sk_buff *skb)
  277. {
  278. struct inet_sock *inet = inet_sk(sk);
  279. struct raw6_sock *rp = raw6_sk(sk);
  280. if (!xfrm6_policy_check(sk, XFRM_POLICY_IN, skb)) {
  281. kfree_skb(skb);
  282. return NET_RX_DROP;
  283. }
  284. if (!rp->checksum)
  285. skb->ip_summed = CHECKSUM_UNNECESSARY;
  286. if (skb->ip_summed == CHECKSUM_HW) {
  287. skb_postpull_rcsum(skb, skb->nh.raw,
  288. skb->h.raw - skb->nh.raw);
  289. if (!csum_ipv6_magic(&skb->nh.ipv6h->saddr,
  290. &skb->nh.ipv6h->daddr,
  291. skb->len, inet->num, skb->csum))
  292. skb->ip_summed = CHECKSUM_UNNECESSARY;
  293. }
  294. if (skb->ip_summed != CHECKSUM_UNNECESSARY)
  295. skb->csum = ~csum_ipv6_magic(&skb->nh.ipv6h->saddr,
  296. &skb->nh.ipv6h->daddr,
  297. skb->len, inet->num, 0);
  298. if (inet->hdrincl) {
  299. if (skb_checksum_complete(skb)) {
  300. /* FIXME: increment a raw6 drops counter here */
  301. kfree_skb(skb);
  302. return 0;
  303. }
  304. }
  305. rawv6_rcv_skb(sk, skb);
  306. return 0;
  307. }
  308. /*
  309. * This should be easy, if there is something there
  310. * we return it, otherwise we block.
  311. */
  312. static int rawv6_recvmsg(struct kiocb *iocb, struct sock *sk,
  313. struct msghdr *msg, size_t len,
  314. int noblock, int flags, int *addr_len)
  315. {
  316. struct ipv6_pinfo *np = inet6_sk(sk);
  317. struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)msg->msg_name;
  318. struct sk_buff *skb;
  319. size_t copied;
  320. int err;
  321. if (flags & MSG_OOB)
  322. return -EOPNOTSUPP;
  323. if (addr_len)
  324. *addr_len=sizeof(*sin6);
  325. if (flags & MSG_ERRQUEUE)
  326. return ipv6_recv_error(sk, msg, len);
  327. skb = skb_recv_datagram(sk, flags, noblock, &err);
  328. if (!skb)
  329. goto out;
  330. copied = skb->len;
  331. if (copied > len) {
  332. copied = len;
  333. msg->msg_flags |= MSG_TRUNC;
  334. }
  335. if (skb->ip_summed==CHECKSUM_UNNECESSARY) {
  336. err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
  337. } else if (msg->msg_flags&MSG_TRUNC) {
  338. if (__skb_checksum_complete(skb))
  339. goto csum_copy_err;
  340. err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
  341. } else {
  342. err = skb_copy_and_csum_datagram_iovec(skb, 0, msg->msg_iov);
  343. if (err == -EINVAL)
  344. goto csum_copy_err;
  345. }
  346. if (err)
  347. goto out_free;
  348. /* Copy the address. */
  349. if (sin6) {
  350. sin6->sin6_family = AF_INET6;
  351. sin6->sin6_port = 0;
  352. ipv6_addr_copy(&sin6->sin6_addr, &skb->nh.ipv6h->saddr);
  353. sin6->sin6_flowinfo = 0;
  354. sin6->sin6_scope_id = 0;
  355. if (ipv6_addr_type(&sin6->sin6_addr) & IPV6_ADDR_LINKLOCAL)
  356. sin6->sin6_scope_id = IP6CB(skb)->iif;
  357. }
  358. sock_recv_timestamp(msg, sk, skb);
  359. if (np->rxopt.all)
  360. datagram_recv_ctl(sk, msg, skb);
  361. err = copied;
  362. if (flags & MSG_TRUNC)
  363. err = skb->len;
  364. out_free:
  365. skb_free_datagram(sk, skb);
  366. out:
  367. return err;
  368. csum_copy_err:
  369. skb_kill_datagram(sk, skb, flags);
  370. /* Error for blocking case is chosen to masquerade
  371. as some normal condition.
  372. */
  373. err = (flags&MSG_DONTWAIT) ? -EAGAIN : -EHOSTUNREACH;
  374. /* FIXME: increment a raw6 drops counter here */
  375. goto out;
  376. }
  377. static int rawv6_push_pending_frames(struct sock *sk, struct flowi *fl,
  378. struct raw6_sock *rp)
  379. {
  380. struct sk_buff *skb;
  381. int err = 0;
  382. int offset;
  383. int len;
  384. int total_len;
  385. u32 tmp_csum;
  386. u16 csum;
  387. if (!rp->checksum)
  388. goto send;
  389. if ((skb = skb_peek(&sk->sk_write_queue)) == NULL)
  390. goto out;
  391. offset = rp->offset;
  392. total_len = inet_sk(sk)->cork.length - (skb->nh.raw - skb->data);
  393. if (offset >= total_len - 1) {
  394. err = -EINVAL;
  395. ip6_flush_pending_frames(sk);
  396. goto out;
  397. }
  398. /* should be check HW csum miyazawa */
  399. if (skb_queue_len(&sk->sk_write_queue) == 1) {
  400. /*
  401. * Only one fragment on the socket.
  402. */
  403. tmp_csum = skb->csum;
  404. } else {
  405. struct sk_buff *csum_skb = NULL;
  406. tmp_csum = 0;
  407. skb_queue_walk(&sk->sk_write_queue, skb) {
  408. tmp_csum = csum_add(tmp_csum, skb->csum);
  409. if (csum_skb)
  410. continue;
  411. len = skb->len - (skb->h.raw - skb->data);
  412. if (offset >= len) {
  413. offset -= len;
  414. continue;
  415. }
  416. csum_skb = skb;
  417. }
  418. skb = csum_skb;
  419. }
  420. offset += skb->h.raw - skb->data;
  421. if (skb_copy_bits(skb, offset, &csum, 2))
  422. BUG();
  423. /* in case cksum was not initialized */
  424. if (unlikely(csum))
  425. tmp_csum = csum_sub(tmp_csum, csum);
  426. tmp_csum = csum_ipv6_magic(&fl->fl6_src,
  427. &fl->fl6_dst,
  428. total_len, fl->proto, tmp_csum);
  429. if (tmp_csum == 0)
  430. tmp_csum = -1;
  431. csum = tmp_csum;
  432. if (skb_store_bits(skb, offset, &csum, 2))
  433. BUG();
  434. send:
  435. err = ip6_push_pending_frames(sk);
  436. out:
  437. return err;
  438. }
  439. static int rawv6_send_hdrinc(struct sock *sk, void *from, int length,
  440. struct flowi *fl, struct rt6_info *rt,
  441. unsigned int flags)
  442. {
  443. struct ipv6_pinfo *np = inet6_sk(sk);
  444. struct ipv6hdr *iph;
  445. struct sk_buff *skb;
  446. unsigned int hh_len;
  447. int err;
  448. if (length > rt->u.dst.dev->mtu) {
  449. ipv6_local_error(sk, EMSGSIZE, fl, rt->u.dst.dev->mtu);
  450. return -EMSGSIZE;
  451. }
  452. if (flags&MSG_PROBE)
  453. goto out;
  454. hh_len = LL_RESERVED_SPACE(rt->u.dst.dev);
  455. skb = sock_alloc_send_skb(sk, length+hh_len+15,
  456. flags&MSG_DONTWAIT, &err);
  457. if (skb == NULL)
  458. goto error;
  459. skb_reserve(skb, hh_len);
  460. skb->priority = sk->sk_priority;
  461. skb->dst = dst_clone(&rt->u.dst);
  462. skb->nh.ipv6h = iph = (struct ipv6hdr *)skb_put(skb, length);
  463. skb->ip_summed = CHECKSUM_NONE;
  464. skb->h.raw = skb->nh.raw;
  465. err = memcpy_fromiovecend((void *)iph, from, 0, length);
  466. if (err)
  467. goto error_fault;
  468. IP6_INC_STATS(IPSTATS_MIB_OUTREQUESTS);
  469. err = NF_HOOK(PF_INET6, NF_IP6_LOCAL_OUT, skb, NULL, rt->u.dst.dev,
  470. dst_output);
  471. if (err > 0)
  472. err = np->recverr ? net_xmit_errno(err) : 0;
  473. if (err)
  474. goto error;
  475. out:
  476. return 0;
  477. error_fault:
  478. err = -EFAULT;
  479. kfree_skb(skb);
  480. error:
  481. IP6_INC_STATS(IPSTATS_MIB_OUTDISCARDS);
  482. return err;
  483. }
  484. static void rawv6_probe_proto_opt(struct flowi *fl, struct msghdr *msg)
  485. {
  486. struct iovec *iov;
  487. u8 __user *type = NULL;
  488. u8 __user *code = NULL;
  489. int probed = 0;
  490. int i;
  491. if (!msg->msg_iov)
  492. return;
  493. for (i = 0; i < msg->msg_iovlen; i++) {
  494. iov = &msg->msg_iov[i];
  495. if (!iov)
  496. continue;
  497. switch (fl->proto) {
  498. case IPPROTO_ICMPV6:
  499. /* check if one-byte field is readable or not. */
  500. if (iov->iov_base && iov->iov_len < 1)
  501. break;
  502. if (!type) {
  503. type = iov->iov_base;
  504. /* check if code field is readable or not. */
  505. if (iov->iov_len > 1)
  506. code = type + 1;
  507. } else if (!code)
  508. code = iov->iov_base;
  509. if (type && code) {
  510. get_user(fl->fl_icmp_type, type);
  511. get_user(fl->fl_icmp_code, code);
  512. probed = 1;
  513. }
  514. break;
  515. default:
  516. probed = 1;
  517. break;
  518. }
  519. if (probed)
  520. break;
  521. }
  522. }
  523. static int rawv6_sendmsg(struct kiocb *iocb, struct sock *sk,
  524. struct msghdr *msg, size_t len)
  525. {
  526. struct ipv6_txoptions opt_space;
  527. struct sockaddr_in6 * sin6 = (struct sockaddr_in6 *) msg->msg_name;
  528. struct in6_addr *daddr, *final_p = NULL, final;
  529. struct inet_sock *inet = inet_sk(sk);
  530. struct ipv6_pinfo *np = inet6_sk(sk);
  531. struct raw6_sock *rp = raw6_sk(sk);
  532. struct ipv6_txoptions *opt = NULL;
  533. struct ip6_flowlabel *flowlabel = NULL;
  534. struct dst_entry *dst = NULL;
  535. struct flowi fl;
  536. int addr_len = msg->msg_namelen;
  537. int hlimit = -1;
  538. int tclass = -1;
  539. u16 proto;
  540. int err;
  541. /* Rough check on arithmetic overflow,
  542. better check is made in ip6_build_xmit
  543. */
  544. if (len < 0)
  545. return -EMSGSIZE;
  546. /* Mirror BSD error message compatibility */
  547. if (msg->msg_flags & MSG_OOB)
  548. return -EOPNOTSUPP;
  549. /*
  550. * Get and verify the address.
  551. */
  552. memset(&fl, 0, sizeof(fl));
  553. if (sin6) {
  554. if (addr_len < SIN6_LEN_RFC2133)
  555. return -EINVAL;
  556. if (sin6->sin6_family && sin6->sin6_family != AF_INET6)
  557. return(-EAFNOSUPPORT);
  558. /* port is the proto value [0..255] carried in nexthdr */
  559. proto = ntohs(sin6->sin6_port);
  560. if (!proto)
  561. proto = inet->num;
  562. else if (proto != inet->num)
  563. return(-EINVAL);
  564. if (proto > 255)
  565. return(-EINVAL);
  566. daddr = &sin6->sin6_addr;
  567. if (np->sndflow) {
  568. fl.fl6_flowlabel = sin6->sin6_flowinfo&IPV6_FLOWINFO_MASK;
  569. if (fl.fl6_flowlabel&IPV6_FLOWLABEL_MASK) {
  570. flowlabel = fl6_sock_lookup(sk, fl.fl6_flowlabel);
  571. if (flowlabel == NULL)
  572. return -EINVAL;
  573. daddr = &flowlabel->dst;
  574. }
  575. }
  576. /*
  577. * Otherwise it will be difficult to maintain
  578. * sk->sk_dst_cache.
  579. */
  580. if (sk->sk_state == TCP_ESTABLISHED &&
  581. ipv6_addr_equal(daddr, &np->daddr))
  582. daddr = &np->daddr;
  583. if (addr_len >= sizeof(struct sockaddr_in6) &&
  584. sin6->sin6_scope_id &&
  585. ipv6_addr_type(daddr)&IPV6_ADDR_LINKLOCAL)
  586. fl.oif = sin6->sin6_scope_id;
  587. } else {
  588. if (sk->sk_state != TCP_ESTABLISHED)
  589. return -EDESTADDRREQ;
  590. proto = inet->num;
  591. daddr = &np->daddr;
  592. fl.fl6_flowlabel = np->flow_label;
  593. }
  594. if (ipv6_addr_any(daddr)) {
  595. /*
  596. * unspecified destination address
  597. * treated as error... is this correct ?
  598. */
  599. fl6_sock_release(flowlabel);
  600. return(-EINVAL);
  601. }
  602. if (fl.oif == 0)
  603. fl.oif = sk->sk_bound_dev_if;
  604. if (msg->msg_controllen) {
  605. opt = &opt_space;
  606. memset(opt, 0, sizeof(struct ipv6_txoptions));
  607. opt->tot_len = sizeof(struct ipv6_txoptions);
  608. err = datagram_send_ctl(msg, &fl, opt, &hlimit, &tclass);
  609. if (err < 0) {
  610. fl6_sock_release(flowlabel);
  611. return err;
  612. }
  613. if ((fl.fl6_flowlabel&IPV6_FLOWLABEL_MASK) && !flowlabel) {
  614. flowlabel = fl6_sock_lookup(sk, fl.fl6_flowlabel);
  615. if (flowlabel == NULL)
  616. return -EINVAL;
  617. }
  618. if (!(opt->opt_nflen|opt->opt_flen))
  619. opt = NULL;
  620. }
  621. if (opt == NULL)
  622. opt = np->opt;
  623. if (flowlabel)
  624. opt = fl6_merge_options(&opt_space, flowlabel, opt);
  625. opt = ipv6_fixup_options(&opt_space, opt);
  626. fl.proto = proto;
  627. rawv6_probe_proto_opt(&fl, msg);
  628. ipv6_addr_copy(&fl.fl6_dst, daddr);
  629. if (ipv6_addr_any(&fl.fl6_src) && !ipv6_addr_any(&np->saddr))
  630. ipv6_addr_copy(&fl.fl6_src, &np->saddr);
  631. /* merge ip6_build_xmit from ip6_output */
  632. if (opt && opt->srcrt) {
  633. struct rt0_hdr *rt0 = (struct rt0_hdr *) opt->srcrt;
  634. ipv6_addr_copy(&final, &fl.fl6_dst);
  635. ipv6_addr_copy(&fl.fl6_dst, rt0->addr);
  636. final_p = &final;
  637. }
  638. if (!fl.oif && ipv6_addr_is_multicast(&fl.fl6_dst))
  639. fl.oif = np->mcast_oif;
  640. err = ip6_dst_lookup(sk, &dst, &fl);
  641. if (err)
  642. goto out;
  643. if (final_p)
  644. ipv6_addr_copy(&fl.fl6_dst, final_p);
  645. if ((err = xfrm_lookup(&dst, &fl, sk, 0)) < 0)
  646. goto out;
  647. if (hlimit < 0) {
  648. if (ipv6_addr_is_multicast(&fl.fl6_dst))
  649. hlimit = np->mcast_hops;
  650. else
  651. hlimit = np->hop_limit;
  652. if (hlimit < 0)
  653. hlimit = dst_metric(dst, RTAX_HOPLIMIT);
  654. if (hlimit < 0)
  655. hlimit = ipv6_get_hoplimit(dst->dev);
  656. }
  657. if (tclass < 0) {
  658. tclass = np->cork.tclass;
  659. if (tclass < 0)
  660. tclass = 0;
  661. }
  662. if (msg->msg_flags&MSG_CONFIRM)
  663. goto do_confirm;
  664. back_from_confirm:
  665. if (inet->hdrincl) {
  666. err = rawv6_send_hdrinc(sk, msg->msg_iov, len, &fl, (struct rt6_info*)dst, msg->msg_flags);
  667. } else {
  668. lock_sock(sk);
  669. err = ip6_append_data(sk, ip_generic_getfrag, msg->msg_iov,
  670. len, 0, hlimit, tclass, opt, &fl, (struct rt6_info*)dst,
  671. msg->msg_flags);
  672. if (err)
  673. ip6_flush_pending_frames(sk);
  674. else if (!(msg->msg_flags & MSG_MORE))
  675. err = rawv6_push_pending_frames(sk, &fl, rp);
  676. }
  677. done:
  678. dst_release(dst);
  679. release_sock(sk);
  680. out:
  681. fl6_sock_release(flowlabel);
  682. return err<0?err:len;
  683. do_confirm:
  684. dst_confirm(dst);
  685. if (!(msg->msg_flags & MSG_PROBE) || len)
  686. goto back_from_confirm;
  687. err = 0;
  688. goto done;
  689. }
  690. static int rawv6_seticmpfilter(struct sock *sk, int level, int optname,
  691. char __user *optval, int optlen)
  692. {
  693. switch (optname) {
  694. case ICMPV6_FILTER:
  695. if (optlen > sizeof(struct icmp6_filter))
  696. optlen = sizeof(struct icmp6_filter);
  697. if (copy_from_user(&raw6_sk(sk)->filter, optval, optlen))
  698. return -EFAULT;
  699. return 0;
  700. default:
  701. return -ENOPROTOOPT;
  702. };
  703. return 0;
  704. }
  705. static int rawv6_geticmpfilter(struct sock *sk, int level, int optname,
  706. char __user *optval, int __user *optlen)
  707. {
  708. int len;
  709. switch (optname) {
  710. case ICMPV6_FILTER:
  711. if (get_user(len, optlen))
  712. return -EFAULT;
  713. if (len < 0)
  714. return -EINVAL;
  715. if (len > sizeof(struct icmp6_filter))
  716. len = sizeof(struct icmp6_filter);
  717. if (put_user(len, optlen))
  718. return -EFAULT;
  719. if (copy_to_user(optval, &raw6_sk(sk)->filter, len))
  720. return -EFAULT;
  721. return 0;
  722. default:
  723. return -ENOPROTOOPT;
  724. };
  725. return 0;
  726. }
  727. static int do_rawv6_setsockopt(struct sock *sk, int level, int optname,
  728. char __user *optval, int optlen)
  729. {
  730. struct raw6_sock *rp = raw6_sk(sk);
  731. int val;
  732. if (get_user(val, (int __user *)optval))
  733. return -EFAULT;
  734. switch (optname) {
  735. case IPV6_CHECKSUM:
  736. /* You may get strange result with a positive odd offset;
  737. RFC2292bis agrees with me. */
  738. if (val > 0 && (val&1))
  739. return(-EINVAL);
  740. if (val < 0) {
  741. rp->checksum = 0;
  742. } else {
  743. rp->checksum = 1;
  744. rp->offset = val;
  745. }
  746. return 0;
  747. break;
  748. default:
  749. return(-ENOPROTOOPT);
  750. }
  751. }
  752. static int rawv6_setsockopt(struct sock *sk, int level, int optname,
  753. char __user *optval, int optlen)
  754. {
  755. switch(level) {
  756. case SOL_RAW:
  757. break;
  758. case SOL_ICMPV6:
  759. if (inet_sk(sk)->num != IPPROTO_ICMPV6)
  760. return -EOPNOTSUPP;
  761. return rawv6_seticmpfilter(sk, level, optname, optval,
  762. optlen);
  763. case SOL_IPV6:
  764. if (optname == IPV6_CHECKSUM)
  765. break;
  766. default:
  767. return ipv6_setsockopt(sk, level, optname, optval,
  768. optlen);
  769. };
  770. return do_rawv6_setsockopt(sk, level, optname, optval, optlen);
  771. }
  772. #ifdef CONFIG_COMPAT
  773. static int compat_rawv6_setsockopt(struct sock *sk, int level, int optname,
  774. char __user *optval, int optlen)
  775. {
  776. switch (level) {
  777. case SOL_RAW:
  778. break;
  779. case SOL_ICMPV6:
  780. if (inet_sk(sk)->num != IPPROTO_ICMPV6)
  781. return -EOPNOTSUPP;
  782. return rawv6_seticmpfilter(sk, level, optname, optval, optlen);
  783. case SOL_IPV6:
  784. if (optname == IPV6_CHECKSUM)
  785. break;
  786. default:
  787. return compat_ipv6_setsockopt(sk, level, optname,
  788. optval, optlen);
  789. };
  790. return do_rawv6_setsockopt(sk, level, optname, optval, optlen);
  791. }
  792. #endif
  793. static int do_rawv6_getsockopt(struct sock *sk, int level, int optname,
  794. char __user *optval, int __user *optlen)
  795. {
  796. struct raw6_sock *rp = raw6_sk(sk);
  797. int val, len;
  798. if (get_user(len,optlen))
  799. return -EFAULT;
  800. switch (optname) {
  801. case IPV6_CHECKSUM:
  802. if (rp->checksum == 0)
  803. val = -1;
  804. else
  805. val = rp->offset;
  806. break;
  807. default:
  808. return -ENOPROTOOPT;
  809. }
  810. len = min_t(unsigned int, sizeof(int), len);
  811. if (put_user(len, optlen))
  812. return -EFAULT;
  813. if (copy_to_user(optval,&val,len))
  814. return -EFAULT;
  815. return 0;
  816. }
  817. static int rawv6_getsockopt(struct sock *sk, int level, int optname,
  818. char __user *optval, int __user *optlen)
  819. {
  820. switch(level) {
  821. case SOL_RAW:
  822. break;
  823. case SOL_ICMPV6:
  824. if (inet_sk(sk)->num != IPPROTO_ICMPV6)
  825. return -EOPNOTSUPP;
  826. return rawv6_geticmpfilter(sk, level, optname, optval,
  827. optlen);
  828. case SOL_IPV6:
  829. if (optname == IPV6_CHECKSUM)
  830. break;
  831. default:
  832. return ipv6_getsockopt(sk, level, optname, optval,
  833. optlen);
  834. };
  835. return do_rawv6_getsockopt(sk, level, optname, optval, optlen);
  836. }
  837. #ifdef CONFIG_COMPAT
  838. static int compat_rawv6_getsockopt(struct sock *sk, int level, int optname,
  839. char __user *optval, int __user *optlen)
  840. {
  841. switch (level) {
  842. case SOL_RAW:
  843. break;
  844. case SOL_ICMPV6:
  845. if (inet_sk(sk)->num != IPPROTO_ICMPV6)
  846. return -EOPNOTSUPP;
  847. return rawv6_geticmpfilter(sk, level, optname, optval, optlen);
  848. case SOL_IPV6:
  849. if (optname == IPV6_CHECKSUM)
  850. break;
  851. default:
  852. return compat_ipv6_getsockopt(sk, level, optname,
  853. optval, optlen);
  854. };
  855. return do_rawv6_getsockopt(sk, level, optname, optval, optlen);
  856. }
  857. #endif
  858. static int rawv6_ioctl(struct sock *sk, int cmd, unsigned long arg)
  859. {
  860. switch(cmd) {
  861. case SIOCOUTQ:
  862. {
  863. int amount = atomic_read(&sk->sk_wmem_alloc);
  864. return put_user(amount, (int __user *)arg);
  865. }
  866. case SIOCINQ:
  867. {
  868. struct sk_buff *skb;
  869. int amount = 0;
  870. spin_lock_bh(&sk->sk_receive_queue.lock);
  871. skb = skb_peek(&sk->sk_receive_queue);
  872. if (skb != NULL)
  873. amount = skb->tail - skb->h.raw;
  874. spin_unlock_bh(&sk->sk_receive_queue.lock);
  875. return put_user(amount, (int __user *)arg);
  876. }
  877. default:
  878. return -ENOIOCTLCMD;
  879. }
  880. }
  881. static void rawv6_close(struct sock *sk, long timeout)
  882. {
  883. if (inet_sk(sk)->num == IPPROTO_RAW)
  884. ip6_ra_control(sk, -1, NULL);
  885. sk_common_release(sk);
  886. }
  887. static int rawv6_init_sk(struct sock *sk)
  888. {
  889. if (inet_sk(sk)->num == IPPROTO_ICMPV6) {
  890. struct raw6_sock *rp = raw6_sk(sk);
  891. rp->checksum = 1;
  892. rp->offset = 2;
  893. }
  894. return(0);
  895. }
  896. struct proto rawv6_prot = {
  897. .name = "RAWv6",
  898. .owner = THIS_MODULE,
  899. .close = rawv6_close,
  900. .connect = ip6_datagram_connect,
  901. .disconnect = udp_disconnect,
  902. .ioctl = rawv6_ioctl,
  903. .init = rawv6_init_sk,
  904. .destroy = inet6_destroy_sock,
  905. .setsockopt = rawv6_setsockopt,
  906. .getsockopt = rawv6_getsockopt,
  907. .sendmsg = rawv6_sendmsg,
  908. .recvmsg = rawv6_recvmsg,
  909. .bind = rawv6_bind,
  910. .backlog_rcv = rawv6_rcv_skb,
  911. .hash = raw_v6_hash,
  912. .unhash = raw_v6_unhash,
  913. .obj_size = sizeof(struct raw6_sock),
  914. #ifdef CONFIG_COMPAT
  915. .compat_setsockopt = compat_rawv6_setsockopt,
  916. .compat_getsockopt = compat_rawv6_getsockopt,
  917. #endif
  918. };
  919. #ifdef CONFIG_PROC_FS
  920. struct raw6_iter_state {
  921. int bucket;
  922. };
  923. #define raw6_seq_private(seq) ((struct raw6_iter_state *)(seq)->private)
  924. static struct sock *raw6_get_first(struct seq_file *seq)
  925. {
  926. struct sock *sk;
  927. struct hlist_node *node;
  928. struct raw6_iter_state* state = raw6_seq_private(seq);
  929. for (state->bucket = 0; state->bucket < RAWV6_HTABLE_SIZE; ++state->bucket)
  930. sk_for_each(sk, node, &raw_v6_htable[state->bucket])
  931. if (sk->sk_family == PF_INET6)
  932. goto out;
  933. sk = NULL;
  934. out:
  935. return sk;
  936. }
  937. static struct sock *raw6_get_next(struct seq_file *seq, struct sock *sk)
  938. {
  939. struct raw6_iter_state* state = raw6_seq_private(seq);
  940. do {
  941. sk = sk_next(sk);
  942. try_again:
  943. ;
  944. } while (sk && sk->sk_family != PF_INET6);
  945. if (!sk && ++state->bucket < RAWV6_HTABLE_SIZE) {
  946. sk = sk_head(&raw_v6_htable[state->bucket]);
  947. goto try_again;
  948. }
  949. return sk;
  950. }
  951. static struct sock *raw6_get_idx(struct seq_file *seq, loff_t pos)
  952. {
  953. struct sock *sk = raw6_get_first(seq);
  954. if (sk)
  955. while (pos && (sk = raw6_get_next(seq, sk)) != NULL)
  956. --pos;
  957. return pos ? NULL : sk;
  958. }
  959. static void *raw6_seq_start(struct seq_file *seq, loff_t *pos)
  960. {
  961. read_lock(&raw_v6_lock);
  962. return *pos ? raw6_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
  963. }
  964. static void *raw6_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  965. {
  966. struct sock *sk;
  967. if (v == SEQ_START_TOKEN)
  968. sk = raw6_get_first(seq);
  969. else
  970. sk = raw6_get_next(seq, v);
  971. ++*pos;
  972. return sk;
  973. }
  974. static void raw6_seq_stop(struct seq_file *seq, void *v)
  975. {
  976. read_unlock(&raw_v6_lock);
  977. }
  978. static void raw6_sock_seq_show(struct seq_file *seq, struct sock *sp, int i)
  979. {
  980. struct ipv6_pinfo *np = inet6_sk(sp);
  981. struct in6_addr *dest, *src;
  982. __u16 destp, srcp;
  983. dest = &np->daddr;
  984. src = &np->rcv_saddr;
  985. destp = 0;
  986. srcp = inet_sk(sp)->num;
  987. seq_printf(seq,
  988. "%4d: %08X%08X%08X%08X:%04X %08X%08X%08X%08X:%04X "
  989. "%02X %08X:%08X %02X:%08lX %08X %5d %8d %lu %d %p\n",
  990. i,
  991. src->s6_addr32[0], src->s6_addr32[1],
  992. src->s6_addr32[2], src->s6_addr32[3], srcp,
  993. dest->s6_addr32[0], dest->s6_addr32[1],
  994. dest->s6_addr32[2], dest->s6_addr32[3], destp,
  995. sp->sk_state,
  996. atomic_read(&sp->sk_wmem_alloc),
  997. atomic_read(&sp->sk_rmem_alloc),
  998. 0, 0L, 0,
  999. sock_i_uid(sp), 0,
  1000. sock_i_ino(sp),
  1001. atomic_read(&sp->sk_refcnt), sp);
  1002. }
  1003. static int raw6_seq_show(struct seq_file *seq, void *v)
  1004. {
  1005. if (v == SEQ_START_TOKEN)
  1006. seq_printf(seq,
  1007. " sl "
  1008. "local_address "
  1009. "remote_address "
  1010. "st tx_queue rx_queue tr tm->when retrnsmt"
  1011. " uid timeout inode\n");
  1012. else
  1013. raw6_sock_seq_show(seq, v, raw6_seq_private(seq)->bucket);
  1014. return 0;
  1015. }
  1016. static struct seq_operations raw6_seq_ops = {
  1017. .start = raw6_seq_start,
  1018. .next = raw6_seq_next,
  1019. .stop = raw6_seq_stop,
  1020. .show = raw6_seq_show,
  1021. };
  1022. static int raw6_seq_open(struct inode *inode, struct file *file)
  1023. {
  1024. struct seq_file *seq;
  1025. int rc = -ENOMEM;
  1026. struct raw6_iter_state *s = kzalloc(sizeof(*s), GFP_KERNEL);
  1027. if (!s)
  1028. goto out;
  1029. rc = seq_open(file, &raw6_seq_ops);
  1030. if (rc)
  1031. goto out_kfree;
  1032. seq = file->private_data;
  1033. seq->private = s;
  1034. out:
  1035. return rc;
  1036. out_kfree:
  1037. kfree(s);
  1038. goto out;
  1039. }
  1040. static struct file_operations raw6_seq_fops = {
  1041. .owner = THIS_MODULE,
  1042. .open = raw6_seq_open,
  1043. .read = seq_read,
  1044. .llseek = seq_lseek,
  1045. .release = seq_release_private,
  1046. };
  1047. int __init raw6_proc_init(void)
  1048. {
  1049. if (!proc_net_fops_create("raw6", S_IRUGO, &raw6_seq_fops))
  1050. return -ENOMEM;
  1051. return 0;
  1052. }
  1053. void raw6_proc_exit(void)
  1054. {
  1055. proc_net_remove("raw6");
  1056. }
  1057. #endif /* CONFIG_PROC_FS */