2 * Copyright 2000, International Business Machines Corporation and others.
5 * This software has been released under the terms of the IBM Public
6 * License. For details, see the LICENSE file in the top-level source
7 * directory or online at http://www.openafs.org/dl/license10.html
11 * rx_knet.c - RX kernel send, receive and timer routines.
13 * Linux implementation.
15 #include <afsconfig.h>
16 #include "afs/param.h"
19 #include <linux/version.h>
20 #ifdef AFS_LINUX22_ENV
21 #include "rx/rx_kcommon.h"
22 #if defined(AFS_LINUX24_ENV)
23 #include "h/smp_lock.h"
25 #include <asm/uaccess.h>
27 #include <linux/errqueue.h>
28 #include <linux/icmp.h>
32 * open and bind RX socket
35 rxk_NewSocketHost(afs_uint32 ahost, short aport)
38 struct sockaddr_in myaddr;
42 int pmtu = IP_PMTUDISC_WANT;
45 int pmtu = IP_PMTUDISC_DONT;
48 /* We need a better test for this. if you need it back, tell us
51 #ifdef LINUX_KERNEL_SOCK_CREATE_V
52 code = sock_create(AF_INET, SOCK_DGRAM, IPPROTO_UDP, &sockp, 0);
54 code = sock_create(AF_INET, SOCK_DGRAM, IPPROTO_UDP, &sockp);
60 myaddr.sin_family = AF_INET;
61 myaddr.sin_addr.s_addr = ahost;
62 myaddr.sin_port = aport;
64 sockp->ops->bind(sockp, (struct sockaddr *)&myaddr, sizeof(myaddr));
67 #if defined(AFS_LINUX24_ENV)
68 printk("sock_release(rx_socket) FIXME\n");
76 sockp->ops->setsockopt(sockp, SOL_IP, IP_MTU_DISCOVER, (char *)&pmtu,
79 sockp->ops->setsockopt(sockp, SOL_IP, IP_RECVERR, (char *)&do_recverr,
83 return (osi_socket *)sockp;
87 rxk_NewSocket(short aport)
89 return rxk_NewSocketHost(htonl(INADDR_ANY), aport);
92 /* free socket allocated by osi_NetSocket */
94 rxk_FreeSocket(register struct socket *asocket)
96 AFS_STATCNT(osi_FreeSocket);
102 handle_socket_error(osi_socket so)
106 struct cmsghdr *cmsg;
107 struct sock_extended_err *err;
108 struct sockaddr_in addr;
109 struct sockaddr *offender;
112 struct socket *sop = (struct socket *)so;
114 if (!(controlmsgbuf=rxi_Alloc(256)))
116 msg.msg_name = &addr;
117 msg.msg_namelen = sizeof(addr);
120 msg.msg_control = controlmsgbuf;
121 msg.msg_controllen = 256;
125 code = sock_recvmsg(sop, &msg, 256, MSG_ERRQUEUE|MSG_DONTWAIT|MSG_TRUNC);
128 if (code < 0 || !(msg.msg_flags & MSG_ERRQUEUE))
131 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
132 if (CMSG_OK(&msg, cmsg) && cmsg->cmsg_level == SOL_IP &&
133 cmsg->cmsg_type == IP_RECVERR)
138 err = CMSG_DATA(cmsg);
139 offender = SO_EE_OFFENDER(err);
141 if (offender->sa_family != AF_INET)
144 memcpy(&addr, offender, sizeof(addr));
146 if (err->ee_origin == SO_EE_ORIGIN_ICMP &&
147 err->ee_type == ICMP_DEST_UNREACH &&
148 err->ee_code == ICMP_FRAG_NEEDED) {
149 rxi_SetPeerMtu(ntohl(addr.sin_addr.s_addr), ntohs(addr.sin_port),
152 /* other DEST_UNREACH's and TIME_EXCEEDED should be dealt with too */
155 rxi_Free(controlmsgbuf, 256);
167 osi_NetSend(osi_socket sop, struct sockaddr_in *to, struct iovec *iovec,
168 int iovcnt, afs_int32 size, int istack)
179 esize = sizeof(sockerr);
181 sop->ops->getsockopt(sop, SOL_SOCKET, SO_ERROR, (char *)&sockerr,
186 handle_socket_error(sop);
190 msg.msg_iovlen = iovcnt;
193 msg.msg_namelen = sizeof(*to);
194 msg.msg_control = NULL;
195 msg.msg_controllen = 0;
199 code = sock_sendmsg(sop, &msg, size);
201 return (code < 0) ? code : 0;
206 * OS dependent part of kernel RX listener thread.
209 * so socket to receive on, typically rx_socket
210 * from pointer to a sockaddr_in.
211 * iov array of iovecs to fill in.
212 * iovcnt how many iovecs there are.
213 * lengthp IN/OUT in: total space available in iovecs. out: size of read.
217 * error code (such as EINTER) if not
220 * Note that the maximum number of iovecs is 2 + RX_MAXWVECS. This is
221 * so we have a little space to look for packets larger than
224 int rxk_lastSocketError;
225 int rxk_nSocketErrors;
227 osi_NetReceive(osi_socket so, struct sockaddr_in *from, struct iovec *iov,
228 int iovcnt, int *lengthp)
237 struct iovec tmpvec[RX_MAXWVECS + 2];
238 struct socket *sop = (struct socket *)so;
240 if (iovcnt > RX_MAXWVECS + 2) {
241 osi_Panic("Too many (%d) iovecs passed to osi_NetReceive\n", iovcnt);
246 esize = sizeof(sockerr);
248 sop->ops->getsockopt(sop, SOL_SOCKET, SO_ERROR, (char *)&sockerr,
253 handle_socket_error(so);
256 memcpy(tmpvec, iov, iovcnt * sizeof(struct iovec));
258 msg.msg_iov = tmpvec;
259 msg.msg_iovlen = iovcnt;
260 msg.msg_control = NULL;
261 msg.msg_controllen = 0;
265 code = sock_recvmsg(sop, &msg, *lengthp, 0);
269 #ifdef AFS_LINUX26_ENV
273 current->flags & PF_FREEZE
275 #if defined(STRUCT_TASK_STRUCT_HAS_TODO)
278 #if defined(STRUCT_TASK_STRUCT_HAS_THREAD_INFO)
279 test_ti_thread_flag(current->thread_info, TIF_FREEZE)
281 test_ti_thread_flag(task_thread_info(current), TIF_FREEZE)
286 #ifdef LINUX_REFRIGERATOR_TAKES_PF_FREEZE
287 refrigerator(PF_FREEZE);
291 set_current_state(TASK_INTERRUPTIBLE);
295 /* Clear the error before using the socket again.
296 * Oh joy, Linux has hidden header files as well. It appears we can
297 * simply call again and have it clear itself via sock_error().
299 #ifdef AFS_LINUX22_ENV
300 flush_signals(current); /* We don't want no stinkin' signals. */
302 current->signal = 0; /* We don't want no stinkin' signals. */
304 rxk_lastSocketError = code;
313 #ifdef EXPORTED_TASKLIST_LOCK
314 extern rwlock_t tasklist_lock __attribute__((weak));
317 osi_StopListener(void)
319 extern struct task_struct *rxk_ListenerTask;
321 while (rxk_ListenerTask) {
322 if (rxk_ListenerTask) {
323 flush_signals(rxk_ListenerTask);
324 force_sig(SIGKILL, rxk_ListenerTask);
326 if (!rxk_ListenerTask)
328 afs_osi_Sleep(&rxk_ListenerTask);
330 sock_release(rx_socket);
334 #endif /* AFS_LINUX22_ENV */