raw.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371
  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. * Fixes:
  11. * Hideaki YOSHIFUJI : sin6_scope_id support
  12. * YOSHIFUJI,H.@USAGI : raw checksum (RFC2292(bis) compliance)
  13. * Kazunori MIYAZAWA @USAGI: change process style to use ip6_append_data
  14. *
  15. * This program is free software; you can redistribute it and/or
  16. * modify it under the terms of the GNU General Public License
  17. * as published by the Free Software Foundation; either version
  18. * 2 of the License, or (at your option) any later version.
  19. */
  20. #include <linux/errno.h>
  21. #include <linux/types.h>
  22. #include <linux/socket.h>
  23. #include <linux/slab.h>
  24. #include <linux/sockios.h>
  25. #include <linux/net.h>
  26. #include <linux/in6.h>
  27. #include <linux/netdevice.h>
  28. #include <linux/if_arp.h>
  29. #include <linux/icmpv6.h>
  30. #include <linux/netfilter.h>
  31. #include <linux/netfilter_ipv6.h>
  32. #include <linux/skbuff.h>
  33. #include <linux/compat.h>
  34. #include <asm/uaccess.h>
  35. #include <asm/ioctls.h>
  36. #include <net/net_namespace.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. #if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
  51. #include <net/mip6.h>
  52. #endif
  53. #include <linux/mroute6.h>
  54. #include <net/raw.h>
  55. #include <net/rawv6.h>
  56. #include <net/xfrm.h>
  57. #include <linux/proc_fs.h>
  58. #include <linux/seq_file.h>
  59. static struct raw_hashinfo raw_v6_hashinfo = {
  60. .lock = __RW_LOCK_UNLOCKED(raw_v6_hashinfo.lock),
  61. };
  62. static struct sock *__raw_v6_lookup(struct net *net, struct sock *sk,
  63. unsigned short num, const struct in6_addr *loc_addr,
  64. const struct in6_addr *rmt_addr, int dif)
  65. {
  66. struct hlist_node *node;
  67. int is_multicast = ipv6_addr_is_multicast(loc_addr);
  68. sk_for_each_from(sk, node)
  69. if (inet_sk(sk)->inet_num == num) {
  70. struct ipv6_pinfo *np = inet6_sk(sk);
  71. if (!net_eq(sock_net(sk), net))
  72. continue;
  73. if (!ipv6_addr_any(&np->daddr) &&
  74. !ipv6_addr_equal(&np->daddr, rmt_addr))
  75. continue;
  76. if (sk->sk_bound_dev_if && sk->sk_bound_dev_if != dif)
  77. continue;
  78. if (!ipv6_addr_any(&np->rcv_saddr)) {
  79. if (ipv6_addr_equal(&np->rcv_saddr, loc_addr))
  80. goto found;
  81. if (is_multicast &&
  82. inet6_mc_check(sk, loc_addr, rmt_addr))
  83. goto found;
  84. continue;
  85. }
  86. goto found;
  87. }
  88. sk = NULL;
  89. found:
  90. return sk;
  91. }
  92. /*
  93. * 0 - deliver
  94. * 1 - block
  95. */
  96. static __inline__ int icmpv6_filter(struct sock *sk, struct sk_buff *skb)
  97. {
  98. struct icmp6hdr *icmph;
  99. struct raw6_sock *rp = raw6_sk(sk);
  100. if (pskb_may_pull(skb, sizeof(struct icmp6hdr))) {
  101. __u32 *data = &rp->filter.data[0];
  102. int bit_nr;
  103. icmph = (struct icmp6hdr *) skb->data;
  104. bit_nr = icmph->icmp6_type;
  105. return (data[bit_nr >> 5] & (1 << (bit_nr & 31))) != 0;
  106. }
  107. return 0;
  108. }
  109. #if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
  110. typedef int mh_filter_t(struct sock *sock, struct sk_buff *skb);
  111. static mh_filter_t __rcu *mh_filter __read_mostly;
  112. int rawv6_mh_filter_register(mh_filter_t filter)
  113. {
  114. rcu_assign_pointer(mh_filter, filter);
  115. return 0;
  116. }
  117. EXPORT_SYMBOL(rawv6_mh_filter_register);
  118. int rawv6_mh_filter_unregister(mh_filter_t filter)
  119. {
  120. rcu_assign_pointer(mh_filter, NULL);
  121. synchronize_rcu();
  122. return 0;
  123. }
  124. EXPORT_SYMBOL(rawv6_mh_filter_unregister);
  125. #endif
  126. /*
  127. * demultiplex raw sockets.
  128. * (should consider queueing the skb in the sock receive_queue
  129. * without calling rawv6.c)
  130. *
  131. * Caller owns SKB so we must make clones.
  132. */
  133. static int ipv6_raw_deliver(struct sk_buff *skb, int nexthdr)
  134. {
  135. const struct in6_addr *saddr;
  136. const struct in6_addr *daddr;
  137. struct sock *sk;
  138. int delivered = 0;
  139. __u8 hash;
  140. struct net *net;
  141. saddr = &ipv6_hdr(skb)->saddr;
  142. daddr = saddr + 1;
  143. hash = nexthdr & (MAX_INET_PROTOS - 1);
  144. read_lock(&raw_v6_hashinfo.lock);
  145. sk = sk_head(&raw_v6_hashinfo.ht[hash]);
  146. if (sk == NULL)
  147. goto out;
  148. net = dev_net(skb->dev);
  149. sk = __raw_v6_lookup(net, sk, nexthdr, daddr, saddr, IP6CB(skb)->iif);
  150. while (sk) {
  151. int filtered;
  152. delivered = 1;
  153. switch (nexthdr) {
  154. case IPPROTO_ICMPV6:
  155. filtered = icmpv6_filter(sk, skb);
  156. break;
  157. #if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
  158. case IPPROTO_MH:
  159. {
  160. /* XXX: To validate MH only once for each packet,
  161. * this is placed here. It should be after checking
  162. * xfrm policy, however it doesn't. The checking xfrm
  163. * policy is placed in rawv6_rcv() because it is
  164. * required for each socket.
  165. */
  166. mh_filter_t *filter;
  167. filter = rcu_dereference(mh_filter);
  168. filtered = filter ? (*filter)(sk, skb) : 0;
  169. break;
  170. }
  171. #endif
  172. default:
  173. filtered = 0;
  174. break;
  175. }
  176. if (filtered < 0)
  177. break;
  178. if (filtered == 0) {
  179. struct sk_buff *clone = skb_clone(skb, GFP_ATOMIC);
  180. /* Not releasing hash table! */
  181. if (clone) {
  182. nf_reset(clone);
  183. rawv6_rcv(sk, clone);
  184. }
  185. }
  186. sk = __raw_v6_lookup(net, sk_next(sk), nexthdr, daddr, saddr,
  187. IP6CB(skb)->iif);
  188. }
  189. out:
  190. read_unlock(&raw_v6_hashinfo.lock);
  191. return delivered;
  192. }
  193. int raw6_local_deliver(struct sk_buff *skb, int nexthdr)
  194. {
  195. struct sock *raw_sk;
  196. raw_sk = sk_head(&raw_v6_hashinfo.ht[nexthdr & (MAX_INET_PROTOS - 1)]);
  197. if (raw_sk && !ipv6_raw_deliver(skb, nexthdr))
  198. raw_sk = NULL;
  199. return raw_sk != NULL;
  200. }
  201. /* This cleans up af_inet6 a bit. -DaveM */
  202. static int rawv6_bind(struct sock *sk, struct sockaddr *uaddr, int addr_len)
  203. {
  204. struct inet_sock *inet = inet_sk(sk);
  205. struct ipv6_pinfo *np = inet6_sk(sk);
  206. struct sockaddr_in6 *addr = (struct sockaddr_in6 *) uaddr;
  207. __be32 v4addr = 0;
  208. int addr_type;
  209. int err;
  210. if (addr_len < SIN6_LEN_RFC2133)
  211. return -EINVAL;
  212. addr_type = ipv6_addr_type(&addr->sin6_addr);
  213. /* Raw sockets are IPv6 only */
  214. if (addr_type == IPV6_ADDR_MAPPED)
  215. return -EADDRNOTAVAIL;
  216. lock_sock(sk);
  217. err = -EINVAL;
  218. if (sk->sk_state != TCP_CLOSE)
  219. goto out;
  220. rcu_read_lock();
  221. /* Check if the address belongs to the host. */
  222. if (addr_type != IPV6_ADDR_ANY) {
  223. struct net_device *dev = NULL;
  224. if (addr_type & IPV6_ADDR_LINKLOCAL) {
  225. if (addr_len >= sizeof(struct sockaddr_in6) &&
  226. addr->sin6_scope_id) {
  227. /* Override any existing binding, if another
  228. * one is supplied by user.
  229. */
  230. sk->sk_bound_dev_if = addr->sin6_scope_id;
  231. }
  232. /* Binding to link-local address requires an interface */
  233. if (!sk->sk_bound_dev_if)
  234. goto out_unlock;
  235. err = -ENODEV;
  236. dev = dev_get_by_index_rcu(sock_net(sk),
  237. sk->sk_bound_dev_if);
  238. if (!dev)
  239. goto out_unlock;
  240. }
  241. /* ipv4 addr of the socket is invalid. Only the
  242. * unspecified and mapped address have a v4 equivalent.
  243. */
  244. v4addr = LOOPBACK4_IPV6;
  245. if (!(addr_type & IPV6_ADDR_MULTICAST)) {
  246. err = -EADDRNOTAVAIL;
  247. if (!ipv6_chk_addr(sock_net(sk), &addr->sin6_addr,
  248. dev, 0)) {
  249. goto out_unlock;
  250. }
  251. }
  252. }
  253. inet->inet_rcv_saddr = inet->inet_saddr = v4addr;
  254. ipv6_addr_copy(&np->rcv_saddr, &addr->sin6_addr);
  255. if (!(addr_type & IPV6_ADDR_MULTICAST))
  256. ipv6_addr_copy(&np->saddr, &addr->sin6_addr);
  257. err = 0;
  258. out_unlock:
  259. rcu_read_unlock();
  260. out:
  261. release_sock(sk);
  262. return err;
  263. }
  264. static void rawv6_err(struct sock *sk, struct sk_buff *skb,
  265. struct inet6_skb_parm *opt,
  266. u8 type, u8 code, int offset, __be32 info)
  267. {
  268. struct inet_sock *inet = inet_sk(sk);
  269. struct ipv6_pinfo *np = inet6_sk(sk);
  270. int err;
  271. int harderr;
  272. /* Report error on raw socket, if:
  273. 1. User requested recverr.
  274. 2. Socket is connected (otherwise the error indication
  275. is useless without recverr and error is hard.
  276. */
  277. if (!np->recverr && sk->sk_state != TCP_ESTABLISHED)
  278. return;
  279. harderr = icmpv6_err_convert(type, code, &err);
  280. if (type == ICMPV6_PKT_TOOBIG)
  281. harderr = (np->pmtudisc == IPV6_PMTUDISC_DO);
  282. if (np->recverr) {
  283. u8 *payload = skb->data;
  284. if (!inet->hdrincl)
  285. payload += offset;
  286. ipv6_icmp_error(sk, skb, err, 0, ntohl(info), payload);
  287. }
  288. if (np->recverr || harderr) {
  289. sk->sk_err = err;
  290. sk->sk_error_report(sk);
  291. }
  292. }
  293. void raw6_icmp_error(struct sk_buff *skb, int nexthdr,
  294. u8 type, u8 code, int inner_offset, __be32 info)
  295. {
  296. struct sock *sk;
  297. int hash;
  298. const struct in6_addr *saddr, *daddr;
  299. struct net *net;
  300. hash = nexthdr & (RAW_HTABLE_SIZE - 1);
  301. read_lock(&raw_v6_hashinfo.lock);
  302. sk = sk_head(&raw_v6_hashinfo.ht[hash]);
  303. if (sk != NULL) {
  304. /* Note: ipv6_hdr(skb) != skb->data */
  305. const struct ipv6hdr *ip6h = (const struct ipv6hdr *)skb->data;
  306. saddr = &ip6h->saddr;
  307. daddr = &ip6h->daddr;
  308. net = dev_net(skb->dev);
  309. while ((sk = __raw_v6_lookup(net, sk, nexthdr, saddr, daddr,
  310. IP6CB(skb)->iif))) {
  311. rawv6_err(sk, skb, NULL, type, code,
  312. inner_offset, info);
  313. sk = sk_next(sk);
  314. }
  315. }
  316. read_unlock(&raw_v6_hashinfo.lock);
  317. }
  318. static inline int rawv6_rcv_skb(struct sock * sk, struct sk_buff * skb)
  319. {
  320. if ((raw6_sk(sk)->checksum || rcu_dereference_raw(sk->sk_filter)) &&
  321. skb_checksum_complete(skb)) {
  322. atomic_inc(&sk->sk_drops);
  323. kfree_skb(skb);
  324. return NET_RX_DROP;
  325. }
  326. /* Charge it to the socket. */
  327. if (ip_queue_rcv_skb(sk, skb) < 0) {
  328. kfree_skb(skb);
  329. return NET_RX_DROP;
  330. }
  331. return 0;
  332. }
  333. /*
  334. * This is next to useless...
  335. * if we demultiplex in network layer we don't need the extra call
  336. * just to queue the skb...
  337. * maybe we could have the network decide upon a hint if it
  338. * should call raw_rcv for demultiplexing
  339. */
  340. int rawv6_rcv(struct sock *sk, struct sk_buff *skb)
  341. {
  342. struct inet_sock *inet = inet_sk(sk);
  343. struct raw6_sock *rp = raw6_sk(sk);
  344. if (!xfrm6_policy_check(sk, XFRM_POLICY_IN, skb)) {
  345. atomic_inc(&sk->sk_drops);
  346. kfree_skb(skb);
  347. return NET_RX_DROP;
  348. }
  349. if (!rp->checksum)
  350. skb->ip_summed = CHECKSUM_UNNECESSARY;
  351. if (skb->ip_summed == CHECKSUM_COMPLETE) {
  352. skb_postpull_rcsum(skb, skb_network_header(skb),
  353. skb_network_header_len(skb));
  354. if (!csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
  355. &ipv6_hdr(skb)->daddr,
  356. skb->len, inet->inet_num, skb->csum))
  357. skb->ip_summed = CHECKSUM_UNNECESSARY;
  358. }
  359. if (!skb_csum_unnecessary(skb))
  360. skb->csum = ~csum_unfold(csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
  361. &ipv6_hdr(skb)->daddr,
  362. skb->len,
  363. inet->inet_num, 0));
  364. if (inet->hdrincl) {
  365. if (skb_checksum_complete(skb)) {
  366. atomic_inc(&sk->sk_drops);
  367. kfree_skb(skb);
  368. return NET_RX_DROP;
  369. }
  370. }
  371. rawv6_rcv_skb(sk, skb);
  372. return 0;
  373. }
  374. /*
  375. * This should be easy, if there is something there
  376. * we return it, otherwise we block.
  377. */
  378. static int rawv6_recvmsg(struct kiocb *iocb, struct sock *sk,
  379. struct msghdr *msg, size_t len,
  380. int noblock, int flags, int *addr_len)
  381. {
  382. struct ipv6_pinfo *np = inet6_sk(sk);
  383. struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)msg->msg_name;
  384. struct sk_buff *skb;
  385. size_t copied;
  386. int err;
  387. if (flags & MSG_OOB)
  388. return -EOPNOTSUPP;
  389. if (addr_len)
  390. *addr_len=sizeof(*sin6);
  391. if (flags & MSG_ERRQUEUE)
  392. return ipv6_recv_error(sk, msg, len);
  393. if (np->rxpmtu && np->rxopt.bits.rxpmtu)
  394. return ipv6_recv_rxpmtu(sk, msg, len);
  395. skb = skb_recv_datagram(sk, flags, noblock, &err);
  396. if (!skb)
  397. goto out;
  398. copied = skb->len;
  399. if (copied > len) {
  400. copied = len;
  401. msg->msg_flags |= MSG_TRUNC;
  402. }
  403. if (skb_csum_unnecessary(skb)) {
  404. err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
  405. } else if (msg->msg_flags&MSG_TRUNC) {
  406. if (__skb_checksum_complete(skb))
  407. goto csum_copy_err;
  408. err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
  409. } else {
  410. err = skb_copy_and_csum_datagram_iovec(skb, 0, msg->msg_iov);
  411. if (err == -EINVAL)
  412. goto csum_copy_err;
  413. }
  414. if (err)
  415. goto out_free;
  416. /* Copy the address. */
  417. if (sin6) {
  418. sin6->sin6_family = AF_INET6;
  419. sin6->sin6_port = 0;
  420. ipv6_addr_copy(&sin6->sin6_addr, &ipv6_hdr(skb)->saddr);
  421. sin6->sin6_flowinfo = 0;
  422. sin6->sin6_scope_id = 0;
  423. if (ipv6_addr_type(&sin6->sin6_addr) & IPV6_ADDR_LINKLOCAL)
  424. sin6->sin6_scope_id = IP6CB(skb)->iif;
  425. }
  426. sock_recv_ts_and_drops(msg, sk, skb);
  427. if (np->rxopt.all)
  428. datagram_recv_ctl(sk, msg, skb);
  429. err = copied;
  430. if (flags & MSG_TRUNC)
  431. err = skb->len;
  432. out_free:
  433. skb_free_datagram(sk, skb);
  434. out:
  435. return err;
  436. csum_copy_err:
  437. skb_kill_datagram(sk, skb, flags);
  438. /* Error for blocking case is chosen to masquerade
  439. as some normal condition.
  440. */
  441. err = (flags&MSG_DONTWAIT) ? -EAGAIN : -EHOSTUNREACH;
  442. goto out;
  443. }
  444. static int rawv6_push_pending_frames(struct sock *sk, struct flowi6 *fl6,
  445. struct raw6_sock *rp)
  446. {
  447. struct sk_buff *skb;
  448. int err = 0;
  449. int offset;
  450. int len;
  451. int total_len;
  452. __wsum tmp_csum;
  453. __sum16 csum;
  454. if (!rp->checksum)
  455. goto send;
  456. if ((skb = skb_peek(&sk->sk_write_queue)) == NULL)
  457. goto out;
  458. offset = rp->offset;
  459. total_len = inet_sk(sk)->cork.base.length - (skb_network_header(skb) -
  460. skb->data);
  461. if (offset >= total_len - 1) {
  462. err = -EINVAL;
  463. ip6_flush_pending_frames(sk);
  464. goto out;
  465. }
  466. /* should be check HW csum miyazawa */
  467. if (skb_queue_len(&sk->sk_write_queue) == 1) {
  468. /*
  469. * Only one fragment on the socket.
  470. */
  471. tmp_csum = skb->csum;
  472. } else {
  473. struct sk_buff *csum_skb = NULL;
  474. tmp_csum = 0;
  475. skb_queue_walk(&sk->sk_write_queue, skb) {
  476. tmp_csum = csum_add(tmp_csum, skb->csum);
  477. if (csum_skb)
  478. continue;
  479. len = skb->len - skb_transport_offset(skb);
  480. if (offset >= len) {
  481. offset -= len;
  482. continue;
  483. }
  484. csum_skb = skb;
  485. }
  486. skb = csum_skb;
  487. }
  488. offset += skb_transport_offset(skb);
  489. if (skb_copy_bits(skb, offset, &csum, 2))
  490. BUG();
  491. /* in case cksum was not initialized */
  492. if (unlikely(csum))
  493. tmp_csum = csum_sub(tmp_csum, csum_unfold(csum));
  494. csum = csum_ipv6_magic(&fl6->saddr, &fl6->daddr,
  495. total_len, fl6->flowi6_proto, tmp_csum);
  496. if (csum == 0 && fl6->flowi6_proto == IPPROTO_UDP)
  497. csum = CSUM_MANGLED_0;
  498. if (skb_store_bits(skb, offset, &csum, 2))
  499. BUG();
  500. send:
  501. err = ip6_push_pending_frames(sk);
  502. out:
  503. return err;
  504. }
  505. static int rawv6_send_hdrinc(struct sock *sk, void *from, int length,
  506. struct flowi6 *fl6, struct dst_entry **dstp,
  507. unsigned int flags)
  508. {
  509. struct ipv6_pinfo *np = inet6_sk(sk);
  510. struct ipv6hdr *iph;
  511. struct sk_buff *skb;
  512. int err;
  513. struct rt6_info *rt = (struct rt6_info *)*dstp;
  514. if (length > rt->dst.dev->mtu) {
  515. ipv6_local_error(sk, EMSGSIZE, fl6, rt->dst.dev->mtu);
  516. return -EMSGSIZE;
  517. }
  518. if (flags&MSG_PROBE)
  519. goto out;
  520. skb = sock_alloc_send_skb(sk,
  521. length + LL_ALLOCATED_SPACE(rt->dst.dev) + 15,
  522. flags & MSG_DONTWAIT, &err);
  523. if (skb == NULL)
  524. goto error;
  525. skb_reserve(skb, LL_RESERVED_SPACE(rt->dst.dev));
  526. skb->priority = sk->sk_priority;
  527. skb->mark = sk->sk_mark;
  528. skb_dst_set(skb, &rt->dst);
  529. *dstp = NULL;
  530. skb_put(skb, length);
  531. skb_reset_network_header(skb);
  532. iph = ipv6_hdr(skb);
  533. skb->ip_summed = CHECKSUM_NONE;
  534. skb->transport_header = skb->network_header;
  535. err = memcpy_fromiovecend((void *)iph, from, 0, length);
  536. if (err)
  537. goto error_fault;
  538. IP6_UPD_PO_STATS(sock_net(sk), rt->rt6i_idev, IPSTATS_MIB_OUT, skb->len);
  539. err = NF_HOOK(NFPROTO_IPV6, NF_INET_LOCAL_OUT, skb, NULL,
  540. rt->dst.dev, dst_output);
  541. if (err > 0)
  542. err = net_xmit_errno(err);
  543. if (err)
  544. goto error;
  545. out:
  546. return 0;
  547. error_fault:
  548. err = -EFAULT;
  549. kfree_skb(skb);
  550. error:
  551. IP6_INC_STATS(sock_net(sk), rt->rt6i_idev, IPSTATS_MIB_OUTDISCARDS);
  552. if (err == -ENOBUFS && !np->recverr)
  553. err = 0;
  554. return err;
  555. }
  556. static int rawv6_probe_proto_opt(struct flowi6 *fl6, struct msghdr *msg)
  557. {
  558. struct iovec *iov;
  559. u8 __user *type = NULL;
  560. u8 __user *code = NULL;
  561. u8 len = 0;
  562. int probed = 0;
  563. int i;
  564. if (!msg->msg_iov)
  565. return 0;
  566. for (i = 0; i < msg->msg_iovlen; i++) {
  567. iov = &msg->msg_iov[i];
  568. if (!iov)
  569. continue;
  570. switch (fl6->flowi6_proto) {
  571. case IPPROTO_ICMPV6:
  572. /* check if one-byte field is readable or not. */
  573. if (iov->iov_base && iov->iov_len < 1)
  574. break;
  575. if (!type) {
  576. type = iov->iov_base;
  577. /* check if code field is readable or not. */
  578. if (iov->iov_len > 1)
  579. code = type + 1;
  580. } else if (!code)
  581. code = iov->iov_base;
  582. if (type && code) {
  583. if (get_user(fl6->fl6_icmp_type, type) ||
  584. get_user(fl6->fl6_icmp_code, code))
  585. return -EFAULT;
  586. probed = 1;
  587. }
  588. break;
  589. case IPPROTO_MH:
  590. if (iov->iov_base && iov->iov_len < 1)
  591. break;
  592. /* check if type field is readable or not. */
  593. if (iov->iov_len > 2 - len) {
  594. u8 __user *p = iov->iov_base;
  595. if (get_user(fl6->fl6_mh_type, &p[2 - len]))
  596. return -EFAULT;
  597. probed = 1;
  598. } else
  599. len += iov->iov_len;
  600. break;
  601. default:
  602. probed = 1;
  603. break;
  604. }
  605. if (probed)
  606. break;
  607. }
  608. return 0;
  609. }
  610. static int rawv6_sendmsg(struct kiocb *iocb, struct sock *sk,
  611. struct msghdr *msg, size_t len)
  612. {
  613. struct ipv6_txoptions opt_space;
  614. struct sockaddr_in6 * sin6 = (struct sockaddr_in6 *) msg->msg_name;
  615. struct in6_addr *daddr, *final_p, final;
  616. struct inet_sock *inet = inet_sk(sk);
  617. struct ipv6_pinfo *np = inet6_sk(sk);
  618. struct raw6_sock *rp = raw6_sk(sk);
  619. struct ipv6_txoptions *opt = NULL;
  620. struct ip6_flowlabel *flowlabel = NULL;
  621. struct dst_entry *dst = NULL;
  622. struct flowi6 fl6;
  623. int addr_len = msg->msg_namelen;
  624. int hlimit = -1;
  625. int tclass = -1;
  626. int dontfrag = -1;
  627. u16 proto;
  628. int err;
  629. /* Rough check on arithmetic overflow,
  630. better check is made in ip6_append_data().
  631. */
  632. if (len > INT_MAX)
  633. return -EMSGSIZE;
  634. /* Mirror BSD error message compatibility */
  635. if (msg->msg_flags & MSG_OOB)
  636. return -EOPNOTSUPP;
  637. /*
  638. * Get and verify the address.
  639. */
  640. memset(&fl6, 0, sizeof(fl6));
  641. fl6.flowi6_mark = sk->sk_mark;
  642. if (sin6) {
  643. if (addr_len < SIN6_LEN_RFC2133)
  644. return -EINVAL;
  645. if (sin6->sin6_family && sin6->sin6_family != AF_INET6)
  646. return -EAFNOSUPPORT;
  647. /* port is the proto value [0..255] carried in nexthdr */
  648. proto = ntohs(sin6->sin6_port);
  649. if (!proto)
  650. proto = inet->inet_num;
  651. else if (proto != inet->inet_num)
  652. return -EINVAL;
  653. if (proto > 255)
  654. return -EINVAL;
  655. daddr = &sin6->sin6_addr;
  656. if (np->sndflow) {
  657. fl6.flowlabel = sin6->sin6_flowinfo&IPV6_FLOWINFO_MASK;
  658. if (fl6.flowlabel&IPV6_FLOWLABEL_MASK) {
  659. flowlabel = fl6_sock_lookup(sk, fl6.flowlabel);
  660. if (flowlabel == NULL)
  661. return -EINVAL;
  662. daddr = &flowlabel->dst;
  663. }
  664. }
  665. /*
  666. * Otherwise it will be difficult to maintain
  667. * sk->sk_dst_cache.
  668. */
  669. if (sk->sk_state == TCP_ESTABLISHED &&
  670. ipv6_addr_equal(daddr, &np->daddr))
  671. daddr = &np->daddr;
  672. if (addr_len >= sizeof(struct sockaddr_in6) &&
  673. sin6->sin6_scope_id &&
  674. ipv6_addr_type(daddr)&IPV6_ADDR_LINKLOCAL)
  675. fl6.flowi6_oif = sin6->sin6_scope_id;
  676. } else {
  677. if (sk->sk_state != TCP_ESTABLISHED)
  678. return -EDESTADDRREQ;
  679. proto = inet->inet_num;
  680. daddr = &np->daddr;
  681. fl6.flowlabel = np->flow_label;
  682. }
  683. if (fl6.flowi6_oif == 0)
  684. fl6.flowi6_oif = sk->sk_bound_dev_if;
  685. if (msg->msg_controllen) {
  686. opt = &opt_space;
  687. memset(opt, 0, sizeof(struct ipv6_txoptions));
  688. opt->tot_len = sizeof(struct ipv6_txoptions);
  689. err = datagram_send_ctl(sock_net(sk), msg, &fl6, opt, &hlimit,
  690. &tclass, &dontfrag);
  691. if (err < 0) {
  692. fl6_sock_release(flowlabel);
  693. return err;
  694. }
  695. if ((fl6.flowlabel&IPV6_FLOWLABEL_MASK) && !flowlabel) {
  696. flowlabel = fl6_sock_lookup(sk, fl6.flowlabel);
  697. if (flowlabel == NULL)
  698. return -EINVAL;
  699. }
  700. if (!(opt->opt_nflen|opt->opt_flen))
  701. opt = NULL;
  702. }
  703. if (opt == NULL)
  704. opt = np->opt;
  705. if (flowlabel)
  706. opt = fl6_merge_options(&opt_space, flowlabel, opt);
  707. opt = ipv6_fixup_options(&opt_space, opt);
  708. fl6.flowi6_proto = proto;
  709. err = rawv6_probe_proto_opt(&fl6, msg);
  710. if (err)
  711. goto out;
  712. if (!ipv6_addr_any(daddr))
  713. ipv6_addr_copy(&fl6.daddr, daddr);
  714. else
  715. fl6.daddr.s6_addr[15] = 0x1; /* :: means loopback (BSD'ism) */
  716. if (ipv6_addr_any(&fl6.saddr) && !ipv6_addr_any(&np->saddr))
  717. ipv6_addr_copy(&fl6.saddr, &np->saddr);
  718. final_p = fl6_update_dst(&fl6, opt, &final);
  719. if (!fl6.flowi6_oif && ipv6_addr_is_multicast(&fl6.daddr))
  720. fl6.flowi6_oif = np->mcast_oif;
  721. security_sk_classify_flow(sk, flowi6_to_flowi(&fl6));
  722. dst = ip6_dst_lookup_flow(sk, &fl6, final_p, true);
  723. if (IS_ERR(dst)) {
  724. err = PTR_ERR(dst);
  725. goto out;
  726. }
  727. if (hlimit < 0) {
  728. if (ipv6_addr_is_multicast(&fl6.daddr))
  729. hlimit = np->mcast_hops;
  730. else
  731. hlimit = np->hop_limit;
  732. if (hlimit < 0)
  733. hlimit = ip6_dst_hoplimit(dst);
  734. }
  735. if (tclass < 0)
  736. tclass = np->tclass;
  737. if (dontfrag < 0)
  738. dontfrag = np->dontfrag;
  739. if (msg->msg_flags&MSG_CONFIRM)
  740. goto do_confirm;
  741. back_from_confirm:
  742. if (inet->hdrincl)
  743. err = rawv6_send_hdrinc(sk, msg->msg_iov, len, &fl6, &dst, msg->msg_flags);
  744. else {
  745. lock_sock(sk);
  746. err = ip6_append_data(sk, ip_generic_getfrag, msg->msg_iov,
  747. len, 0, hlimit, tclass, opt, &fl6, (struct rt6_info*)dst,
  748. msg->msg_flags, dontfrag);
  749. if (err)
  750. ip6_flush_pending_frames(sk);
  751. else if (!(msg->msg_flags & MSG_MORE))
  752. err = rawv6_push_pending_frames(sk, &fl6, rp);
  753. release_sock(sk);
  754. }
  755. done:
  756. dst_release(dst);
  757. out:
  758. fl6_sock_release(flowlabel);
  759. return err<0?err:len;
  760. do_confirm:
  761. dst_confirm(dst);
  762. if (!(msg->msg_flags & MSG_PROBE) || len)
  763. goto back_from_confirm;
  764. err = 0;
  765. goto done;
  766. }
  767. static int rawv6_seticmpfilter(struct sock *sk, int level, int optname,
  768. char __user *optval, int optlen)
  769. {
  770. switch (optname) {
  771. case ICMPV6_FILTER:
  772. if (optlen > sizeof(struct icmp6_filter))
  773. optlen = sizeof(struct icmp6_filter);
  774. if (copy_from_user(&raw6_sk(sk)->filter, optval, optlen))
  775. return -EFAULT;
  776. return 0;
  777. default:
  778. return -ENOPROTOOPT;
  779. }
  780. return 0;
  781. }
  782. static int rawv6_geticmpfilter(struct sock *sk, int level, int optname,
  783. char __user *optval, int __user *optlen)
  784. {
  785. int len;
  786. switch (optname) {
  787. case ICMPV6_FILTER:
  788. if (get_user(len, optlen))
  789. return -EFAULT;
  790. if (len < 0)
  791. return -EINVAL;
  792. if (len > sizeof(struct icmp6_filter))
  793. len = sizeof(struct icmp6_filter);
  794. if (put_user(len, optlen))
  795. return -EFAULT;
  796. if (copy_to_user(optval, &raw6_sk(sk)->filter, len))
  797. return -EFAULT;
  798. return 0;
  799. default:
  800. return -ENOPROTOOPT;
  801. }
  802. return 0;
  803. }
  804. static int do_rawv6_setsockopt(struct sock *sk, int level, int optname,
  805. char __user *optval, unsigned int optlen)
  806. {
  807. struct raw6_sock *rp = raw6_sk(sk);
  808. int val;
  809. if (get_user(val, (int __user *)optval))
  810. return -EFAULT;
  811. switch (optname) {
  812. case IPV6_CHECKSUM:
  813. if (inet_sk(sk)->inet_num == IPPROTO_ICMPV6 &&
  814. level == IPPROTO_IPV6) {
  815. /*
  816. * RFC3542 tells that IPV6_CHECKSUM socket
  817. * option in the IPPROTO_IPV6 level is not
  818. * allowed on ICMPv6 sockets.
  819. * If you want to set it, use IPPROTO_RAW
  820. * level IPV6_CHECKSUM socket option
  821. * (Linux extension).
  822. */
  823. return -EINVAL;
  824. }
  825. /* You may get strange result with a positive odd offset;
  826. RFC2292bis agrees with me. */
  827. if (val > 0 && (val&1))
  828. return -EINVAL;
  829. if (val < 0) {
  830. rp->checksum = 0;
  831. } else {
  832. rp->checksum = 1;
  833. rp->offset = val;
  834. }
  835. return 0;
  836. break;
  837. default:
  838. return -ENOPROTOOPT;
  839. }
  840. }
  841. static int rawv6_setsockopt(struct sock *sk, int level, int optname,
  842. char __user *optval, unsigned int optlen)
  843. {
  844. switch(level) {
  845. case SOL_RAW:
  846. break;
  847. case SOL_ICMPV6:
  848. if (inet_sk(sk)->inet_num != IPPROTO_ICMPV6)
  849. return -EOPNOTSUPP;
  850. return rawv6_seticmpfilter(sk, level, optname, optval,
  851. optlen);
  852. case SOL_IPV6:
  853. if (optname == IPV6_CHECKSUM)
  854. break;
  855. default:
  856. return ipv6_setsockopt(sk, level, optname, optval,
  857. optlen);
  858. }
  859. return do_rawv6_setsockopt(sk, level, optname, optval, optlen);
  860. }
  861. #ifdef CONFIG_COMPAT
  862. static int compat_rawv6_setsockopt(struct sock *sk, int level, int optname,
  863. char __user *optval, unsigned int optlen)
  864. {
  865. switch (level) {
  866. case SOL_RAW:
  867. break;
  868. case SOL_ICMPV6:
  869. if (inet_sk(sk)->inet_num != IPPROTO_ICMPV6)
  870. return -EOPNOTSUPP;
  871. return rawv6_seticmpfilter(sk, level, optname, optval, optlen);
  872. case SOL_IPV6:
  873. if (optname == IPV6_CHECKSUM)
  874. break;
  875. default:
  876. return compat_ipv6_setsockopt(sk, level, optname,
  877. optval, optlen);
  878. }
  879. return do_rawv6_setsockopt(sk, level, optname, optval, optlen);
  880. }
  881. #endif
  882. static int do_rawv6_getsockopt(struct sock *sk, int level, int optname,
  883. char __user *optval, int __user *optlen)
  884. {
  885. struct raw6_sock *rp = raw6_sk(sk);
  886. int val, len;
  887. if (get_user(len,optlen))
  888. return -EFAULT;
  889. switch (optname) {
  890. case IPV6_CHECKSUM:
  891. /*
  892. * We allow getsockopt() for IPPROTO_IPV6-level
  893. * IPV6_CHECKSUM socket option on ICMPv6 sockets
  894. * since RFC3542 is silent about it.
  895. */
  896. if (rp->checksum == 0)
  897. val = -1;
  898. else
  899. val = rp->offset;
  900. break;
  901. default:
  902. return -ENOPROTOOPT;
  903. }
  904. len = min_t(unsigned int, sizeof(int), len);
  905. if (put_user(len, optlen))
  906. return -EFAULT;
  907. if (copy_to_user(optval,&val,len))
  908. return -EFAULT;
  909. return 0;
  910. }
  911. static int rawv6_getsockopt(struct sock *sk, int level, int optname,
  912. char __user *optval, int __user *optlen)
  913. {
  914. switch(level) {
  915. case SOL_RAW:
  916. break;
  917. case SOL_ICMPV6:
  918. if (inet_sk(sk)->inet_num != IPPROTO_ICMPV6)
  919. return -EOPNOTSUPP;
  920. return rawv6_geticmpfilter(sk, level, optname, optval,
  921. optlen);
  922. case SOL_IPV6:
  923. if (optname == IPV6_CHECKSUM)
  924. break;
  925. default:
  926. return ipv6_getsockopt(sk, level, optname, optval,
  927. optlen);
  928. }
  929. return do_rawv6_getsockopt(sk, level, optname, optval, optlen);
  930. }
  931. #ifdef CONFIG_COMPAT
  932. static int compat_rawv6_getsockopt(struct sock *sk, int level, int optname,
  933. char __user *optval, int __user *optlen)
  934. {
  935. switch (level) {
  936. case SOL_RAW:
  937. break;
  938. case SOL_ICMPV6:
  939. if (inet_sk(sk)->inet_num != IPPROTO_ICMPV6)
  940. return -EOPNOTSUPP;
  941. return rawv6_geticmpfilter(sk, level, optname, optval, optlen);
  942. case SOL_IPV6:
  943. if (optname == IPV6_CHECKSUM)
  944. break;
  945. default:
  946. return compat_ipv6_getsockopt(sk, level, optname,
  947. optval, optlen);
  948. }
  949. return do_rawv6_getsockopt(sk, level, optname, optval, optlen);
  950. }
  951. #endif
  952. static int rawv6_ioctl(struct sock *sk, int cmd, unsigned long arg)
  953. {
  954. switch(cmd) {
  955. case SIOCOUTQ:
  956. {
  957. int amount = sk_wmem_alloc_get(sk);
  958. return put_user(amount, (int __user *)arg);
  959. }
  960. case SIOCINQ:
  961. {
  962. struct sk_buff *skb;
  963. int amount = 0;
  964. spin_lock_bh(&sk->sk_receive_queue.lock);
  965. skb = skb_peek(&sk->sk_receive_queue);
  966. if (skb != NULL)
  967. amount = skb->tail - skb->transport_header;
  968. spin_unlock_bh(&sk->sk_receive_queue.lock);
  969. return put_user(amount, (int __user *)arg);
  970. }
  971. default:
  972. #ifdef CONFIG_IPV6_MROUTE
  973. return ip6mr_ioctl(sk, cmd, (void __user *)arg);
  974. #else
  975. return -ENOIOCTLCMD;
  976. #endif
  977. }
  978. }
  979. #ifdef CONFIG_COMPAT
  980. static int compat_rawv6_ioctl(struct sock *sk, unsigned int cmd, unsigned long arg)
  981. {
  982. switch (cmd) {
  983. case SIOCOUTQ:
  984. case SIOCINQ:
  985. return -ENOIOCTLCMD;
  986. default:
  987. #ifdef CONFIG_IPV6_MROUTE
  988. return ip6mr_compat_ioctl(sk, cmd, compat_ptr(arg));
  989. #else
  990. return -ENOIOCTLCMD;
  991. #endif
  992. }
  993. }
  994. #endif
  995. static void rawv6_close(struct sock *sk, long timeout)
  996. {
  997. if (inet_sk(sk)->inet_num == IPPROTO_RAW)
  998. ip6_ra_control(sk, -1);
  999. ip6mr_sk_done(sk);
  1000. sk_common_release(sk);
  1001. }
  1002. static void raw6_destroy(struct sock *sk)
  1003. {
  1004. lock_sock(sk);
  1005. ip6_flush_pending_frames(sk);
  1006. release_sock(sk);
  1007. inet6_destroy_sock(sk);
  1008. }
  1009. static int rawv6_init_sk(struct sock *sk)
  1010. {
  1011. struct raw6_sock *rp = raw6_sk(sk);
  1012. switch (inet_sk(sk)->inet_num) {
  1013. case IPPROTO_ICMPV6:
  1014. rp->checksum = 1;
  1015. rp->offset = 2;
  1016. break;
  1017. case IPPROTO_MH:
  1018. rp->checksum = 1;
  1019. rp->offset = 4;
  1020. break;
  1021. default:
  1022. break;
  1023. }
  1024. return 0;
  1025. }
  1026. struct proto rawv6_prot = {
  1027. .name = "RAWv6",
  1028. .owner = THIS_MODULE,
  1029. .close = rawv6_close,
  1030. .destroy = raw6_destroy,
  1031. .connect = ip6_datagram_connect,
  1032. .disconnect = udp_disconnect,
  1033. .ioctl = rawv6_ioctl,
  1034. .init = rawv6_init_sk,
  1035. .setsockopt = rawv6_setsockopt,
  1036. .getsockopt = rawv6_getsockopt,
  1037. .sendmsg = rawv6_sendmsg,
  1038. .recvmsg = rawv6_recvmsg,
  1039. .bind = rawv6_bind,
  1040. .backlog_rcv = rawv6_rcv_skb,
  1041. .hash = raw_hash_sk,
  1042. .unhash = raw_unhash_sk,
  1043. .obj_size = sizeof(struct raw6_sock),
  1044. .h.raw_hash = &raw_v6_hashinfo,
  1045. #ifdef CONFIG_COMPAT
  1046. .compat_setsockopt = compat_rawv6_setsockopt,
  1047. .compat_getsockopt = compat_rawv6_getsockopt,
  1048. .compat_ioctl = compat_rawv6_ioctl,
  1049. #endif
  1050. };
  1051. #ifdef CONFIG_PROC_FS
  1052. static void raw6_sock_seq_show(struct seq_file *seq, struct sock *sp, int i)
  1053. {
  1054. struct ipv6_pinfo *np = inet6_sk(sp);
  1055. const struct in6_addr *dest, *src;
  1056. __u16 destp, srcp;
  1057. dest = &np->daddr;
  1058. src = &np->rcv_saddr;
  1059. destp = 0;
  1060. srcp = inet_sk(sp)->inet_num;
  1061. seq_printf(seq,
  1062. "%4d: %08X%08X%08X%08X:%04X %08X%08X%08X%08X:%04X "
  1063. "%02X %08X:%08X %02X:%08lX %08X %5d %8d %lu %d %pK %d\n",
  1064. i,
  1065. src->s6_addr32[0], src->s6_addr32[1],
  1066. src->s6_addr32[2], src->s6_addr32[3], srcp,
  1067. dest->s6_addr32[0], dest->s6_addr32[1],
  1068. dest->s6_addr32[2], dest->s6_addr32[3], destp,
  1069. sp->sk_state,
  1070. sk_wmem_alloc_get(sp),
  1071. sk_rmem_alloc_get(sp),
  1072. 0, 0L, 0,
  1073. sock_i_uid(sp), 0,
  1074. sock_i_ino(sp),
  1075. atomic_read(&sp->sk_refcnt), sp, atomic_read(&sp->sk_drops));
  1076. }
  1077. static int raw6_seq_show(struct seq_file *seq, void *v)
  1078. {
  1079. if (v == SEQ_START_TOKEN)
  1080. seq_printf(seq,
  1081. " sl "
  1082. "local_address "
  1083. "remote_address "
  1084. "st tx_queue rx_queue tr tm->when retrnsmt"
  1085. " uid timeout inode ref pointer drops\n");
  1086. else
  1087. raw6_sock_seq_show(seq, v, raw_seq_private(seq)->bucket);
  1088. return 0;
  1089. }
  1090. static const struct seq_operations raw6_seq_ops = {
  1091. .start = raw_seq_start,
  1092. .next = raw_seq_next,
  1093. .stop = raw_seq_stop,
  1094. .show = raw6_seq_show,
  1095. };
  1096. static int raw6_seq_open(struct inode *inode, struct file *file)
  1097. {
  1098. return raw_seq_open(inode, file, &raw_v6_hashinfo, &raw6_seq_ops);
  1099. }
  1100. static const struct file_operations raw6_seq_fops = {
  1101. .owner = THIS_MODULE,
  1102. .open = raw6_seq_open,
  1103. .read = seq_read,
  1104. .llseek = seq_lseek,
  1105. .release = seq_release_net,
  1106. };
  1107. static int __net_init raw6_init_net(struct net *net)
  1108. {
  1109. if (!proc_net_fops_create(net, "raw6", S_IRUGO, &raw6_seq_fops))
  1110. return -ENOMEM;
  1111. return 0;
  1112. }
  1113. static void __net_exit raw6_exit_net(struct net *net)
  1114. {
  1115. proc_net_remove(net, "raw6");
  1116. }
  1117. static struct pernet_operations raw6_net_ops = {
  1118. .init = raw6_init_net,
  1119. .exit = raw6_exit_net,
  1120. };
  1121. int __init raw6_proc_init(void)
  1122. {
  1123. return register_pernet_subsys(&raw6_net_ops);
  1124. }
  1125. void raw6_proc_exit(void)
  1126. {
  1127. unregister_pernet_subsys(&raw6_net_ops);
  1128. }
  1129. #endif /* CONFIG_PROC_FS */
  1130. /* Same as inet6_dgram_ops, sans udp_poll. */
  1131. static const struct proto_ops inet6_sockraw_ops = {
  1132. .family = PF_INET6,
  1133. .owner = THIS_MODULE,
  1134. .release = inet6_release,
  1135. .bind = inet6_bind,
  1136. .connect = inet_dgram_connect, /* ok */
  1137. .socketpair = sock_no_socketpair, /* a do nothing */
  1138. .accept = sock_no_accept, /* a do nothing */
  1139. .getname = inet6_getname,
  1140. .poll = datagram_poll, /* ok */
  1141. .ioctl = inet6_ioctl, /* must change */
  1142. .listen = sock_no_listen, /* ok */
  1143. .shutdown = inet_shutdown, /* ok */
  1144. .setsockopt = sock_common_setsockopt, /* ok */
  1145. .getsockopt = sock_common_getsockopt, /* ok */
  1146. .sendmsg = inet_sendmsg, /* ok */
  1147. .recvmsg = sock_common_recvmsg, /* ok */
  1148. .mmap = sock_no_mmap,
  1149. .sendpage = sock_no_sendpage,
  1150. #ifdef CONFIG_COMPAT
  1151. .compat_setsockopt = compat_sock_common_setsockopt,
  1152. .compat_getsockopt = compat_sock_common_getsockopt,
  1153. #endif
  1154. };
  1155. static struct inet_protosw rawv6_protosw = {
  1156. .type = SOCK_RAW,
  1157. .protocol = IPPROTO_IP, /* wild card */
  1158. .prot = &rawv6_prot,
  1159. .ops = &inet6_sockraw_ops,
  1160. .no_check = UDP_CSUM_DEFAULT,
  1161. .flags = INET_PROTOSW_REUSE,
  1162. };
  1163. int __init rawv6_init(void)
  1164. {
  1165. int ret;
  1166. ret = inet6_register_protosw(&rawv6_protosw);
  1167. if (ret)
  1168. goto out;
  1169. out:
  1170. return ret;
  1171. }
  1172. void rawv6_exit(void)
  1173. {
  1174. inet6_unregister_protosw(&rawv6_protosw);
  1175. }