root/net/inet/protocol.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. inet_get_protocol
  2. inet_add_protocol
  3. inet_del_protocol

   1 /*
   2  * INET         An implementation of the TCP/IP protocol suite for the LINUX
   3  *              operating system.  INET is implemented using the  BSD Socket
   4  *              interface as the means of communication with the user level.
   5  *
   6  *              INET protocol dispatch tables.
   7  *
   8  * Version:     @(#)protocol.c  1.0.5   05/25/93
   9  *
  10  * Authors:     Ross Biro, <bir7@leland.Stanford.Edu>
  11  *              Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
  12  *
  13  *              This program is free software; you can redistribute it and/or
  14  *              modify it under the terms of the GNU General Public License
  15  *              as published by the Free Software Foundation; either version
  16  *              2 of the License, or (at your option) any later version.
  17  */
  18 #include <asm/segment.h>
  19 #include <asm/system.h>
  20 #include <linux/types.h>
  21 #include <linux/kernel.h>
  22 #include <linux/sched.h>
  23 #include <linux/string.h>
  24 #include <linux/socket.h>
  25 #include <linux/in.h>
  26 #include "inet.h"
  27 #include "dev.h"
  28 #include "ip.h"
  29 #include "protocol.h"
  30 #include "tcp.h"
  31 #include "skbuff.h"
  32 #include "sock.h"
  33 #include "icmp.h"
  34 #include "udp.h"
  35 
  36 
  37 static struct inet_protocol tcp_protocol = {
  38   tcp_rcv,              /* TCP handler          */
  39   tcp_err,              /* TCP error control    */
  40   NULL,                 /* next                 */
  41   IPPROTO_TCP,          /* protocol ID          */
  42   0,                    /* copy                 */
  43   NULL                  /* data                 */
  44 };
  45 
  46 
  47 static struct inet_protocol udp_protocol = {
  48   udp_rcv,              /* UDP handler          */
  49   NULL,                 /* UDP error control    */
  50   &tcp_protocol,        /* next                 */
  51   IPPROTO_UDP,          /* protocol ID          */
  52   0,                    /* copy                 */
  53   NULL                  /* data                 */
  54 };
  55 
  56 
  57 static struct inet_protocol icmp_protocol = {
  58   icmp_rcv,             /* ICMP handler         */
  59   NULL,                 /* ICMP error control   */
  60   &udp_protocol,        /* next                 */
  61   IPPROTO_ICMP,         /* protocol ID          */
  62   0,                    /* copy                 */
  63   NULL                  /* data                 */
  64 };
  65 
  66 
  67 struct inet_protocol *inet_protocol_base = &icmp_protocol;
  68 struct inet_protocol *inet_protos[MAX_INET_PROTOS] = {
  69   NULL
  70 };
  71 
  72 
  73 struct inet_protocol *
  74 inet_get_protocol(unsigned char prot)
     /* [previous][next][first][last][top][bottom][index][help] */
  75 {
  76   unsigned char hash;
  77   struct inet_protocol *p;
  78 
  79   DPRINTF((DBG_PROTO, "get_protocol (%d)\n ", prot));
  80   hash = prot & (MAX_INET_PROTOS - 1);
  81   for (p = inet_protos[hash] ; p != NULL; p=p->next) {
  82         DPRINTF((DBG_PROTO, "trying protocol %d\n", p->protocol));
  83         if (p->protocol == prot) return((struct inet_protocol *) p);
  84   }
  85   return(NULL);
  86 }
  87 
  88 
  89 void
  90 inet_add_protocol(struct inet_protocol *prot)
     /* [previous][next][first][last][top][bottom][index][help] */
  91 {
  92   unsigned char hash;
  93   struct inet_protocol *p2;
  94 
  95   hash = prot->protocol & (MAX_INET_PROTOS - 1);
  96   prot ->next = inet_protos[hash];
  97   inet_protos[hash] = prot;
  98   prot->copy = 0;
  99 
 100   /* Set the copy bit if we need to. */
 101   p2 = (struct inet_protocol *) prot->next;
 102   while(p2 != NULL) {
 103         if (p2->protocol == prot->protocol) {
 104                 prot->copy = 1;
 105                 break;
 106         }
 107         p2 = (struct inet_protocol *) prot->next;
 108   }
 109 }
 110 
 111 
 112 int
 113 inet_del_protocol(struct inet_protocol *prot)
     /* [previous][next][first][last][top][bottom][index][help] */
 114 {
 115   struct inet_protocol *p;
 116   struct inet_protocol *lp = NULL;
 117   unsigned char hash;
 118 
 119   hash = prot->protocol & (MAX_INET_PROTOS - 1);
 120   if (prot == inet_protos[hash]) {
 121         inet_protos[hash] = (struct inet_protocol *) inet_protos[hash]->next;
 122         return(0);
 123   }
 124 
 125   p = (struct inet_protocol *) inet_protos[hash];
 126   while(p != NULL) {
 127         /*
 128          * We have to worry if the protocol being deleted is
 129          * the last one on the list, then we may need to reset
 130          * someones copied bit.
 131          */
 132         if (p->next != NULL && p->next == prot) {
 133                 /*
 134                  * if we are the last one with this protocol and
 135                  * there is a previous one, reset its copy bit.
 136                  */
 137              if (p->copy == 0 && lp != NULL) lp->copy = 0;
 138              p->next = prot->next;
 139              return(0);
 140         }
 141 
 142         if (p->next != NULL && p->next->protocol == prot->protocol) {
 143                 lp = p;
 144         }
 145 
 146         p = (struct inet_protocol *) p->next;
 147   }
 148   return(-1);
 149 }

/* [previous][next][first][last][top][bottom][index][help] */