raw.c 31 KB

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