ipv6_sockglue.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:11k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  1. /*
  2.  * IPv6 BSD socket options interface
  3.  * Linux INET6 implementation 
  4.  *
  5.  * Authors:
  6.  * Pedro Roque <roque@di.fc.ul.pt>
  7.  *
  8.  * Based on linux/net/ipv4/ip_sockglue.c
  9.  *
  10.  * $Id: ipv6_sockglue.c,v 1.40 2001/09/18 22:29:10 davem Exp $
  11.  *
  12.  * This program is free software; you can redistribute it and/or
  13.  *      modify it under the terms of the GNU General Public License
  14.  *      as published by the Free Software Foundation; either version
  15.  *      2 of the License, or (at your option) any later version.
  16.  *
  17.  * FIXME: Make the setsockopt code POSIX compliant: That is
  18.  *
  19.  * o Return -EINVAL for setsockopt of short lengths
  20.  * o Truncate getsockopt returns
  21.  * o Return an optlen of the truncated length if need be
  22.  */
  23. #define __NO_VERSION__
  24. #include <linux/module.h>
  25. #include <linux/config.h>
  26. #include <linux/errno.h>
  27. #include <linux/types.h>
  28. #include <linux/socket.h>
  29. #include <linux/sockios.h>
  30. #include <linux/sched.h>
  31. #include <linux/net.h>
  32. #include <linux/in6.h>
  33. #include <linux/netdevice.h>
  34. #include <linux/if_arp.h>
  35. #include <linux/init.h>
  36. #include <linux/sysctl.h>
  37. #include <linux/netfilter.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/transp_v6.h>
  44. #include <net/ip6_route.h>
  45. #include <net/addrconf.h>
  46. #include <net/inet_common.h>
  47. #include <net/tcp.h>
  48. #include <net/udp.h>
  49. #include <asm/uaccess.h>
  50. struct ipv6_mib ipv6_statistics[NR_CPUS*2];
  51. struct packet_type ipv6_packet_type =
  52. {
  53. __constant_htons(ETH_P_IPV6), 
  54. NULL, /* All devices */
  55. ipv6_rcv,
  56. (void*)1,
  57. NULL
  58. };
  59. /*
  60.  * addrconf module should be notifyed of a device going up
  61.  */
  62. static struct notifier_block ipv6_dev_notf = {
  63. addrconf_notify,
  64. NULL,
  65. 0
  66. };
  67. struct ip6_ra_chain *ip6_ra_chain;
  68. rwlock_t ip6_ra_lock = RW_LOCK_UNLOCKED;
  69. int ip6_ra_control(struct sock *sk, int sel, void (*destructor)(struct sock *))
  70. {
  71. struct ip6_ra_chain *ra, *new_ra, **rap;
  72. /* RA packet may be delivered ONLY to IPPROTO_RAW socket */
  73. if (sk->type != SOCK_RAW || sk->num != IPPROTO_RAW)
  74. return -EINVAL;
  75. new_ra = (sel>=0) ? kmalloc(sizeof(*new_ra), GFP_KERNEL) : NULL;
  76. write_lock_bh(&ip6_ra_lock);
  77. for (rap = &ip6_ra_chain; (ra=*rap) != NULL; rap = &ra->next) {
  78. if (ra->sk == sk) {
  79. if (sel>=0) {
  80. write_unlock_bh(&ip6_ra_lock);
  81. if (new_ra)
  82. kfree(new_ra);
  83. return -EADDRINUSE;
  84. }
  85. *rap = ra->next;
  86. write_unlock_bh(&ip6_ra_lock);
  87. if (ra->destructor)
  88. ra->destructor(sk);
  89. sock_put(sk);
  90. kfree(ra);
  91. return 0;
  92. }
  93. }
  94. if (new_ra == NULL) {
  95. write_unlock_bh(&ip6_ra_lock);
  96. return -ENOBUFS;
  97. }
  98. new_ra->sk = sk;
  99. new_ra->sel = sel;
  100. new_ra->destructor = destructor;
  101. new_ra->next = ra;
  102. *rap = new_ra;
  103. sock_hold(sk);
  104. write_unlock_bh(&ip6_ra_lock);
  105. return 0;
  106. }
  107. int ipv6_setsockopt(struct sock *sk, int level, int optname, char *optval, 
  108.     int optlen)
  109. {
  110. struct ipv6_pinfo *np = &sk->net_pinfo.af_inet6;
  111. int val, valbool;
  112. int retv = -ENOPROTOOPT;
  113. if(level==SOL_IP && sk->type != SOCK_RAW)
  114. return udp_prot.setsockopt(sk, level, optname, optval, optlen);
  115. if(level!=SOL_IPV6)
  116. goto out;
  117. if (optval == NULL)
  118. val=0;
  119. else if (get_user(val, (int *) optval))
  120. return -EFAULT;
  121. valbool = (val!=0);
  122. lock_sock(sk);
  123. switch (optname) {
  124. case IPV6_ADDRFORM:
  125. if (val == PF_INET) {
  126. struct ipv6_txoptions *opt;
  127. struct sk_buff *pktopt;
  128. if (sk->protocol != IPPROTO_UDP &&
  129.     sk->protocol != IPPROTO_TCP)
  130. break;
  131. if (sk->state != TCP_ESTABLISHED) {
  132. retv = -ENOTCONN;
  133. break;
  134. }
  135. if (!(ipv6_addr_type(&np->daddr) & IPV6_ADDR_MAPPED)) {
  136. retv = -EADDRNOTAVAIL;
  137. break;
  138. }
  139. fl6_free_socklist(sk);
  140. ipv6_sock_mc_close(sk);
  141. if (sk->protocol == IPPROTO_TCP) {
  142. struct tcp_opt *tp = &(sk->tp_pinfo.af_tcp);
  143. local_bh_disable();
  144. sock_prot_dec_use(sk->prot);
  145. sock_prot_inc_use(&tcp_prot);
  146. local_bh_enable();
  147. sk->prot = &tcp_prot;
  148. tp->af_specific = &ipv4_specific;
  149. sk->socket->ops = &inet_stream_ops;
  150. sk->family = PF_INET;
  151. tcp_sync_mss(sk, tp->pmtu_cookie);
  152. } else {
  153. local_bh_disable();
  154. sock_prot_dec_use(sk->prot);
  155. sock_prot_inc_use(&udp_prot);
  156. local_bh_enable();
  157. sk->prot = &udp_prot;
  158. sk->socket->ops = &inet_dgram_ops;
  159. sk->family = PF_INET;
  160. }
  161. opt = xchg(&np->opt, NULL);
  162. if (opt)
  163. sock_kfree_s(sk, opt, opt->tot_len);
  164. pktopt = xchg(&np->pktoptions, NULL);
  165. if (pktopt)
  166. kfree_skb(pktopt);
  167. sk->destruct = inet_sock_destruct;
  168. #ifdef INET_REFCNT_DEBUG
  169. atomic_dec(&inet6_sock_nr);
  170. #endif
  171. MOD_DEC_USE_COUNT;
  172. retv = 0;
  173. break;
  174. }
  175. goto e_inval;
  176. case IPV6_PKTINFO:
  177. np->rxopt.bits.rxinfo = valbool;
  178. retv = 0;
  179. break;
  180. case IPV6_HOPLIMIT:
  181. np->rxopt.bits.rxhlim = valbool;
  182. retv = 0;
  183. break;
  184. case IPV6_RTHDR:
  185. if (val < 0 || val > 2)
  186. goto e_inval;
  187. np->rxopt.bits.srcrt = val;
  188. retv = 0;
  189. break;
  190. case IPV6_HOPOPTS:
  191. np->rxopt.bits.hopopts = valbool;
  192. retv = 0;
  193. break;
  194. case IPV6_AUTHHDR:
  195. np->rxopt.bits.authhdr = valbool;
  196. retv = 0;
  197. break;
  198. case IPV6_DSTOPTS:
  199. np->rxopt.bits.dstopts = valbool;
  200. retv = 0;
  201. break;
  202. case IPV6_FLOWINFO:
  203. np->rxopt.bits.rxflow = valbool;
  204. retv = 0;
  205. break;
  206. case IPV6_PKTOPTIONS:
  207. {
  208. struct ipv6_txoptions *opt = NULL;
  209. struct msghdr msg;
  210. struct flowi fl;
  211. int junk;
  212. fl.fl6_flowlabel = 0;
  213. fl.oif = sk->bound_dev_if;
  214. if (optlen == 0)
  215. goto update;
  216. /* 1K is probably excessive
  217.  * 1K is surely not enough, 2K per standard header is 16K.
  218.  */
  219. retv = -EINVAL;
  220. if (optlen > 64*1024)
  221. break;
  222. opt = sock_kmalloc(sk, sizeof(*opt) + optlen, GFP_KERNEL);
  223. retv = -ENOBUFS;
  224. if (opt == NULL)
  225. break;
  226. memset(opt, 0, sizeof(*opt));
  227. opt->tot_len = sizeof(*opt) + optlen;
  228. retv = -EFAULT;
  229. if (copy_from_user(opt+1, optval, optlen))
  230. goto done;
  231. msg.msg_controllen = optlen;
  232. msg.msg_control = (void*)(opt+1);
  233. retv = datagram_send_ctl(&msg, &fl, opt, &junk);
  234. if (retv)
  235. goto done;
  236. update:
  237. retv = 0;
  238. if (sk->type == SOCK_STREAM) {
  239. if (opt) {
  240. struct tcp_opt *tp = &sk->tp_pinfo.af_tcp;
  241. if (!((1<<sk->state)&(TCPF_LISTEN|TCPF_CLOSE))
  242.     && sk->daddr != LOOPBACK4_IPV6) {
  243. tp->ext_header_len = opt->opt_flen + opt->opt_nflen;
  244. tcp_sync_mss(sk, tp->pmtu_cookie);
  245. }
  246. }
  247. opt = xchg(&np->opt, opt);
  248. sk_dst_reset(sk);
  249. } else {
  250. write_lock(&sk->dst_lock);
  251. opt = xchg(&np->opt, opt);
  252. write_unlock(&sk->dst_lock);
  253. sk_dst_reset(sk);
  254. }
  255. done:
  256. if (opt)
  257. sock_kfree_s(sk, opt, opt->tot_len);
  258. break;
  259. }
  260. case IPV6_UNICAST_HOPS:
  261. if (val > 255 || val < -1)
  262. goto e_inval;
  263. np->hop_limit = val;
  264. retv = 0;
  265. break;
  266. case IPV6_MULTICAST_HOPS:
  267. if (sk->type == SOCK_STREAM)
  268. goto e_inval;
  269. if (val > 255 || val < -1)
  270. goto e_inval;
  271. np->mcast_hops = val;
  272. retv = 0;
  273. break;
  274. case IPV6_MULTICAST_LOOP:
  275. np->mc_loop = valbool;
  276. retv = 0;
  277. break;
  278. case IPV6_MULTICAST_IF:
  279. if (sk->type == SOCK_STREAM)
  280. goto e_inval;
  281. if (sk->bound_dev_if && sk->bound_dev_if != val)
  282. goto e_inval;
  283. if (__dev_get_by_index(val) == NULL) {
  284. retv = -ENODEV;
  285. break;
  286. }
  287. np->mcast_oif = val;
  288. retv = 0;
  289. break;
  290. case IPV6_ADD_MEMBERSHIP:
  291. case IPV6_DROP_MEMBERSHIP:
  292. {
  293. struct ipv6_mreq mreq;
  294. retv = -EFAULT;
  295. if (copy_from_user(&mreq, optval, sizeof(struct ipv6_mreq)))
  296. break;
  297. if (optname == IPV6_ADD_MEMBERSHIP)
  298. retv = ipv6_sock_mc_join(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_multiaddr);
  299. else
  300. retv = ipv6_sock_mc_drop(sk, mreq.ipv6mr_ifindex, &mreq.ipv6mr_multiaddr);
  301. break;
  302. }
  303. case IPV6_ROUTER_ALERT:
  304. retv = ip6_ra_control(sk, val, NULL);
  305. break;
  306. case IPV6_MTU_DISCOVER:
  307. if (val<0 || val>2)
  308. goto e_inval;
  309. np->pmtudisc = val;
  310. retv = 0;
  311. break;
  312. case IPV6_MTU:
  313. if (val && val < IPV6_MIN_MTU)
  314. goto e_inval;
  315. np->frag_size = val;
  316. retv = 0;
  317. break;
  318. case IPV6_RECVERR:
  319. np->recverr = valbool;
  320. if (!val)
  321. skb_queue_purge(&sk->error_queue);
  322. retv = 0;
  323. break;
  324. case IPV6_FLOWINFO_SEND:
  325. np->sndflow = valbool;
  326. retv = 0;
  327. break;
  328. case IPV6_FLOWLABEL_MGR:
  329. retv = ipv6_flowlabel_opt(sk, optval, optlen);
  330. break;
  331. #ifdef CONFIG_NETFILTER
  332. default:
  333. retv = nf_setsockopt(sk, PF_INET6, optname, optval, 
  334.     optlen);
  335. break;
  336. #endif
  337. }
  338. release_sock(sk);
  339. out:
  340. return retv;
  341. e_inval:
  342. release_sock(sk);
  343. return -EINVAL;
  344. }
  345. int ipv6_getsockopt(struct sock *sk, int level, int optname, char *optval, 
  346.     int *optlen)
  347. {
  348. struct ipv6_pinfo *np = &sk->net_pinfo.af_inet6;
  349. int len;
  350. int val;
  351. if(level==SOL_IP && sk->type != SOCK_RAW)
  352. return udp_prot.getsockopt(sk, level, optname, optval, optlen);
  353. if(level!=SOL_IPV6)
  354. return -ENOPROTOOPT;
  355. if (get_user(len, optlen))
  356. return -EFAULT;
  357. switch (optname) {
  358. case IPV6_PKTOPTIONS:
  359. {
  360. struct msghdr msg;
  361. struct sk_buff *skb;
  362. if (sk->type != SOCK_STREAM)
  363. return -ENOPROTOOPT;
  364. msg.msg_control = optval;
  365. msg.msg_controllen = len;
  366. msg.msg_flags = 0;
  367. lock_sock(sk);
  368. skb = np->pktoptions;
  369. if (skb)
  370. atomic_inc(&skb->users);
  371. release_sock(sk);
  372. if (skb) {
  373. int err = datagram_recv_ctl(sk, &msg, skb);
  374. kfree_skb(skb);
  375. if (err)
  376. return err;
  377. } else {
  378. if (np->rxopt.bits.rxinfo) {
  379. struct in6_pktinfo src_info;
  380. src_info.ipi6_ifindex = np->mcast_oif;
  381. ipv6_addr_copy(&src_info.ipi6_addr, &np->daddr);
  382. put_cmsg(&msg, SOL_IPV6, IPV6_PKTINFO, sizeof(src_info), &src_info);
  383. }
  384. if (np->rxopt.bits.rxhlim) {
  385. int hlim = np->mcast_hops;
  386. put_cmsg(&msg, SOL_IPV6, IPV6_HOPLIMIT, sizeof(hlim), &hlim);
  387. }
  388. }
  389. len -= msg.msg_controllen;
  390. return put_user(len, optlen);
  391. }
  392. case IPV6_MTU:
  393. {
  394. struct dst_entry *dst;
  395. val = 0;
  396. lock_sock(sk);
  397. dst = sk_dst_get(sk);
  398. if (dst) {
  399. val = dst->pmtu;
  400. dst_release(dst);
  401. }
  402. release_sock(sk);
  403. if (!val)
  404. return -ENOTCONN;
  405. break;
  406. }
  407. case IPV6_PKTINFO:
  408. val = np->rxopt.bits.rxinfo;
  409. break;
  410. case IPV6_HOPLIMIT:
  411. val = np->rxopt.bits.rxhlim;
  412. break;
  413. case IPV6_RTHDR:
  414. val = np->rxopt.bits.srcrt;
  415. break;
  416. case IPV6_HOPOPTS:
  417. val = np->rxopt.bits.hopopts;
  418. break;
  419. case IPV6_AUTHHDR:
  420. val = np->rxopt.bits.authhdr;
  421. break;
  422. case IPV6_DSTOPTS:
  423. val = np->rxopt.bits.dstopts;
  424. break;
  425. case IPV6_FLOWINFO:
  426. val = np->rxopt.bits.rxflow;
  427. break;
  428. case IPV6_UNICAST_HOPS:
  429. val = np->hop_limit;
  430. break;
  431. case IPV6_MULTICAST_HOPS:
  432. val = np->mcast_hops;
  433. break;
  434. case IPV6_MULTICAST_LOOP:
  435. val = np->mc_loop;
  436. break;
  437. case IPV6_MULTICAST_IF:
  438. val = np->mcast_oif;
  439. break;
  440. case IPV6_MTU_DISCOVER:
  441. val = np->pmtudisc;
  442. break;
  443. case IPV6_RECVERR:
  444. val = np->recverr;
  445. break;
  446. case IPV6_FLOWINFO_SEND:
  447. val = np->sndflow;
  448. break;
  449. default:
  450. #ifdef CONFIG_NETFILTER
  451. lock_sock(sk);
  452. val = nf_getsockopt(sk, PF_INET6, optname, optval, 
  453.     &len);
  454. release_sock(sk);
  455. if (val >= 0)
  456. val = put_user(len, optlen);
  457. return val;
  458. #else
  459. return -EINVAL;
  460. #endif
  461. }
  462. len = min_t(unsigned int, sizeof(int), len);
  463. if(put_user(len, optlen))
  464. return -EFAULT;
  465. if(copy_to_user(optval,&val,len))
  466. return -EFAULT;
  467. return 0;
  468. }
  469. #if defined(MODULE) && defined(CONFIG_SYSCTL)
  470. /*
  471.  * sysctl registration functions defined in sysctl_net_ipv6.c
  472.  */
  473. extern void ipv6_sysctl_register(void);
  474. extern void ipv6_sysctl_unregister(void);
  475. #endif
  476. void __init ipv6_packet_init(void)
  477. {
  478. dev_add_pack(&ipv6_packet_type);
  479. }
  480. void __init ipv6_netdev_notif_init(void)
  481. {
  482. register_netdevice_notifier(&ipv6_dev_notf);
  483. }
  484. #ifdef MODULE
  485. void ipv6_packet_cleanup(void)
  486. {
  487. dev_remove_pack(&ipv6_packet_type);
  488. }
  489. void ipv6_netdev_notif_cleanup(void)
  490. {
  491. unregister_netdevice_notifier(&ipv6_dev_notf);
  492. }
  493. #endif