📄 protocol.c
字号:
/* If there is no association or if a source address is passed, no * more validation is required. */ if (!asoc || saddr) goto out; bp = &asoc->base.bind_addr; if (dst) { /* Walk through the bind address list and look for a bind * address that matches the source address of the returned dst. */ rcu_read_lock(); list_for_each_entry_rcu(laddr, &bp->address_list, list) { if (!laddr->valid || (laddr->state != SCTP_ADDR_SRC)) continue; sctp_v4_dst_saddr(&dst_saddr, dst, htons(bp->port)); if (sctp_v4_cmp_addr(&dst_saddr, &laddr->a)) goto out_unlock; } rcu_read_unlock(); /* None of the bound addresses match the source address of the * dst. So release it. */ dst_release(dst); dst = NULL; } /* Walk through the bind address list and try to get a dst that * matches a bind address as the source address. */ rcu_read_lock(); list_for_each_entry_rcu(laddr, &bp->address_list, list) { if (!laddr->valid) continue; if ((laddr->state == SCTP_ADDR_SRC) && (AF_INET == laddr->a.sa.sa_family)) { fl.fl4_src = laddr->a.v4.sin_addr.s_addr; if (!ip_route_output_key(&init_net, &rt, &fl)) { dst = &rt->u.dst; goto out_unlock; } } }out_unlock: rcu_read_unlock();out: if (dst) SCTP_DEBUG_PRINTK("rt_dst:%u.%u.%u.%u, rt_src:%u.%u.%u.%u\n", NIPQUAD(rt->rt_dst), NIPQUAD(rt->rt_src)); else SCTP_DEBUG_PRINTK("NO ROUTE\n"); return dst;}/* For v4, the source address is cached in the route entry(dst). So no need * to cache it separately and hence this is an empty routine. */static void sctp_v4_get_saddr(struct sctp_association *asoc, struct dst_entry *dst, union sctp_addr *daddr, union sctp_addr *saddr){ struct rtable *rt = (struct rtable *)dst; if (!asoc) return; if (rt) { saddr->v4.sin_family = AF_INET; saddr->v4.sin_port = htons(asoc->base.bind_addr.port); saddr->v4.sin_addr.s_addr = rt->rt_src; }}/* What interface did this skb arrive on? */static int sctp_v4_skb_iif(const struct sk_buff *skb){ return ((struct rtable *)skb->dst)->rt_iif;}/* Was this packet marked by Explicit Congestion Notification? */static int sctp_v4_is_ce(const struct sk_buff *skb){ return INET_ECN_is_ce(ip_hdr(skb)->tos);}/* Create and initialize a new sk for the socket returned by accept(). */static struct sock *sctp_v4_create_accept_sk(struct sock *sk, struct sctp_association *asoc){ struct inet_sock *inet = inet_sk(sk); struct inet_sock *newinet; struct sock *newsk = sk_alloc(sk->sk_net, PF_INET, GFP_KERNEL, sk->sk_prot); if (!newsk) goto out; sock_init_data(NULL, newsk); newsk->sk_type = SOCK_STREAM; newsk->sk_no_check = sk->sk_no_check; newsk->sk_reuse = sk->sk_reuse; newsk->sk_shutdown = sk->sk_shutdown; newsk->sk_destruct = inet_sock_destruct; newsk->sk_family = PF_INET; newsk->sk_protocol = IPPROTO_SCTP; newsk->sk_backlog_rcv = sk->sk_prot->backlog_rcv; sock_reset_flag(newsk, SOCK_ZAPPED); newinet = inet_sk(newsk); /* Initialize sk's sport, dport, rcv_saddr and daddr for * getsockname() and getpeername() */ newinet->sport = inet->sport; newinet->saddr = inet->saddr; newinet->rcv_saddr = inet->rcv_saddr; newinet->dport = htons(asoc->peer.port); newinet->daddr = asoc->peer.primary_addr.v4.sin_addr.s_addr; newinet->pmtudisc = inet->pmtudisc; newinet->id = asoc->next_tsn ^ jiffies; newinet->uc_ttl = -1; newinet->mc_loop = 1; newinet->mc_ttl = 1; newinet->mc_index = 0; newinet->mc_list = NULL; sk_refcnt_debug_inc(newsk); if (newsk->sk_prot->init(newsk)) { sk_common_release(newsk); newsk = NULL; }out: return newsk;}/* Map address, empty for v4 family */static void sctp_v4_addr_v4map(struct sctp_sock *sp, union sctp_addr *addr){ /* Empty */}/* Dump the v4 addr to the seq file. */static void sctp_v4_seq_dump_addr(struct seq_file *seq, union sctp_addr *addr){ seq_printf(seq, "%d.%d.%d.%d ", NIPQUAD(addr->v4.sin_addr));}/* Event handler for inet address addition/deletion events. * The sctp_local_addr_list needs to be protocted by a spin lock since * multiple notifiers (say IPv4 and IPv6) may be running at the same * time and thus corrupt the list. * The reader side is protected with RCU. */static int sctp_inetaddr_event(struct notifier_block *this, unsigned long ev, void *ptr){ struct in_ifaddr *ifa = (struct in_ifaddr *)ptr; struct sctp_sockaddr_entry *addr = NULL; struct sctp_sockaddr_entry *temp; int found = 0; switch (ev) { case NETDEV_UP: addr = kmalloc(sizeof(struct sctp_sockaddr_entry), GFP_ATOMIC); if (addr) { addr->a.v4.sin_family = AF_INET; addr->a.v4.sin_port = 0; addr->a.v4.sin_addr.s_addr = ifa->ifa_local; addr->valid = 1; spin_lock_bh(&sctp_local_addr_lock); list_add_tail_rcu(&addr->list, &sctp_local_addr_list); spin_unlock_bh(&sctp_local_addr_lock); } break; case NETDEV_DOWN: spin_lock_bh(&sctp_local_addr_lock); list_for_each_entry_safe(addr, temp, &sctp_local_addr_list, list) { if (addr->a.sa.sa_family == AF_INET && addr->a.v4.sin_addr.s_addr == ifa->ifa_local) { found = 1; addr->valid = 0; list_del_rcu(&addr->list); break; } } spin_unlock_bh(&sctp_local_addr_lock); if (found) call_rcu(&addr->rcu, sctp_local_addr_free); break; } return NOTIFY_DONE;}/* * Initialize the control inode/socket with a control endpoint data * structure. This endpoint is reserved exclusively for the OOTB processing. */static int sctp_ctl_sock_init(void){ int err; sa_family_t family; if (sctp_get_pf_specific(PF_INET6)) family = PF_INET6; else family = PF_INET; err = sock_create_kern(family, SOCK_SEQPACKET, IPPROTO_SCTP, &sctp_ctl_socket); if (err < 0) { printk(KERN_ERR "SCTP: Failed to create the SCTP control socket.\n"); return err; } sctp_ctl_socket->sk->sk_allocation = GFP_ATOMIC; inet_sk(sctp_ctl_socket->sk)->uc_ttl = -1; return 0;}/* Register address family specific functions. */int sctp_register_af(struct sctp_af *af){ switch (af->sa_family) { case AF_INET: if (sctp_af_v4_specific) return 0; sctp_af_v4_specific = af; break; case AF_INET6: if (sctp_af_v6_specific) return 0; sctp_af_v6_specific = af; break; default: return 0; } INIT_LIST_HEAD(&af->list); list_add_tail(&af->list, &sctp_address_families); return 1;}/* Get the table of functions for manipulating a particular address * family. */struct sctp_af *sctp_get_af_specific(sa_family_t family){ switch (family) { case AF_INET: return sctp_af_v4_specific; case AF_INET6: return sctp_af_v6_specific; default: return NULL; }}/* Common code to initialize a AF_INET msg_name. */static void sctp_inet_msgname(char *msgname, int *addr_len){ struct sockaddr_in *sin; sin = (struct sockaddr_in *)msgname; *addr_len = sizeof(struct sockaddr_in); sin->sin_family = AF_INET; memset(sin->sin_zero, 0, sizeof(sin->sin_zero));}/* Copy the primary address of the peer primary address as the msg_name. */static void sctp_inet_event_msgname(struct sctp_ulpevent *event, char *msgname, int *addr_len){ struct sockaddr_in *sin, *sinfrom; if (msgname) { struct sctp_association *asoc; asoc = event->asoc; sctp_inet_msgname(msgname, addr_len); sin = (struct sockaddr_in *)msgname; sinfrom = &asoc->peer.primary_addr.v4; sin->sin_port = htons(asoc->peer.port); sin->sin_addr.s_addr = sinfrom->sin_addr.s_addr; }}/* Initialize and copy out a msgname from an inbound skb. */static void sctp_inet_skb_msgname(struct sk_buff *skb, char *msgname, int *len){ if (msgname) { struct sctphdr *sh = sctp_hdr(skb); struct sockaddr_in *sin = (struct sockaddr_in *)msgname; sctp_inet_msgname(msgname, len); sin->sin_port = sh->source; sin->sin_addr.s_addr = ip_hdr(skb)->saddr; }}/* Do we support this AF? */static int sctp_inet_af_supported(sa_family_t family, struct sctp_sock *sp){ /* PF_INET only supports AF_INET addresses. */ return (AF_INET == family);}/* Address matching with wildcards allowed. */static int sctp_inet_cmp_addr(const union sctp_addr *addr1, const union sctp_addr *addr2, struct sctp_sock *opt){ /* PF_INET only supports AF_INET addresses. */ if (addr1->sa.sa_family != addr2->sa.sa_family) return 0; if (htonl(INADDR_ANY) == addr1->v4.sin_addr.s_addr || htonl(INADDR_ANY) == addr2->v4.sin_addr.s_addr) return 1; if (addr1->v4.sin_addr.s_addr == addr2->v4.sin_addr.s_addr) return 1; return 0;}/* Verify that provided sockaddr looks bindable. Common verification has * already been taken care of. */static int sctp_inet_bind_verify(struct sctp_sock *opt, union sctp_addr *addr){ return sctp_v4_available(addr, opt);}/* Verify that sockaddr looks sendable. Common verification has already * been taken care of. */static int sctp_inet_send_verify(struct sctp_sock *opt, union sctp_addr *addr){ return 1;}/* Fill in Supported Address Type information for INIT and INIT-ACK * chunks. Returns number of addresses supported. */static int sctp_inet_supported_addrs(const struct sctp_sock *opt, __be16 *types){ types[0] = SCTP_PARAM_IPV4_ADDRESS; return 1;}/* Wrapper routine that calls the ip transmit routine. */static inline int sctp_v4_xmit(struct sk_buff *skb, struct sctp_transport *transport, int ipfragok){ SCTP_DEBUG_PRINTK("%s: skb:%p, len:%d, " "src:%u.%u.%u.%u, dst:%u.%u.%u.%u\n", __FUNCTION__, skb, skb->len, NIPQUAD(((struct rtable *)skb->dst)->rt_src), NIPQUAD(((struct rtable *)skb->dst)->rt_dst)); SCTP_INC_STATS(SCTP_MIB_OUTSCTPPACKS); return ip_queue_xmit(skb, ipfragok);}static struct sctp_af sctp_af_inet;static struct sctp_pf sctp_pf_inet = { .event_msgname = sctp_inet_event_msgname, .skb_msgname = sctp_inet_skb_msgname, .af_supported = sctp_inet_af_supported, .cmp_addr = sctp_inet_cmp_addr, .bind_verify = sctp_inet_bind_verify, .send_verify = sctp_inet_send_verify, .supported_addrs = sctp_inet_supported_addrs, .create_accept_sk = sctp_v4_create_accept_sk, .addr_v4map = sctp_v4_addr_v4map, .af = &sctp_af_inet};/* Notifier for inetaddr addition/deletion events. */static struct notifier_block sctp_inetaddr_notifier = { .notifier_call = sctp_inetaddr_event,};/* Socket operations. */static const struct proto_ops inet_seqpacket_ops = { .family = PF_INET, .owner = THIS_MODULE, .release = inet_release, /* Needs to be wrapped... */ .bind = inet_bind, .connect = inet_dgram_connect, .socketpair = sock_no_socketpair, .accept = inet_accept, .getname = inet_getname, /* Semantics are different. */ .poll = sctp_poll, .ioctl = inet_ioctl, .listen = sctp_inet_listen, .shutdown = inet_shutdown, /* Looks harmless. */ .setsockopt = sock_common_setsockopt, /* IP_SOL IP_OPTION is a problem */ .getsockopt = sock_common_getsockopt, .sendmsg = inet_sendmsg, .recvmsg = sock_common_recvmsg, .mmap = sock_no_mmap, .sendpage = sock_no_sendpage,#ifdef CONFIG_COMPAT .compat_setsockopt = compat_sock_common_setsockopt, .compat_getsockopt = compat_sock_common_getsockopt,#endif};/* Registration with AF_INET family. */static struct inet_protosw sctp_seqpacket_protosw = { .type = SOCK_SEQPACKET, .protocol = IPPROTO_SCTP, .prot = &sctp_prot, .ops = &inet_seqpacket_ops, .capability = -1, .no_check = 0, .flags = SCTP_PROTOSW_FLAG};static struct inet_protosw sctp_stream_protosw = { .type = SOCK_STREAM, .protocol = IPPROTO_SCTP, .prot = &sctp_prot, .ops = &inet_seqpacket_ops, .capability = -1, .no_check = 0, .flags = SCTP_PROTOSW_FLAG};/* Register with IP layer. */static struct net_protocol sctp_protocol = { .handler = sctp_rcv, .err_handler = sctp_v4_err, .no_policy = 1,};/* IPv4 address related functions. */static struct sctp_af sctp_af_inet = { .sa_family = AF_INET, .sctp_xmit = sctp_v4_xmit, .setsockopt = ip_setsockopt, .getsockopt = ip_getsockopt, .get_dst = sctp_v4_get_dst,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -