ipv6.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043
  1. /* SCTP kernel reference Implementation
  2. * (C) Copyright IBM Corp. 2002, 2004
  3. * Copyright (c) 2001 Nokia, Inc.
  4. * Copyright (c) 2001 La Monte H.P. Yarroll
  5. * Copyright (c) 2002-2003 Intel Corp.
  6. *
  7. * This file is part of the SCTP kernel reference Implementation
  8. *
  9. * SCTP over IPv6.
  10. *
  11. * The SCTP reference implementation is free software;
  12. * you can redistribute it and/or modify it under the terms of
  13. * the GNU General Public License as published by
  14. * the Free Software Foundation; either version 2, or (at your option)
  15. * any later version.
  16. *
  17. * The SCTP reference implementation is distributed in the hope that it
  18. * will be useful, but WITHOUT ANY WARRANTY; without even the implied
  19. * ************************
  20. * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  21. * See the GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with GNU CC; see the file COPYING. If not, write to
  25. * the Free Software Foundation, 59 Temple Place - Suite 330,
  26. * Boston, MA 02111-1307, USA.
  27. *
  28. * Please send any bug reports or fixes you make to the
  29. * email address(es):
  30. * lksctp developers <lksctp-developers@lists.sourceforge.net>
  31. *
  32. * Or submit a bug report through the following website:
  33. * http://www.sf.net/projects/lksctp
  34. *
  35. * Written or modified by:
  36. * Le Yanqun <yanqun.le@nokia.com>
  37. * Hui Huang <hui.huang@nokia.com>
  38. * La Monte H.P. Yarroll <piggy@acm.org>
  39. * Sridhar Samudrala <sri@us.ibm.com>
  40. * Jon Grimm <jgrimm@us.ibm.com>
  41. * Ardelle Fan <ardelle.fan@intel.com>
  42. *
  43. * Based on:
  44. * linux/net/ipv6/tcp_ipv6.c
  45. *
  46. * Any bugs reported given to us we will try to fix... any fixes shared will
  47. * be incorporated into the next SCTP release.
  48. */
  49. #include <linux/module.h>
  50. #include <linux/errno.h>
  51. #include <linux/types.h>
  52. #include <linux/socket.h>
  53. #include <linux/sockios.h>
  54. #include <linux/net.h>
  55. #include <linux/in.h>
  56. #include <linux/in6.h>
  57. #include <linux/netdevice.h>
  58. #include <linux/init.h>
  59. #include <linux/ipsec.h>
  60. #include <linux/ipv6.h>
  61. #include <linux/icmpv6.h>
  62. #include <linux/random.h>
  63. #include <linux/seq_file.h>
  64. #include <net/protocol.h>
  65. #include <net/ndisc.h>
  66. #include <net/ip.h>
  67. #include <net/ipv6.h>
  68. #include <net/transp_v6.h>
  69. #include <net/addrconf.h>
  70. #include <net/ip6_route.h>
  71. #include <net/inet_common.h>
  72. #include <net/inet_ecn.h>
  73. #include <net/sctp/sctp.h>
  74. #include <asm/uaccess.h>
  75. /* Event handler for inet6 address addition/deletion events. */
  76. static int sctp_inet6addr_event(struct notifier_block *this, unsigned long ev,
  77. void *ptr)
  78. {
  79. struct inet6_ifaddr *ifa = (struct inet6_ifaddr *)ptr;
  80. struct sctp_sockaddr_entry *addr;
  81. struct list_head *pos, *temp;
  82. switch (ev) {
  83. case NETDEV_UP:
  84. addr = kmalloc(sizeof(struct sctp_sockaddr_entry), GFP_ATOMIC);
  85. if (addr) {
  86. addr->a.v6.sin6_family = AF_INET6;
  87. addr->a.v6.sin6_port = 0;
  88. memcpy(&addr->a.v6.sin6_addr, &ifa->addr,
  89. sizeof(struct in6_addr));
  90. addr->a.v6.sin6_scope_id = ifa->idev->dev->ifindex;
  91. list_add_tail(&addr->list, &sctp_local_addr_list);
  92. }
  93. break;
  94. case NETDEV_DOWN:
  95. list_for_each_safe(pos, temp, &sctp_local_addr_list) {
  96. addr = list_entry(pos, struct sctp_sockaddr_entry, list);
  97. if (ipv6_addr_equal(&addr->a.v6.sin6_addr, &ifa->addr)) {
  98. list_del(pos);
  99. kfree(addr);
  100. break;
  101. }
  102. }
  103. break;
  104. }
  105. return NOTIFY_DONE;
  106. }
  107. static struct notifier_block sctp_inet6addr_notifier = {
  108. .notifier_call = sctp_inet6addr_event,
  109. };
  110. /* ICMP error handler. */
  111. SCTP_STATIC void sctp_v6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
  112. int type, int code, int offset, __be32 info)
  113. {
  114. struct inet6_dev *idev;
  115. struct sock *sk;
  116. struct sctp_association *asoc;
  117. struct sctp_transport *transport;
  118. struct ipv6_pinfo *np;
  119. sk_buff_data_t saveip, savesctp;
  120. int err;
  121. idev = in6_dev_get(skb->dev);
  122. /* Fix up skb to look at the embedded net header. */
  123. saveip = skb->network_header;
  124. savesctp = skb->transport_header;
  125. skb_reset_network_header(skb);
  126. skb_set_transport_header(skb, offset);
  127. sk = sctp_err_lookup(AF_INET6, skb, sctp_hdr(skb), &asoc, &transport);
  128. /* Put back, the original pointers. */
  129. skb->network_header = saveip;
  130. skb->transport_header = savesctp;
  131. if (!sk) {
  132. ICMP6_INC_STATS_BH(idev, ICMP6_MIB_INERRORS);
  133. goto out;
  134. }
  135. /* Warning: The sock lock is held. Remember to call
  136. * sctp_err_finish!
  137. */
  138. switch (type) {
  139. case ICMPV6_PKT_TOOBIG:
  140. sctp_icmp_frag_needed(sk, asoc, transport, ntohl(info));
  141. goto out_unlock;
  142. case ICMPV6_PARAMPROB:
  143. if (ICMPV6_UNK_NEXTHDR == code) {
  144. sctp_icmp_proto_unreachable(sk, asoc, transport);
  145. goto out_unlock;
  146. }
  147. break;
  148. default:
  149. break;
  150. }
  151. np = inet6_sk(sk);
  152. icmpv6_err_convert(type, code, &err);
  153. if (!sock_owned_by_user(sk) && np->recverr) {
  154. sk->sk_err = err;
  155. sk->sk_error_report(sk);
  156. } else { /* Only an error on timeout */
  157. sk->sk_err_soft = err;
  158. }
  159. out_unlock:
  160. sctp_err_finish(sk, asoc);
  161. out:
  162. if (likely(idev != NULL))
  163. in6_dev_put(idev);
  164. }
  165. /* Based on tcp_v6_xmit() in tcp_ipv6.c. */
  166. static int sctp_v6_xmit(struct sk_buff *skb, struct sctp_transport *transport,
  167. int ipfragok)
  168. {
  169. struct sock *sk = skb->sk;
  170. struct ipv6_pinfo *np = inet6_sk(sk);
  171. struct flowi fl;
  172. memset(&fl, 0, sizeof(fl));
  173. fl.proto = sk->sk_protocol;
  174. /* Fill in the dest address from the route entry passed with the skb
  175. * and the source address from the transport.
  176. */
  177. ipv6_addr_copy(&fl.fl6_dst, &transport->ipaddr.v6.sin6_addr);
  178. ipv6_addr_copy(&fl.fl6_src, &transport->saddr.v6.sin6_addr);
  179. fl.fl6_flowlabel = np->flow_label;
  180. IP6_ECN_flow_xmit(sk, fl.fl6_flowlabel);
  181. if (ipv6_addr_type(&fl.fl6_src) & IPV6_ADDR_LINKLOCAL)
  182. fl.oif = transport->saddr.v6.sin6_scope_id;
  183. else
  184. fl.oif = sk->sk_bound_dev_if;
  185. if (np->opt && np->opt->srcrt) {
  186. struct rt0_hdr *rt0 = (struct rt0_hdr *) np->opt->srcrt;
  187. ipv6_addr_copy(&fl.fl6_dst, rt0->addr);
  188. }
  189. SCTP_DEBUG_PRINTK("%s: skb:%p, len:%d, "
  190. "src:" NIP6_FMT " dst:" NIP6_FMT "\n",
  191. __FUNCTION__, skb, skb->len,
  192. NIP6(fl.fl6_src), NIP6(fl.fl6_dst));
  193. SCTP_INC_STATS(SCTP_MIB_OUTSCTPPACKS);
  194. return ip6_xmit(sk, skb, &fl, np->opt, ipfragok);
  195. }
  196. /* Returns the dst cache entry for the given source and destination ip
  197. * addresses.
  198. */
  199. static struct dst_entry *sctp_v6_get_dst(struct sctp_association *asoc,
  200. union sctp_addr *daddr,
  201. union sctp_addr *saddr)
  202. {
  203. struct dst_entry *dst;
  204. struct flowi fl;
  205. memset(&fl, 0, sizeof(fl));
  206. ipv6_addr_copy(&fl.fl6_dst, &daddr->v6.sin6_addr);
  207. if (ipv6_addr_type(&daddr->v6.sin6_addr) & IPV6_ADDR_LINKLOCAL)
  208. fl.oif = daddr->v6.sin6_scope_id;
  209. SCTP_DEBUG_PRINTK("%s: DST=" NIP6_FMT " ",
  210. __FUNCTION__, NIP6(fl.fl6_dst));
  211. if (saddr) {
  212. ipv6_addr_copy(&fl.fl6_src, &saddr->v6.sin6_addr);
  213. SCTP_DEBUG_PRINTK(
  214. "SRC=" NIP6_FMT " - ",
  215. NIP6(fl.fl6_src));
  216. }
  217. dst = ip6_route_output(NULL, &fl);
  218. if (!dst->error) {
  219. struct rt6_info *rt;
  220. rt = (struct rt6_info *)dst;
  221. SCTP_DEBUG_PRINTK(
  222. "rt6_dst:" NIP6_FMT " rt6_src:" NIP6_FMT "\n",
  223. NIP6(rt->rt6i_dst.addr), NIP6(rt->rt6i_src.addr));
  224. return dst;
  225. }
  226. SCTP_DEBUG_PRINTK("NO ROUTE\n");
  227. dst_release(dst);
  228. return NULL;
  229. }
  230. /* Returns the number of consecutive initial bits that match in the 2 ipv6
  231. * addresses.
  232. */
  233. static inline int sctp_v6_addr_match_len(union sctp_addr *s1,
  234. union sctp_addr *s2)
  235. {
  236. struct in6_addr *a1 = &s1->v6.sin6_addr;
  237. struct in6_addr *a2 = &s2->v6.sin6_addr;
  238. int i, j;
  239. for (i = 0; i < 4 ; i++) {
  240. __be32 a1xora2;
  241. a1xora2 = a1->s6_addr32[i] ^ a2->s6_addr32[i];
  242. if ((j = fls(ntohl(a1xora2))))
  243. return (i * 32 + 32 - j);
  244. }
  245. return (i*32);
  246. }
  247. /* Fills in the source address(saddr) based on the destination address(daddr)
  248. * and asoc's bind address list.
  249. */
  250. static void sctp_v6_get_saddr(struct sctp_association *asoc,
  251. struct dst_entry *dst,
  252. union sctp_addr *daddr,
  253. union sctp_addr *saddr)
  254. {
  255. struct sctp_bind_addr *bp;
  256. rwlock_t *addr_lock;
  257. struct sctp_sockaddr_entry *laddr;
  258. struct list_head *pos;
  259. sctp_scope_t scope;
  260. union sctp_addr *baddr = NULL;
  261. __u8 matchlen = 0;
  262. __u8 bmatchlen;
  263. SCTP_DEBUG_PRINTK("%s: asoc:%p dst:%p "
  264. "daddr:" NIP6_FMT " ",
  265. __FUNCTION__, asoc, dst, NIP6(daddr->v6.sin6_addr));
  266. if (!asoc) {
  267. ipv6_get_saddr(dst, &daddr->v6.sin6_addr,&saddr->v6.sin6_addr);
  268. SCTP_DEBUG_PRINTK("saddr from ipv6_get_saddr: " NIP6_FMT "\n",
  269. NIP6(saddr->v6.sin6_addr));
  270. return;
  271. }
  272. scope = sctp_scope(daddr);
  273. bp = &asoc->base.bind_addr;
  274. addr_lock = &asoc->base.addr_lock;
  275. /* Go through the bind address list and find the best source address
  276. * that matches the scope of the destination address.
  277. */
  278. sctp_read_lock(addr_lock);
  279. list_for_each(pos, &bp->address_list) {
  280. laddr = list_entry(pos, struct sctp_sockaddr_entry, list);
  281. if ((laddr->use_as_src) &&
  282. (laddr->a.sa.sa_family == AF_INET6) &&
  283. (scope <= sctp_scope(&laddr->a))) {
  284. bmatchlen = sctp_v6_addr_match_len(daddr, &laddr->a);
  285. if (!baddr || (matchlen < bmatchlen)) {
  286. baddr = &laddr->a;
  287. matchlen = bmatchlen;
  288. }
  289. }
  290. }
  291. if (baddr) {
  292. memcpy(saddr, baddr, sizeof(union sctp_addr));
  293. SCTP_DEBUG_PRINTK("saddr: " NIP6_FMT "\n",
  294. NIP6(saddr->v6.sin6_addr));
  295. } else {
  296. printk(KERN_ERR "%s: asoc:%p Could not find a valid source "
  297. "address for the dest:" NIP6_FMT "\n",
  298. __FUNCTION__, asoc, NIP6(daddr->v6.sin6_addr));
  299. }
  300. sctp_read_unlock(addr_lock);
  301. }
  302. /* Make a copy of all potential local addresses. */
  303. static void sctp_v6_copy_addrlist(struct list_head *addrlist,
  304. struct net_device *dev)
  305. {
  306. struct inet6_dev *in6_dev;
  307. struct inet6_ifaddr *ifp;
  308. struct sctp_sockaddr_entry *addr;
  309. rcu_read_lock();
  310. if ((in6_dev = __in6_dev_get(dev)) == NULL) {
  311. rcu_read_unlock();
  312. return;
  313. }
  314. read_lock_bh(&in6_dev->lock);
  315. for (ifp = in6_dev->addr_list; ifp; ifp = ifp->if_next) {
  316. /* Add the address to the local list. */
  317. addr = t_new(struct sctp_sockaddr_entry, GFP_ATOMIC);
  318. if (addr) {
  319. addr->a.v6.sin6_family = AF_INET6;
  320. addr->a.v6.sin6_port = 0;
  321. addr->a.v6.sin6_addr = ifp->addr;
  322. addr->a.v6.sin6_scope_id = dev->ifindex;
  323. INIT_LIST_HEAD(&addr->list);
  324. list_add_tail(&addr->list, addrlist);
  325. }
  326. }
  327. read_unlock_bh(&in6_dev->lock);
  328. rcu_read_unlock();
  329. }
  330. /* Initialize a sockaddr_storage from in incoming skb. */
  331. static void sctp_v6_from_skb(union sctp_addr *addr,struct sk_buff *skb,
  332. int is_saddr)
  333. {
  334. void *from;
  335. __be16 *port;
  336. struct sctphdr *sh;
  337. port = &addr->v6.sin6_port;
  338. addr->v6.sin6_family = AF_INET6;
  339. addr->v6.sin6_flowinfo = 0; /* FIXME */
  340. addr->v6.sin6_scope_id = ((struct inet6_skb_parm *)skb->cb)->iif;
  341. sh = sctp_hdr(skb);
  342. if (is_saddr) {
  343. *port = sh->source;
  344. from = &ipv6_hdr(skb)->saddr;
  345. } else {
  346. *port = sh->dest;
  347. from = &ipv6_hdr(skb)->daddr;
  348. }
  349. ipv6_addr_copy(&addr->v6.sin6_addr, from);
  350. }
  351. /* Initialize an sctp_addr from a socket. */
  352. static void sctp_v6_from_sk(union sctp_addr *addr, struct sock *sk)
  353. {
  354. addr->v6.sin6_family = AF_INET6;
  355. addr->v6.sin6_port = 0;
  356. addr->v6.sin6_addr = inet6_sk(sk)->rcv_saddr;
  357. }
  358. /* Initialize sk->sk_rcv_saddr from sctp_addr. */
  359. static void sctp_v6_to_sk_saddr(union sctp_addr *addr, struct sock *sk)
  360. {
  361. if (addr->sa.sa_family == AF_INET && sctp_sk(sk)->v4mapped) {
  362. inet6_sk(sk)->rcv_saddr.s6_addr32[0] = 0;
  363. inet6_sk(sk)->rcv_saddr.s6_addr32[1] = 0;
  364. inet6_sk(sk)->rcv_saddr.s6_addr32[2] = htonl(0x0000ffff);
  365. inet6_sk(sk)->rcv_saddr.s6_addr32[3] =
  366. addr->v4.sin_addr.s_addr;
  367. } else {
  368. inet6_sk(sk)->rcv_saddr = addr->v6.sin6_addr;
  369. }
  370. }
  371. /* Initialize sk->sk_daddr from sctp_addr. */
  372. static void sctp_v6_to_sk_daddr(union sctp_addr *addr, struct sock *sk)
  373. {
  374. if (addr->sa.sa_family == AF_INET && sctp_sk(sk)->v4mapped) {
  375. inet6_sk(sk)->daddr.s6_addr32[0] = 0;
  376. inet6_sk(sk)->daddr.s6_addr32[1] = 0;
  377. inet6_sk(sk)->daddr.s6_addr32[2] = htonl(0x0000ffff);
  378. inet6_sk(sk)->daddr.s6_addr32[3] = addr->v4.sin_addr.s_addr;
  379. } else {
  380. inet6_sk(sk)->daddr = addr->v6.sin6_addr;
  381. }
  382. }
  383. /* Initialize a sctp_addr from an address parameter. */
  384. static void sctp_v6_from_addr_param(union sctp_addr *addr,
  385. union sctp_addr_param *param,
  386. __be16 port, int iif)
  387. {
  388. addr->v6.sin6_family = AF_INET6;
  389. addr->v6.sin6_port = port;
  390. addr->v6.sin6_flowinfo = 0; /* BUG */
  391. ipv6_addr_copy(&addr->v6.sin6_addr, &param->v6.addr);
  392. addr->v6.sin6_scope_id = iif;
  393. }
  394. /* Initialize an address parameter from a sctp_addr and return the length
  395. * of the address parameter.
  396. */
  397. static int sctp_v6_to_addr_param(const union sctp_addr *addr,
  398. union sctp_addr_param *param)
  399. {
  400. int length = sizeof(sctp_ipv6addr_param_t);
  401. param->v6.param_hdr.type = SCTP_PARAM_IPV6_ADDRESS;
  402. param->v6.param_hdr.length = htons(length);
  403. ipv6_addr_copy(&param->v6.addr, &addr->v6.sin6_addr);
  404. return length;
  405. }
  406. /* Initialize a sctp_addr from a dst_entry. */
  407. static void sctp_v6_dst_saddr(union sctp_addr *addr, struct dst_entry *dst,
  408. __be16 port)
  409. {
  410. struct rt6_info *rt = (struct rt6_info *)dst;
  411. addr->sa.sa_family = AF_INET6;
  412. addr->v6.sin6_port = port;
  413. ipv6_addr_copy(&addr->v6.sin6_addr, &rt->rt6i_src.addr);
  414. }
  415. /* Compare addresses exactly.
  416. * v4-mapped-v6 is also in consideration.
  417. */
  418. static int sctp_v6_cmp_addr(const union sctp_addr *addr1,
  419. const union sctp_addr *addr2)
  420. {
  421. if (addr1->sa.sa_family != addr2->sa.sa_family) {
  422. if (addr1->sa.sa_family == AF_INET &&
  423. addr2->sa.sa_family == AF_INET6 &&
  424. IPV6_ADDR_MAPPED == ipv6_addr_type(&addr2->v6.sin6_addr)) {
  425. if (addr2->v6.sin6_port == addr1->v4.sin_port &&
  426. addr2->v6.sin6_addr.s6_addr32[3] ==
  427. addr1->v4.sin_addr.s_addr)
  428. return 1;
  429. }
  430. if (addr2->sa.sa_family == AF_INET &&
  431. addr1->sa.sa_family == AF_INET6 &&
  432. IPV6_ADDR_MAPPED == ipv6_addr_type(&addr1->v6.sin6_addr)) {
  433. if (addr1->v6.sin6_port == addr2->v4.sin_port &&
  434. addr1->v6.sin6_addr.s6_addr32[3] ==
  435. addr2->v4.sin_addr.s_addr)
  436. return 1;
  437. }
  438. return 0;
  439. }
  440. if (!ipv6_addr_equal(&addr1->v6.sin6_addr, &addr2->v6.sin6_addr))
  441. return 0;
  442. /* If this is a linklocal address, compare the scope_id. */
  443. if (ipv6_addr_type(&addr1->v6.sin6_addr) & IPV6_ADDR_LINKLOCAL) {
  444. if (addr1->v6.sin6_scope_id && addr2->v6.sin6_scope_id &&
  445. (addr1->v6.sin6_scope_id != addr2->v6.sin6_scope_id)) {
  446. return 0;
  447. }
  448. }
  449. return 1;
  450. }
  451. /* Initialize addr struct to INADDR_ANY. */
  452. static void sctp_v6_inaddr_any(union sctp_addr *addr, __be16 port)
  453. {
  454. memset(addr, 0x00, sizeof(union sctp_addr));
  455. addr->v6.sin6_family = AF_INET6;
  456. addr->v6.sin6_port = port;
  457. }
  458. /* Is this a wildcard address? */
  459. static int sctp_v6_is_any(const union sctp_addr *addr)
  460. {
  461. return ipv6_addr_any(&addr->v6.sin6_addr);
  462. }
  463. /* Should this be available for binding? */
  464. static int sctp_v6_available(union sctp_addr *addr, struct sctp_sock *sp)
  465. {
  466. int type;
  467. struct in6_addr *in6 = (struct in6_addr *)&addr->v6.sin6_addr;
  468. type = ipv6_addr_type(in6);
  469. if (IPV6_ADDR_ANY == type)
  470. return 1;
  471. if (type == IPV6_ADDR_MAPPED) {
  472. if (sp && !sp->v4mapped)
  473. return 0;
  474. if (sp && ipv6_only_sock(sctp_opt2sk(sp)))
  475. return 0;
  476. sctp_v6_map_v4(addr);
  477. return sctp_get_af_specific(AF_INET)->available(addr, sp);
  478. }
  479. if (!(type & IPV6_ADDR_UNICAST))
  480. return 0;
  481. return ipv6_chk_addr(in6, NULL, 0);
  482. }
  483. /* This function checks if the address is a valid address to be used for
  484. * SCTP.
  485. *
  486. * Output:
  487. * Return 0 - If the address is a non-unicast or an illegal address.
  488. * Return 1 - If the address is a unicast.
  489. */
  490. static int sctp_v6_addr_valid(union sctp_addr *addr,
  491. struct sctp_sock *sp,
  492. const struct sk_buff *skb)
  493. {
  494. int ret = ipv6_addr_type(&addr->v6.sin6_addr);
  495. /* Support v4-mapped-v6 address. */
  496. if (ret == IPV6_ADDR_MAPPED) {
  497. /* Note: This routine is used in input, so v4-mapped-v6
  498. * are disallowed here when there is no sctp_sock.
  499. */
  500. if (!sp || !sp->v4mapped)
  501. return 0;
  502. if (sp && ipv6_only_sock(sctp_opt2sk(sp)))
  503. return 0;
  504. sctp_v6_map_v4(addr);
  505. return sctp_get_af_specific(AF_INET)->addr_valid(addr, sp, skb);
  506. }
  507. /* Is this a non-unicast address */
  508. if (!(ret & IPV6_ADDR_UNICAST))
  509. return 0;
  510. return 1;
  511. }
  512. /* What is the scope of 'addr'? */
  513. static sctp_scope_t sctp_v6_scope(union sctp_addr *addr)
  514. {
  515. int v6scope;
  516. sctp_scope_t retval;
  517. /* The IPv6 scope is really a set of bit fields.
  518. * See IFA_* in <net/if_inet6.h>. Map to a generic SCTP scope.
  519. */
  520. v6scope = ipv6_addr_scope(&addr->v6.sin6_addr);
  521. switch (v6scope) {
  522. case IFA_HOST:
  523. retval = SCTP_SCOPE_LOOPBACK;
  524. break;
  525. case IFA_LINK:
  526. retval = SCTP_SCOPE_LINK;
  527. break;
  528. case IFA_SITE:
  529. retval = SCTP_SCOPE_PRIVATE;
  530. break;
  531. default:
  532. retval = SCTP_SCOPE_GLOBAL;
  533. break;
  534. }
  535. return retval;
  536. }
  537. /* Create and initialize a new sk for the socket to be returned by accept(). */
  538. static struct sock *sctp_v6_create_accept_sk(struct sock *sk,
  539. struct sctp_association *asoc)
  540. {
  541. struct inet_sock *inet = inet_sk(sk);
  542. struct sock *newsk;
  543. struct inet_sock *newinet;
  544. struct ipv6_pinfo *newnp, *np = inet6_sk(sk);
  545. struct sctp6_sock *newsctp6sk;
  546. newsk = sk_alloc(PF_INET6, GFP_KERNEL, sk->sk_prot, 1);
  547. if (!newsk)
  548. goto out;
  549. sock_init_data(NULL, newsk);
  550. newsk->sk_type = SOCK_STREAM;
  551. newsk->sk_prot = sk->sk_prot;
  552. newsk->sk_no_check = sk->sk_no_check;
  553. newsk->sk_reuse = sk->sk_reuse;
  554. newsk->sk_destruct = inet_sock_destruct;
  555. newsk->sk_family = PF_INET6;
  556. newsk->sk_protocol = IPPROTO_SCTP;
  557. newsk->sk_backlog_rcv = sk->sk_prot->backlog_rcv;
  558. newsk->sk_shutdown = sk->sk_shutdown;
  559. sock_reset_flag(sk, SOCK_ZAPPED);
  560. newsctp6sk = (struct sctp6_sock *)newsk;
  561. inet_sk(newsk)->pinet6 = &newsctp6sk->inet6;
  562. newinet = inet_sk(newsk);
  563. newnp = inet6_sk(newsk);
  564. memcpy(newnp, np, sizeof(struct ipv6_pinfo));
  565. /* Initialize sk's sport, dport, rcv_saddr and daddr for getsockname()
  566. * and getpeername().
  567. */
  568. newinet->sport = inet->sport;
  569. newnp->saddr = np->saddr;
  570. newnp->rcv_saddr = np->rcv_saddr;
  571. newinet->dport = htons(asoc->peer.port);
  572. sctp_v6_to_sk_daddr(&asoc->peer.primary_addr, newsk);
  573. /* Init the ipv4 part of the socket since we can have sockets
  574. * using v6 API for ipv4.
  575. */
  576. newinet->uc_ttl = -1;
  577. newinet->mc_loop = 1;
  578. newinet->mc_ttl = 1;
  579. newinet->mc_index = 0;
  580. newinet->mc_list = NULL;
  581. if (ipv4_config.no_pmtu_disc)
  582. newinet->pmtudisc = IP_PMTUDISC_DONT;
  583. else
  584. newinet->pmtudisc = IP_PMTUDISC_WANT;
  585. sk_refcnt_debug_inc(newsk);
  586. if (newsk->sk_prot->init(newsk)) {
  587. sk_common_release(newsk);
  588. newsk = NULL;
  589. }
  590. out:
  591. return newsk;
  592. }
  593. /* Map v4 address to mapped v6 address */
  594. static void sctp_v6_addr_v4map(struct sctp_sock *sp, union sctp_addr *addr)
  595. {
  596. if (sp->v4mapped && AF_INET == addr->sa.sa_family)
  597. sctp_v4_map_v6(addr);
  598. }
  599. /* Where did this skb come from? */
  600. static int sctp_v6_skb_iif(const struct sk_buff *skb)
  601. {
  602. struct inet6_skb_parm *opt = (struct inet6_skb_parm *) skb->cb;
  603. return opt->iif;
  604. }
  605. /* Was this packet marked by Explicit Congestion Notification? */
  606. static int sctp_v6_is_ce(const struct sk_buff *skb)
  607. {
  608. return *((__u32 *)(ipv6_hdr(skb))) & htonl(1 << 20);
  609. }
  610. /* Dump the v6 addr to the seq file. */
  611. static void sctp_v6_seq_dump_addr(struct seq_file *seq, union sctp_addr *addr)
  612. {
  613. seq_printf(seq, NIP6_FMT " ", NIP6(addr->v6.sin6_addr));
  614. }
  615. /* Initialize a PF_INET6 socket msg_name. */
  616. static void sctp_inet6_msgname(char *msgname, int *addr_len)
  617. {
  618. struct sockaddr_in6 *sin6;
  619. sin6 = (struct sockaddr_in6 *)msgname;
  620. sin6->sin6_family = AF_INET6;
  621. sin6->sin6_flowinfo = 0;
  622. sin6->sin6_scope_id = 0; /*FIXME */
  623. *addr_len = sizeof(struct sockaddr_in6);
  624. }
  625. /* Initialize a PF_INET msgname from a ulpevent. */
  626. static void sctp_inet6_event_msgname(struct sctp_ulpevent *event,
  627. char *msgname, int *addrlen)
  628. {
  629. struct sockaddr_in6 *sin6, *sin6from;
  630. if (msgname) {
  631. union sctp_addr *addr;
  632. struct sctp_association *asoc;
  633. asoc = event->asoc;
  634. sctp_inet6_msgname(msgname, addrlen);
  635. sin6 = (struct sockaddr_in6 *)msgname;
  636. sin6->sin6_port = htons(asoc->peer.port);
  637. addr = &asoc->peer.primary_addr;
  638. /* Note: If we go to a common v6 format, this code
  639. * will change.
  640. */
  641. /* Map ipv4 address into v4-mapped-on-v6 address. */
  642. if (sctp_sk(asoc->base.sk)->v4mapped &&
  643. AF_INET == addr->sa.sa_family) {
  644. sctp_v4_map_v6((union sctp_addr *)sin6);
  645. sin6->sin6_addr.s6_addr32[3] =
  646. addr->v4.sin_addr.s_addr;
  647. return;
  648. }
  649. sin6from = &asoc->peer.primary_addr.v6;
  650. ipv6_addr_copy(&sin6->sin6_addr, &sin6from->sin6_addr);
  651. if (ipv6_addr_type(&sin6->sin6_addr) & IPV6_ADDR_LINKLOCAL)
  652. sin6->sin6_scope_id = sin6from->sin6_scope_id;
  653. }
  654. }
  655. /* Initialize a msg_name from an inbound skb. */
  656. static void sctp_inet6_skb_msgname(struct sk_buff *skb, char *msgname,
  657. int *addr_len)
  658. {
  659. struct sctphdr *sh;
  660. struct sockaddr_in6 *sin6;
  661. if (msgname) {
  662. sctp_inet6_msgname(msgname, addr_len);
  663. sin6 = (struct sockaddr_in6 *)msgname;
  664. sh = sctp_hdr(skb);
  665. sin6->sin6_port = sh->source;
  666. /* Map ipv4 address into v4-mapped-on-v6 address. */
  667. if (sctp_sk(skb->sk)->v4mapped &&
  668. ip_hdr(skb)->version == 4) {
  669. sctp_v4_map_v6((union sctp_addr *)sin6);
  670. sin6->sin6_addr.s6_addr32[3] = ip_hdr(skb)->saddr;
  671. return;
  672. }
  673. /* Otherwise, just copy the v6 address. */
  674. ipv6_addr_copy(&sin6->sin6_addr, &ipv6_hdr(skb)->saddr);
  675. if (ipv6_addr_type(&sin6->sin6_addr) & IPV6_ADDR_LINKLOCAL) {
  676. struct sctp_ulpevent *ev = sctp_skb2event(skb);
  677. sin6->sin6_scope_id = ev->iif;
  678. }
  679. }
  680. }
  681. /* Do we support this AF? */
  682. static int sctp_inet6_af_supported(sa_family_t family, struct sctp_sock *sp)
  683. {
  684. switch (family) {
  685. case AF_INET6:
  686. return 1;
  687. /* v4-mapped-v6 addresses */
  688. case AF_INET:
  689. if (!__ipv6_only_sock(sctp_opt2sk(sp)) && sp->v4mapped)
  690. return 1;
  691. default:
  692. return 0;
  693. }
  694. }
  695. /* Address matching with wildcards allowed. This extra level
  696. * of indirection lets us choose whether a PF_INET6 should
  697. * disallow any v4 addresses if we so choose.
  698. */
  699. static int sctp_inet6_cmp_addr(const union sctp_addr *addr1,
  700. const union sctp_addr *addr2,
  701. struct sctp_sock *opt)
  702. {
  703. struct sctp_af *af1, *af2;
  704. af1 = sctp_get_af_specific(addr1->sa.sa_family);
  705. af2 = sctp_get_af_specific(addr2->sa.sa_family);
  706. if (!af1 || !af2)
  707. return 0;
  708. /* Today, wildcard AF_INET/AF_INET6. */
  709. if (sctp_is_any(addr1) || sctp_is_any(addr2))
  710. return 1;
  711. if (addr1->sa.sa_family != addr2->sa.sa_family)
  712. return 0;
  713. return af1->cmp_addr(addr1, addr2);
  714. }
  715. /* Verify that the provided sockaddr looks bindable. Common verification,
  716. * has already been taken care of.
  717. */
  718. static int sctp_inet6_bind_verify(struct sctp_sock *opt, union sctp_addr *addr)
  719. {
  720. struct sctp_af *af;
  721. /* ASSERT: address family has already been verified. */
  722. if (addr->sa.sa_family != AF_INET6)
  723. af = sctp_get_af_specific(addr->sa.sa_family);
  724. else {
  725. int type = ipv6_addr_type(&addr->v6.sin6_addr);
  726. struct net_device *dev;
  727. if (type & IPV6_ADDR_LINKLOCAL) {
  728. if (!addr->v6.sin6_scope_id)
  729. return 0;
  730. dev = dev_get_by_index(addr->v6.sin6_scope_id);
  731. if (!dev)
  732. return 0;
  733. dev_put(dev);
  734. }
  735. af = opt->pf->af;
  736. }
  737. return af->available(addr, opt);
  738. }
  739. /* Verify that the provided sockaddr looks sendable. Common verification,
  740. * has already been taken care of.
  741. */
  742. static int sctp_inet6_send_verify(struct sctp_sock *opt, union sctp_addr *addr)
  743. {
  744. struct sctp_af *af = NULL;
  745. /* ASSERT: address family has already been verified. */
  746. if (addr->sa.sa_family != AF_INET6)
  747. af = sctp_get_af_specific(addr->sa.sa_family);
  748. else {
  749. int type = ipv6_addr_type(&addr->v6.sin6_addr);
  750. struct net_device *dev;
  751. if (type & IPV6_ADDR_LINKLOCAL) {
  752. if (!addr->v6.sin6_scope_id)
  753. return 0;
  754. dev = dev_get_by_index(addr->v6.sin6_scope_id);
  755. if (!dev)
  756. return 0;
  757. dev_put(dev);
  758. }
  759. af = opt->pf->af;
  760. }
  761. return af != NULL;
  762. }
  763. /* Fill in Supported Address Type information for INIT and INIT-ACK
  764. * chunks. Note: In the future, we may want to look at sock options
  765. * to determine whether a PF_INET6 socket really wants to have IPV4
  766. * addresses.
  767. * Returns number of addresses supported.
  768. */
  769. static int sctp_inet6_supported_addrs(const struct sctp_sock *opt,
  770. __be16 *types)
  771. {
  772. types[0] = SCTP_PARAM_IPV4_ADDRESS;
  773. types[1] = SCTP_PARAM_IPV6_ADDRESS;
  774. return 2;
  775. }
  776. static const struct proto_ops inet6_seqpacket_ops = {
  777. .family = PF_INET6,
  778. .owner = THIS_MODULE,
  779. .release = inet6_release,
  780. .bind = inet6_bind,
  781. .connect = inet_dgram_connect,
  782. .socketpair = sock_no_socketpair,
  783. .accept = inet_accept,
  784. .getname = inet6_getname,
  785. .poll = sctp_poll,
  786. .ioctl = inet6_ioctl,
  787. .listen = sctp_inet_listen,
  788. .shutdown = inet_shutdown,
  789. .setsockopt = sock_common_setsockopt,
  790. .getsockopt = sock_common_getsockopt,
  791. .sendmsg = inet_sendmsg,
  792. .recvmsg = sock_common_recvmsg,
  793. .mmap = sock_no_mmap,
  794. #ifdef CONFIG_COMPAT
  795. .compat_setsockopt = compat_sock_common_setsockopt,
  796. .compat_getsockopt = compat_sock_common_getsockopt,
  797. #endif
  798. };
  799. static struct inet_protosw sctpv6_seqpacket_protosw = {
  800. .type = SOCK_SEQPACKET,
  801. .protocol = IPPROTO_SCTP,
  802. .prot = &sctpv6_prot,
  803. .ops = &inet6_seqpacket_ops,
  804. .capability = -1,
  805. .no_check = 0,
  806. .flags = SCTP_PROTOSW_FLAG
  807. };
  808. static struct inet_protosw sctpv6_stream_protosw = {
  809. .type = SOCK_STREAM,
  810. .protocol = IPPROTO_SCTP,
  811. .prot = &sctpv6_prot,
  812. .ops = &inet6_seqpacket_ops,
  813. .capability = -1,
  814. .no_check = 0,
  815. .flags = SCTP_PROTOSW_FLAG,
  816. };
  817. static int sctp6_rcv(struct sk_buff **pskb)
  818. {
  819. return sctp_rcv(*pskb) ? -1 : 0;
  820. }
  821. static struct inet6_protocol sctpv6_protocol = {
  822. .handler = sctp6_rcv,
  823. .err_handler = sctp_v6_err,
  824. .flags = INET6_PROTO_NOPOLICY | INET6_PROTO_FINAL,
  825. };
  826. static struct sctp_af sctp_ipv6_specific = {
  827. .sa_family = AF_INET6,
  828. .sctp_xmit = sctp_v6_xmit,
  829. .setsockopt = ipv6_setsockopt,
  830. .getsockopt = ipv6_getsockopt,
  831. .get_dst = sctp_v6_get_dst,
  832. .get_saddr = sctp_v6_get_saddr,
  833. .copy_addrlist = sctp_v6_copy_addrlist,
  834. .from_skb = sctp_v6_from_skb,
  835. .from_sk = sctp_v6_from_sk,
  836. .to_sk_saddr = sctp_v6_to_sk_saddr,
  837. .to_sk_daddr = sctp_v6_to_sk_daddr,
  838. .from_addr_param = sctp_v6_from_addr_param,
  839. .to_addr_param = sctp_v6_to_addr_param,
  840. .dst_saddr = sctp_v6_dst_saddr,
  841. .cmp_addr = sctp_v6_cmp_addr,
  842. .scope = sctp_v6_scope,
  843. .addr_valid = sctp_v6_addr_valid,
  844. .inaddr_any = sctp_v6_inaddr_any,
  845. .is_any = sctp_v6_is_any,
  846. .available = sctp_v6_available,
  847. .skb_iif = sctp_v6_skb_iif,
  848. .is_ce = sctp_v6_is_ce,
  849. .seq_dump_addr = sctp_v6_seq_dump_addr,
  850. .net_header_len = sizeof(struct ipv6hdr),
  851. .sockaddr_len = sizeof(struct sockaddr_in6),
  852. #ifdef CONFIG_COMPAT
  853. .compat_setsockopt = compat_ipv6_setsockopt,
  854. .compat_getsockopt = compat_ipv6_getsockopt,
  855. #endif
  856. };
  857. static struct sctp_pf sctp_pf_inet6_specific = {
  858. .event_msgname = sctp_inet6_event_msgname,
  859. .skb_msgname = sctp_inet6_skb_msgname,
  860. .af_supported = sctp_inet6_af_supported,
  861. .cmp_addr = sctp_inet6_cmp_addr,
  862. .bind_verify = sctp_inet6_bind_verify,
  863. .send_verify = sctp_inet6_send_verify,
  864. .supported_addrs = sctp_inet6_supported_addrs,
  865. .create_accept_sk = sctp_v6_create_accept_sk,
  866. .addr_v4map = sctp_v6_addr_v4map,
  867. .af = &sctp_ipv6_specific,
  868. };
  869. /* Initialize IPv6 support and register with socket layer. */
  870. int sctp_v6_init(void)
  871. {
  872. int rc;
  873. /* Register the SCTP specific PF_INET6 functions. */
  874. sctp_register_pf(&sctp_pf_inet6_specific, PF_INET6);
  875. /* Register the SCTP specific AF_INET6 functions. */
  876. sctp_register_af(&sctp_ipv6_specific);
  877. rc = proto_register(&sctpv6_prot, 1);
  878. if (rc)
  879. return rc;
  880. /* Add SCTPv6(UDP and TCP style) to inetsw6 linked list. */
  881. inet6_register_protosw(&sctpv6_seqpacket_protosw);
  882. inet6_register_protosw(&sctpv6_stream_protosw);
  883. return 0;
  884. }
  885. /* Register with inet6 layer. */
  886. int sctp_v6_add_protocol(void)
  887. {
  888. /* Register notifier for inet6 address additions/deletions. */
  889. register_inet6addr_notifier(&sctp_inet6addr_notifier);
  890. if (inet6_add_protocol(&sctpv6_protocol, IPPROTO_SCTP) < 0)
  891. return -EAGAIN;
  892. return 0;
  893. }
  894. /* IPv6 specific exit support. */
  895. void sctp_v6_exit(void)
  896. {
  897. inet6_unregister_protosw(&sctpv6_seqpacket_protosw);
  898. inet6_unregister_protosw(&sctpv6_stream_protosw);
  899. proto_unregister(&sctpv6_prot);
  900. list_del(&sctp_ipv6_specific.list);
  901. }
  902. /* Unregister with inet6 layer. */
  903. void sctp_v6_del_protocol(void)
  904. {
  905. inet6_del_protocol(&sctpv6_protocol, IPPROTO_SCTP);
  906. unregister_inet6addr_notifier(&sctp_inet6addr_notifier);
  907. }